-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JSON parser abstraction. #2060
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
JSON parser abstraction. #2060
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71653b2
JSON parser abstraction.
riccardobl c135145
Use full syntax consistently in interfaces. Add javadoc for JsonParse…
riccardobl 2d4c533
Fix formatting, restrict access to Gson implementation classes
riccardobl ef0a71c
Add package-info
riccardobl 48cb684
improve json parser finding
riccardobl 74be076
Remove more wildcards
riccardobl 32c0562
make jme3-plugins-json an "implementation" dep
riccardobl 6388f90
Add missing import and documentation
riccardobl 113a1a8
Add Json Primitive methods, use isJsonNull in null check,
riccardobl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| sourceSets { | ||
| main { | ||
| java { | ||
|
|
||
| srcDir 'src/main/java' | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
|
|
||
| api project(':jme3-plugins-json') | ||
| api 'com.google.code.gson:gson:2.9.1' | ||
|
|
||
| } |
80 changes: 80 additions & 0 deletions
80
jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright (c) 2009-2023 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.plugins.gson; | ||
|
|
||
| import java.util.Iterator; | ||
|
|
||
| import com.jme3.plugins.json.JsonArray; | ||
| import com.jme3.plugins.json.JsonElement; | ||
|
|
||
| /** | ||
| * GSON implementation of {@link JsonArray}. | ||
| */ | ||
| class GsonArray extends GsonElement implements JsonArray { | ||
|
|
||
| GsonArray(com.google.gson.JsonElement element) { | ||
| super(element); | ||
| } | ||
|
|
||
| private com.google.gson.JsonArray arr() { | ||
| return element.getAsJsonArray(); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<JsonElement> iterator() { | ||
| return new Iterator<JsonElement>() { | ||
| Iterator<com.google.gson.JsonElement> it = arr().iterator(); | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return it.hasNext(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement next() { | ||
| return new GsonElement(it.next()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement get(int i) { | ||
| com.google.gson.JsonElement el = arr().get(i); | ||
| return isNull(el) ? null : new GsonElement(el); | ||
| } | ||
|
|
||
| @Override | ||
| public int size() { | ||
| return arr().size(); | ||
| } | ||
|
|
||
| } |
95 changes: 95 additions & 0 deletions
95
jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2009-2023 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.plugins.gson; | ||
|
|
||
| import com.jme3.plugins.json.JsonArray; | ||
| import com.jme3.plugins.json.JsonElement; | ||
| import com.jme3.plugins.json.JsonObject; | ||
| import com.jme3.plugins.json.JsonPrimitive; | ||
|
|
||
| /** | ||
| * GSON implementation of {@link JsonElement} | ||
| */ | ||
| class GsonElement implements JsonElement { | ||
| com.google.gson.JsonElement element; | ||
|
|
||
| GsonElement(com.google.gson.JsonElement element) { | ||
| this.element = element; | ||
| } | ||
|
|
||
| protected boolean isNull(com.google.gson.JsonElement element) { | ||
| if (element == null) return true; | ||
| if (element.isJsonNull()) return true; | ||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public String getAsString() { | ||
| return element.getAsString(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonObject getAsJsonObject() { | ||
| return new GsonObject(element.getAsJsonObject()); | ||
| } | ||
|
|
||
| @Override | ||
| public float getAsFloat() { | ||
| return element.getAsFloat(); | ||
| } | ||
|
|
||
| @Override | ||
| public int getAsInt() { | ||
| return element.getAsInt(); | ||
| } | ||
|
|
||
| @Override | ||
| public Number getAsNumber() { | ||
| return element.getAsNumber(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean getAsBoolean() { | ||
| return element.getAsBoolean(); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonArray getAsJsonArray() { | ||
| return new GsonArray(element.getAsJsonArray()); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonPrimitive getAsJsonPrimitive() { | ||
| return new GsonPrimitive(element.getAsJsonPrimitive()); | ||
| } | ||
|
|
||
| } |
113 changes: 113 additions & 0 deletions
113
jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright (c) 2009-2023 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.plugins.gson; | ||
|
|
||
| import java.util.Set; | ||
| import java.util.Map.Entry; | ||
|
|
||
| import com.jme3.plugins.json.JsonArray; | ||
| import com.jme3.plugins.json.JsonElement; | ||
| import com.jme3.plugins.json.JsonObject; | ||
| import com.jme3.plugins.json.JsonPrimitive; | ||
|
|
||
| /** | ||
| * GSON implementation of {@link JsonObject} | ||
| */ | ||
| class GsonObject extends GsonElement implements JsonObject { | ||
|
|
||
| GsonObject(com.google.gson.JsonObject gsonObject) { | ||
| super(gsonObject); | ||
| } | ||
|
|
||
| private com.google.gson.JsonObject obj() { | ||
| return (com.google.gson.JsonObject) element; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonArray getAsJsonArray(String string) { | ||
| com.google.gson.JsonArray el = obj().getAsJsonArray(string); | ||
| return isNull(el) ? null : new GsonArray(el); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonObject getAsJsonObject(String string) { | ||
| com.google.gson.JsonObject el = obj().getAsJsonObject(string); | ||
| return isNull(el) ? null : new GsonObject(el); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean has(String string) { | ||
| return obj().has(string); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonElement get(String string) { | ||
| com.google.gson.JsonElement el = obj().get(string); | ||
| return isNull(el) ? null : new GsonElement(el); | ||
| } | ||
|
|
||
| @Override | ||
| public Entry<String, JsonElement>[] entrySet() { | ||
| Set<Entry<String, com.google.gson.JsonElement>> entrySet = obj().entrySet(); | ||
riccardobl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Entry<String, JsonElement>[] entries = new Entry[entrySet.size()]; | ||
| int i = 0; | ||
| for (Entry<String, com.google.gson.JsonElement> entry : entrySet) { | ||
|
|
||
| Entry<String, JsonElement> e = new Entry<String, JsonElement>() { | ||
riccardobl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Override | ||
| public String getKey() { | ||
| return entry.getKey(); | ||
| } | ||
|
|
||
| @Override | ||
| public GsonElement getValue() { | ||
| return new GsonElement(entry.getValue()); | ||
| } | ||
|
|
||
| @Override | ||
| public GsonElement setValue(JsonElement value) { | ||
| throw new UnsupportedOperationException("Unimplemented method 'setValue'"); | ||
| } | ||
| }; | ||
|
|
||
| entries[i++] = e; | ||
| } | ||
| return entries; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public JsonPrimitive getAsJsonPrimitive(String string) { | ||
| com.google.gson.JsonPrimitive el= obj().getAsJsonPrimitive(string); | ||
riccardobl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return isNull(el) ? null : new GsonPrimitive(el); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright (c) 2009-2023 jMonkeyEngine | ||
| * All rights reserved. | ||
| * | ||
| * Redistribution and use in source and binary forms, with or without | ||
| * modification, are permitted provided that the following conditions are | ||
| * met: | ||
| * | ||
| * * Redistributions of source code must retain the above copyright | ||
| * notice, this list of conditions and the following disclaimer. | ||
| * | ||
| * * Redistributions in binary form must reproduce the above copyright | ||
| * notice, this list of conditions and the following disclaimer in the | ||
| * documentation and/or other materials provided with the distribution. | ||
| * | ||
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | ||
| * may be used to endorse or promote products derived from this software | ||
| * without specific prior written permission. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
| package com.jme3.plugins.gson; | ||
|
|
||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
|
|
||
| import com.jme3.plugins.json.JsonObject; | ||
| import com.jme3.plugins.json.JsonParser; | ||
|
|
||
| /** | ||
| * GSON implementation of {@link JsonParser} | ||
| */ | ||
| public class GsonParser implements JsonParser { | ||
| @Override | ||
| public JsonObject parse(InputStream stream) { | ||
| return new GsonObject(com.google.gson.JsonParser.parseReader(new com.google.gson.stream.JsonReader(new InputStreamReader(stream))).getAsJsonObject()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.