From 71653b20bba191b5e5851566bfb3b62c4def174c Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Wed, 9 Aug 2023 15:02:16 +0200 Subject: [PATCH 1/9] JSON parser abstraction. --- jme3-plugins-json-gson/build.gradle | 16 +++ .../java/com/jme3/plugins/gson/GsonArray.java | 79 +++++++++++++ .../com/jme3/plugins/gson/GsonElement.java | 76 ++++++++++++ .../com/jme3/plugins/gson/GsonObject.java | 109 ++++++++++++++++++ .../com/jme3/plugins/gson/GsonParser.java | 49 ++++++++ .../com/jme3/plugins/gson/GsonPrimitive.java | 44 +++++++ jme3-plugins-json/build.gradle | 14 +++ .../main/java/com/jme3/plugins/json/Json.java | 87 ++++++++++++++ .../java/com/jme3/plugins/json/JsonArray.java | 51 ++++++++ .../com/jme3/plugins/json/JsonElement.java | 77 +++++++++++++ .../com/jme3/plugins/json/JsonObject.java | 83 +++++++++++++ .../com/jme3/plugins/json/JsonParser.java | 42 +++++++ .../com/jme3/plugins/json/JsonPrimitive.java | 45 ++++++++ jme3-plugins/build.gradle | 3 +- .../plugins/gltf/CustomContentManager.java | 3 +- .../scene/plugins/gltf/ExtensionLoader.java | 2 +- .../jme3/scene/plugins/gltf/ExtrasLoader.java | 2 +- .../jme3/scene/plugins/gltf/GltfLoader.java | 6 +- .../jme3/scene/plugins/gltf/GltfUtils.java | 8 +- .../gltf/LightsPunctualExtensionLoader.java | 5 +- .../gltf/PBRSpecGlossExtensionLoader.java | 2 +- .../gltf/TextureTransformExtensionLoader.java | 5 +- .../plugins/gltf/UnlitExtensionLoader.java | 2 +- settings.gradle | 3 + 24 files changed, 796 insertions(+), 17 deletions(-) create mode 100644 jme3-plugins-json-gson/build.gradle create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java create mode 100644 jme3-plugins-json/build.gradle create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java diff --git a/jme3-plugins-json-gson/build.gradle b/jme3-plugins-json-gson/build.gradle new file mode 100644 index 0000000000..6d22f7a774 --- /dev/null +++ b/jme3-plugins-json-gson/build.gradle @@ -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' + +} diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java new file mode 100644 index 0000000000..ef87101d29 --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java @@ -0,0 +1,79 @@ +/* + * 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.*; + +/** + * GSON implementation of {@link JsonArray}. + */ +public class GsonArray extends GsonElement implements JsonArray { + public GsonArray(com.google.gson.JsonElement element) { + super(element); + } + + private com.google.gson.JsonArray arr() { + return element.getAsJsonArray(); + } + + @Override + public Iterator iterator() { + return new Iterator() { + Iterator 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 el==null?null:new GsonElement(el); + } + + @Override + public int size() { + return arr().size(); + } + +} diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java new file mode 100644 index 0000000000..343cc78de1 --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java @@ -0,0 +1,76 @@ +/* + * 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.*; + +/** + * GSON implementation of {@link JsonElement} + */ +public class GsonElement implements JsonElement { + protected com.google.gson.JsonElement element; + + public GsonElement(com.google.gson.JsonElement element) { + this.element = element; + } + + @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 boolean getAsBoolean() { + return element.getAsBoolean(); + } + + @Override + public JsonArray getAsJsonArray() { + return new GsonArray(element.getAsJsonArray()); + } + +} diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java new file mode 100644 index 0000000000..ed926ba106 --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java @@ -0,0 +1,109 @@ +/* + * 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.*; + +/** + * GSON implementation of {@link JsonObject} + */ +public class GsonObject extends GsonElement implements JsonObject { + + public 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 el==null?null:new GsonArray(el); + } + + @Override + public JsonObject getAsJsonObject(String string) { + com.google.gson.JsonObject el = obj().getAsJsonObject(string); + return el==null?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 el==null?null:new GsonElement(el); + } + + @Override + public Entry[] entrySet() { + Set> entrySet = obj().entrySet(); + Entry[] entries = new Entry[entrySet.size()]; + int i = 0; + for (Entry entry : entrySet) { + + Entry e = new Entry() { + @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); + return el==null?null:new GsonPrimitive(el); + } +} \ No newline at end of file diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java new file mode 100644 index 0000000000..78926d27e9 --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java @@ -0,0 +1,49 @@ +/* + * 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.*; + +/** + * 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()); + } + +} diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java new file mode 100644 index 0000000000..3da46fb2e9 --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java @@ -0,0 +1,44 @@ +/* + * 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.*; + +/** + * GSON implementation of {@link JsonPrimitive} + */ +public class GsonPrimitive extends GsonElement implements JsonPrimitive { + public GsonPrimitive(com.google.gson.JsonElement element) { + super(element); + } + +} diff --git a/jme3-plugins-json/build.gradle b/jme3-plugins-json/build.gradle new file mode 100644 index 0000000000..9a0bb3e43b --- /dev/null +++ b/jme3-plugins-json/build.gradle @@ -0,0 +1,14 @@ +sourceSets { + main { + java { + + srcDir 'src/main/java' + + } + } +} + +dependencies { + + +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java new file mode 100644 index 0000000000..2a947ad42e --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java @@ -0,0 +1,87 @@ +/* + * 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.json; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.management.RuntimeErrorException; + +/** + * A json parser factory that allows you to set the parser to use. + * @author Riccardo Balbo + */ + public class Json { + public static final String PROPERTY_JSON_PARSER_IMPLEMENTATION = "com.jme3.JsonParserImplementation"; + private static final Logger LOGGER = Logger.getLogger(Json.class.getName()); + private static final String DEFAULT_JSON_PARSER_IMPLEMENTATION = "com.jme3.plugins.gson.GsonParser"; + + private static Class clazz = null; + + /** + * Set the parser to use. + * @param clazz a class that implements JsonParser + */ + public static void setParser(Class clazz) { + Json.clazz = clazz; + } + + + /** + * Create a new JsonParser instance. + * @return a new JsonParser instance + */ + @SuppressWarnings("unchecked") + public static JsonParser create() { + if (Json.clazz == null) { + String className = System.getProperty(PROPERTY_JSON_PARSER_IMPLEMENTATION, DEFAULT_JSON_PARSER_IMPLEMENTATION); + try { + Json.clazz= (Class) Class.forName(className); + } catch (final Throwable e) { + LOGGER.log(Level.WARNING, "Unable to access {0}", className); + try{ + Json.clazz = (Class) Class.forName(DEFAULT_JSON_PARSER_IMPLEMENTATION); + } catch (Throwable e2) { + throw new RuntimeException("Unable to access default json parser", e2); + } + } + } + + + try { + return clazz.getDeclaredConstructor().newInstance(); + } catch (Exception e) { + throw new RuntimeException("Could not instantiate JsonParser class " + clazz.getName(), e); + } + + } +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java new file mode 100644 index 0000000000..992e00fbf4 --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java @@ -0,0 +1,51 @@ +/* + * 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.json; + +/** + * Represents an array. + * @author Riccardo Balbo + */ +public interface JsonArray extends Iterable { + /** + * Get the element at index i + * @param i index + * @return the element + */ + JsonElement get(int i); + + /** + * Get the size of the array + * @return the size + */ + int size(); +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java new file mode 100644 index 0000000000..ec7068c895 --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java @@ -0,0 +1,77 @@ +/* + * 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.json; + +/** + * A generic element + * @author Riccardo Balbo + */ +public interface JsonElement { + + /** + * Returns the object as a String + * @return the string + */ + String getAsString(); + + /** + * Returns the object as a JsonObject + * @return the JsonObject + */ + JsonObject getAsJsonObject(); + + /** + * Returns the object as a float + * @return the float + */ + float getAsFloat(); + + /** + * Returns the object as an int + * @return the int + */ + int getAsInt(); + + + /** + * Returns the object as a boolean + * @return the boolean + */ + boolean getAsBoolean(); + + /** + * Returns the object as a JsonArray + * @return the JsonArray + */ + JsonArray getAsJsonArray(); + +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java new file mode 100644 index 0000000000..7f108cacc8 --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java @@ -0,0 +1,83 @@ +/* + * 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.json; + +import java.util.Map.Entry; + +/** + * A generic object/map + * @author Riccardo Balbo + */ +public interface JsonObject extends JsonElement{ + + /** + * Returns the object property as a String + * @param string name of the property + * @return the string + */ + public JsonArray getAsJsonArray(String string) ; + + /** + * Returns the object property as a JsonObject + * @param string name of the property + * @return the JsonObject + */ + public JsonObject getAsJsonObject(String string); + + /** + * Check if the object has a property + * @param string name of the property + * @return true if it exists, false otherwise + */ + public boolean has(String string); + + /** + * Returns the object property as generic element + * @param string name of the property + * @return the element + */ + public JsonElement get(String string); + + /** + * Returns the object's key-value pairs + * @return an array of key-value pairs + */ + public Entry[] entrySet(); + + /** + * Returns the object property as a wrapped primitive + * @param string name of the property + * @return the wrapped primitive + */ + public JsonPrimitive getAsJsonPrimitive(String string); + +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java new file mode 100644 index 0000000000..14833fc6ef --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java @@ -0,0 +1,42 @@ +/* + * 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.json; + +import java.io.InputStream; + +/** + * A json parser + * @author Riccardo Balbo + */ +public interface JsonParser { + public JsonObject parse(InputStream stream); +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java new file mode 100644 index 0000000000..ac429e1ee6 --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java @@ -0,0 +1,45 @@ +/* + * 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.json; + +/** + * A wrapped primitive + * @author Riccardo Balbo + */ +public interface JsonPrimitive { + float getAsFloat(); + + int getAsInt(); + + boolean getAsBoolean(); + +} diff --git a/jme3-plugins/build.gradle b/jme3-plugins/build.gradle index b88f0a64fd..4eb9b17645 100644 --- a/jme3-plugins/build.gradle +++ b/jme3-plugins/build.gradle @@ -11,6 +11,7 @@ sourceSets { dependencies { api project(':jme3-core') - api 'com.google.code.gson:gson:2.9.1' + api project(':jme3-plugins-json') + implementation project(':jme3-plugins-json-gson') testRuntimeOnly project(':jme3-desktop') } diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java index 5e76201819..256df52032 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java @@ -31,9 +31,8 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; import com.jme3.asset.AssetLoadException; +import com.jme3.plugins.json.*; import java.io.IOException; import java.util.HashMap; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java index 4bdbb29eff..9f84a07563 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java @@ -31,7 +31,7 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.JsonElement; +import com.jme3.plugins.json.*; import java.io.IOException; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java index c470478db1..a73a1e98af 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java @@ -30,8 +30,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.gltf; +import com.jme3.plugins.json.*; -import com.google.gson.JsonElement; /** * Interface to handle a glTF extra. diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java index 2475c01ea9..626ec0050f 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java @@ -31,8 +31,8 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.*; -import com.google.gson.stream.JsonReader; +import com.jme3.plugins.json.*; + import com.jme3.anim.*; import com.jme3.asset.*; import com.jme3.material.Material; @@ -119,7 +119,7 @@ protected Object loadFromStream(AssetInfo assetInfo, InputStream stream) throws defaultMat.setFloat("Roughness", 1f); } - docRoot = JsonParser.parseReader(new JsonReader(new InputStreamReader(stream))).getAsJsonObject(); + docRoot = parse(stream); JsonObject asset = docRoot.getAsJsonObject().get("asset").getAsJsonObject(); getAsString(asset, "generator"); diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java index e5b42d99a1..d0bf36a054 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java @@ -31,11 +31,11 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.*; import com.jme3.asset.AssetInfo; import com.jme3.asset.AssetLoadException; import com.jme3.math.*; import com.jme3.scene.*; +import com.jme3.plugins.json.*; import com.jme3.texture.Texture; import com.jme3.util.*; import java.io.*; @@ -57,6 +57,12 @@ public class GltfUtils { private GltfUtils() { } + + public static JsonObject parse(InputStream stream) { + JsonParser parser = Json.create(); + return parser.parse(stream); + } + public static Mesh.Mode getMeshMode(Integer mode) { if (mode == null) { return Mesh.Mode.Triangles; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java index 6b98e3f8d7..a3b0c5d5ae 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java @@ -31,9 +31,8 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; + import com.jme3.plugins.json.*; + import com.jme3.asset.AssetLoadException; import com.jme3.light.DirectionalLight; import com.jme3.light.Light; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java index 365c49eb29..dc9088d7e7 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java @@ -31,10 +31,10 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.JsonElement; import com.jme3.asset.AssetKey; import java.io.IOException; +import com.jme3.plugins.json.*; import static com.jme3.scene.plugins.gltf.GltfUtils.getAsColor; import static com.jme3.scene.plugins.gltf.GltfUtils.getAsFloat; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java index d54382d8ac..ae1cd841bb 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java @@ -31,9 +31,8 @@ */ package com.jme3.scene.plugins.gltf; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; +import com.jme3.plugins.json.*; + import com.jme3.asset.AssetLoadException; import com.jme3.math.Matrix3f; import com.jme3.math.Vector3f; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java index b9e8d5066a..9b58b0863a 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java @@ -30,8 +30,8 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.gltf; +import com.jme3.plugins.json.*; -import com.google.gson.JsonElement; import com.jme3.asset.AssetKey; /** diff --git a/settings.gradle b/settings.gradle index 3f6acceb8e..074b2b8a50 100644 --- a/settings.gradle +++ b/settings.gradle @@ -8,6 +8,9 @@ include 'jme3-core' include 'jme3-effects' include 'jme3-networking' include 'jme3-plugins' +include 'jme3-plugins-json' +include 'jme3-plugins-json-gson' + include 'jme3-terrain' // Desktop dependent java classes From c13514592cccb8b0c216fe68fb6003c778779821 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 11:13:53 +0200 Subject: [PATCH 2/9] Use full syntax consistently in interfaces. Add javadoc for JsonParser.parse --- .../main/java/com/jme3/plugins/json/JsonArray.java | 4 ++-- .../main/java/com/jme3/plugins/json/JsonElement.java | 12 ++++++------ .../main/java/com/jme3/plugins/json/JsonParser.java | 5 +++++ .../java/com/jme3/plugins/json/JsonPrimitive.java | 6 +++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java index 992e00fbf4..500cdc91da 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java @@ -41,11 +41,11 @@ public interface JsonArray extends Iterable { * @param i index * @return the element */ - JsonElement get(int i); + public JsonElement get(int i); /** * Get the size of the array * @return the size */ - int size(); + public int size(); } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java index ec7068c895..3f197913c5 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java @@ -41,37 +41,37 @@ public interface JsonElement { * Returns the object as a String * @return the string */ - String getAsString(); + public String getAsString(); /** * Returns the object as a JsonObject * @return the JsonObject */ - JsonObject getAsJsonObject(); + public JsonObject getAsJsonObject(); /** * Returns the object as a float * @return the float */ - float getAsFloat(); + public float getAsFloat(); /** * Returns the object as an int * @return the int */ - int getAsInt(); + public int getAsInt(); /** * Returns the object as a boolean * @return the boolean */ - boolean getAsBoolean(); + public boolean getAsBoolean(); /** * Returns the object as a JsonArray * @return the JsonArray */ - JsonArray getAsJsonArray(); + public JsonArray getAsJsonArray(); } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java index 14833fc6ef..1e1efd1e70 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java @@ -38,5 +38,10 @@ * @author Riccardo Balbo */ public interface JsonParser { + /** + * Parse a json input stream and returns a {@link JsonObject} + * @param stream the stream to parse + * @return the JsonObject + */ public JsonObject parse(InputStream stream); } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java index ac429e1ee6..136ae81f44 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java @@ -36,10 +36,10 @@ * @author Riccardo Balbo */ public interface JsonPrimitive { - float getAsFloat(); + public float getAsFloat(); - int getAsInt(); + public int getAsInt(); - boolean getAsBoolean(); + public boolean getAsBoolean(); } From 2d4c533e7cd806fbd59511a0b6745f027f57acab Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 11:25:37 +0200 Subject: [PATCH 3/9] Fix formatting, restrict access to Gson implementation classes --- .../java/com/jme3/plugins/gson/GsonArray.java | 15 +++++----- .../com/jme3/plugins/gson/GsonElement.java | 14 ++++++---- .../com/jme3/plugins/gson/GsonObject.java | 24 +++++++++------- .../com/jme3/plugins/gson/GsonParser.java | 5 ++-- .../com/jme3/plugins/gson/GsonPrimitive.java | 7 ++--- .../main/java/com/jme3/plugins/json/Json.java | 27 ++++++++++-------- .../java/com/jme3/plugins/json/JsonArray.java | 6 +++- .../com/jme3/plugins/json/JsonElement.java | 8 +++++- .../com/jme3/plugins/json/JsonObject.java | 28 +++++++++++++------ .../com/jme3/plugins/json/JsonParser.java | 7 +++-- .../com/jme3/plugins/json/JsonPrimitive.java | 5 ++-- 11 files changed, 91 insertions(+), 55 deletions(-) diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java index ef87101d29..9d9cc27e1b 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java @@ -31,16 +31,17 @@ */ package com.jme3.plugins.gson; - import java.util.Iterator; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonElement; /** * GSON implementation of {@link JsonArray}. */ -public class GsonArray extends GsonElement implements JsonArray { - public GsonArray(com.google.gson.JsonElement element) { +class GsonArray extends GsonElement implements JsonArray { + + GsonArray(com.google.gson.JsonElement element) { super(element); } @@ -67,13 +68,13 @@ public JsonElement next() { @Override public JsonElement get(int i) { - com.google.gson.JsonElement el=arr().get(i); - return el==null?null:new GsonElement(el); + com.google.gson.JsonElement el = arr().get(i); + return el == null ? null : new GsonElement(el); } @Override public int size() { return arr().size(); } - + } diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java index 343cc78de1..9aabe87a95 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java @@ -31,15 +31,17 @@ */ package com.jme3.plugins.gson; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonElement; +import com.jme3.plugins.json.JsonObject; /** * GSON implementation of {@link JsonElement} */ -public class GsonElement implements JsonElement { - protected com.google.gson.JsonElement element; +class GsonElement implements JsonElement { + com.google.gson.JsonElement element; - public GsonElement(com.google.gson.JsonElement element) { + GsonElement(com.google.gson.JsonElement element) { this.element = element; } @@ -49,7 +51,7 @@ public String getAsString() { } @Override - public JsonObject getAsJsonObject() { + public JsonObject getAsJsonObject() { return new GsonObject(element.getAsJsonObject()); } @@ -72,5 +74,5 @@ public boolean getAsBoolean() { public JsonArray getAsJsonArray() { return new GsonArray(element.getAsJsonArray()); } - + } diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java index ed926ba106..34bc6e3338 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java @@ -34,16 +34,20 @@ import java.util.Set; import java.util.Map.Entry; -import com.jme3.plugins.json.*; +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} */ -public class GsonObject extends GsonElement implements JsonObject { - - public GsonObject(com.google.gson.JsonObject gsonObject) { +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; } @@ -51,13 +55,13 @@ private com.google.gson.JsonObject obj() { @Override public JsonArray getAsJsonArray(String string) { com.google.gson.JsonArray el = obj().getAsJsonArray(string); - return el==null?null:new GsonArray(el); + return el == null ? null : new GsonArray(el); } @Override public JsonObject getAsJsonObject(String string) { com.google.gson.JsonObject el = obj().getAsJsonObject(string); - return el==null?null:new GsonObject(el); + return el == null ? null : new GsonObject(el); } @Override @@ -68,7 +72,7 @@ public boolean has(String string) { @Override public JsonElement get(String string) { com.google.gson.JsonElement el = obj().get(string); - return el==null?null:new GsonElement(el); + return el == null ? null : new GsonElement(el); } @Override @@ -98,12 +102,12 @@ public GsonElement setValue(JsonElement value) { entries[i++] = e; } return entries; - + } @Override public JsonPrimitive getAsJsonPrimitive(String string) { - com.google.gson.JsonPrimitive el= obj().getAsJsonPrimitive(string); - return el==null?null:new GsonPrimitive(el); + com.google.gson.JsonPrimitive el = obj().getAsJsonPrimitive(string); + return el == null ? null : new GsonPrimitive(el); } } \ No newline at end of file diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java index 78926d27e9..fd7fd0462c 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonParser.java @@ -34,16 +34,15 @@ import java.io.InputStream; import java.io.InputStreamReader; -import com.jme3.plugins.json.*; +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()); } - } diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java index 3da46fb2e9..a78f4a2495 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java @@ -31,14 +31,13 @@ */ package com.jme3.plugins.gson; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonPrimitive; /** * GSON implementation of {@link JsonPrimitive} */ -public class GsonPrimitive extends GsonElement implements JsonPrimitive { - public GsonPrimitive(com.google.gson.JsonElement element) { +class GsonPrimitive extends GsonElement implements JsonPrimitive { + GsonPrimitive(com.google.gson.JsonElement element) { super(element); } - } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java index 2a947ad42e..7f9e309c73 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java @@ -34,13 +34,17 @@ import java.util.logging.Level; import java.util.logging.Logger; -import javax.management.RuntimeErrorException; - /** - * A json parser factory that allows you to set the parser to use. - * @author Riccardo Balbo - */ - public class Json { + * A json parser factory that allows you to set the parser to use. + * + * @author Riccardo Balbo + */ +public class Json { + /** + * The property name to set the parser to use. + * Should be set automatically by the JmeSystemDelegate. + * Note: changing this property after the first call to Json.create() will have no effect. + */ public static final String PROPERTY_JSON_PARSER_IMPLEMENTATION = "com.jme3.JsonParserImplementation"; private static final Logger LOGGER = Logger.getLogger(Json.class.getName()); private static final String DEFAULT_JSON_PARSER_IMPLEMENTATION = "com.jme3.plugins.gson.GsonParser"; @@ -49,15 +53,17 @@ public class Json { /** * Set the parser to use. - * @param clazz a class that implements JsonParser + * + * @param clazz + * a class that implements JsonParser */ public static void setParser(Class clazz) { Json.clazz = clazz; } - /** * Create a new JsonParser instance. + * * @return a new JsonParser instance */ @SuppressWarnings("unchecked") @@ -65,17 +71,16 @@ public static JsonParser create() { if (Json.clazz == null) { String className = System.getProperty(PROPERTY_JSON_PARSER_IMPLEMENTATION, DEFAULT_JSON_PARSER_IMPLEMENTATION); try { - Json.clazz= (Class) Class.forName(className); + Json.clazz = (Class) Class.forName(className); } catch (final Throwable e) { LOGGER.log(Level.WARNING, "Unable to access {0}", className); - try{ + try { Json.clazz = (Class) Class.forName(DEFAULT_JSON_PARSER_IMPLEMENTATION); } catch (Throwable e2) { throw new RuntimeException("Unable to access default json parser", e2); } } } - try { return clazz.getDeclaredConstructor().newInstance(); diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java index 500cdc91da..3be3e0321c 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonArray.java @@ -33,18 +33,22 @@ /** * Represents an array. + * * @author Riccardo Balbo */ public interface JsonArray extends Iterable { /** * Get the element at index i - * @param i index + * + * @param i + * index * @return the element */ public JsonElement get(int i); /** * Get the size of the array + * * @return the size */ public int size(); diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java index 3f197913c5..657a899876 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java @@ -33,43 +33,49 @@ /** * A generic element + * * @author Riccardo Balbo */ public interface JsonElement { /** * Returns the object as a String + * * @return the string */ public String getAsString(); /** * Returns the object as a JsonObject + * * @return the JsonObject */ public JsonObject getAsJsonObject(); /** * Returns the object as a float + * * @return the float */ public float getAsFloat(); /** * Returns the object as an int + * * @return the int */ public int getAsInt(); - /** * Returns the object as a boolean + * * @return the boolean */ public boolean getAsBoolean(); /** * Returns the object as a JsonArray + * * @return the JsonArray */ public JsonArray getAsJsonArray(); diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java index 7f108cacc8..230cba0da1 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonObject.java @@ -35,47 +35,59 @@ /** * A generic object/map + * * @author Riccardo Balbo */ -public interface JsonObject extends JsonElement{ +public interface JsonObject extends JsonElement { /** * Returns the object property as a String - * @param string name of the property - * @return the string + * + * @param string + * name of the property + * @return the string */ - public JsonArray getAsJsonArray(String string) ; + public JsonArray getAsJsonArray(String string); /** * Returns the object property as a JsonObject - * @param string name of the property + * + * @param string + * name of the property * @return the JsonObject */ public JsonObject getAsJsonObject(String string); /** * Check if the object has a property - * @param string name of the property + * + * @param string + * name of the property * @return true if it exists, false otherwise */ public boolean has(String string); /** * Returns the object property as generic element - * @param string name of the property + * + * @param string + * name of the property * @return the element */ public JsonElement get(String string); /** * Returns the object's key-value pairs + * * @return an array of key-value pairs */ public Entry[] entrySet(); /** * Returns the object property as a wrapped primitive - * @param string name of the property + * + * @param string + * name of the property * @return the wrapped primitive */ public JsonPrimitive getAsJsonPrimitive(String string); diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java index 1e1efd1e70..7e87912c87 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonParser.java @@ -35,13 +35,16 @@ /** * A json parser + * * @author Riccardo Balbo */ public interface JsonParser { /** * Parse a json input stream and returns a {@link JsonObject} - * @param stream the stream to parse + * + * @param stream + * the stream to parse * @return the JsonObject */ public JsonObject parse(InputStream stream); -} +} diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java index 136ae81f44..59c73c6458 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java @@ -33,13 +33,14 @@ /** * A wrapped primitive + * * @author Riccardo Balbo */ public interface JsonPrimitive { public float getAsFloat(); public int getAsInt(); - + public boolean getAsBoolean(); - + } From ef0a71cfe775d0040f7e1cf67621516020ffb522 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 11:40:05 +0200 Subject: [PATCH 4/9] Add package-info --- .../com/jme3/plugins/gson/package-info.java | 37 +++++++++++++++++++ .../com/jme3/plugins/json/package-info.java | 36 ++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/package-info.java create mode 100644 jme3-plugins-json/src/main/java/com/jme3/plugins/json/package-info.java diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/package-info.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/package-info.java new file mode 100644 index 0000000000..df1e88063f --- /dev/null +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/package-info.java @@ -0,0 +1,37 @@ +/* + * 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. + */ + +/** + * A JSON parser that uses Gson internally + */ + +package com.jme3.plugins.gson; diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/package-info.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/package-info.java new file mode 100644 index 0000000000..55a9208e1c --- /dev/null +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/package-info.java @@ -0,0 +1,36 @@ +/* + * 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. + */ + +/** + * An abstraction layer to implement JSON parsers in jMonkeyEngine + */ +package com.jme3.plugins.json; From 48cb684b2af92c99340976f88fbb3ea616e12a27 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 12:07:17 +0200 Subject: [PATCH 5/9] improve json parser finding --- .../main/java/com/jme3/plugins/json/Json.java | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java index 7f9e309c73..19eb8490d6 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/Json.java @@ -61,32 +61,51 @@ public static void setParser(Class clazz) { Json.clazz = clazz; } + @SuppressWarnings("unchecked") + private static Class findJsonParser(String className) { + Class clazz = null; + + try { + clazz = Class.forName(className); + } catch (final Throwable e) { + LOGGER.log(Level.WARNING, "Unable to access {0}", className); + } + + if (clazz != null && !JsonParser.class.isAssignableFrom(clazz)) { + LOGGER.log(Level.WARNING, "{0} does not implement {1}", new Object[] { className, JsonParser.class.getName() }); + clazz = null; + } + + return (Class) clazz; + } + /** * Create a new JsonParser instance. * * @return a new JsonParser instance */ - @SuppressWarnings("unchecked") + public static JsonParser create() { if (Json.clazz == null) { - String className = System.getProperty(PROPERTY_JSON_PARSER_IMPLEMENTATION, DEFAULT_JSON_PARSER_IMPLEMENTATION); - try { - Json.clazz = (Class) Class.forName(className); - } catch (final Throwable e) { - LOGGER.log(Level.WARNING, "Unable to access {0}", className); - try { - Json.clazz = (Class) Class.forName(DEFAULT_JSON_PARSER_IMPLEMENTATION); - } catch (Throwable e2) { - throw new RuntimeException("Unable to access default json parser", e2); - } + String userDefinedImpl = System.getProperty(PROPERTY_JSON_PARSER_IMPLEMENTATION, null); + if (userDefinedImpl != null) { + LOGGER.log(Level.FINE, "Loading user defined JsonParser implementation {0}", userDefinedImpl); + Json.clazz = findJsonParser(userDefinedImpl); + } + if (Json.clazz == null) { + LOGGER.log(Level.FINE, "No usable user defined JsonParser implementation found, using default implementation {0}", DEFAULT_JSON_PARSER_IMPLEMENTATION); + Json.clazz = findJsonParser(DEFAULT_JSON_PARSER_IMPLEMENTATION); } } + if (Json.clazz == null) { + throw new RuntimeException("No JsonParser implementation found"); + } + try { return clazz.getDeclaredConstructor().newInstance(); } catch (Exception e) { throw new RuntimeException("Could not instantiate JsonParser class " + clazz.getName(), e); } - } } From 74be0760b8338a00235a196f21086df26d15c903 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 12:10:48 +0200 Subject: [PATCH 6/9] Remove more wildcards --- .../com/jme3/scene/plugins/gltf/CustomContentManager.java | 3 ++- .../java/com/jme3/scene/plugins/gltf/ExtensionLoader.java | 2 +- .../gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java | 3 +-- .../gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java | 6 ++++-- .../gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java | 5 ++++- .../scene/plugins/gltf/LightsPunctualExtensionLoader.java | 5 +++-- .../scene/plugins/gltf/PBRSpecGlossExtensionLoader.java | 3 +-- .../scene/plugins/gltf/TextureTransformExtensionLoader.java | 5 +++-- .../com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java | 3 +-- 9 files changed, 20 insertions(+), 15 deletions(-) diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java index 256df52032..6168132313 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/CustomContentManager.java @@ -32,7 +32,8 @@ package com.jme3.scene.plugins.gltf; import com.jme3.asset.AssetLoadException; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonElement; import java.io.IOException; import java.util.HashMap; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java index 9f84a07563..033b200378 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtensionLoader.java @@ -31,8 +31,8 @@ */ package com.jme3.scene.plugins.gltf; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonElement; import java.io.IOException; /** diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java index a73a1e98af..3c4c9819dc 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/ExtrasLoader.java @@ -30,8 +30,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.gltf; -import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonElement; /** * Interface to handle a glTF extra. diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java index 626ec0050f..44fe325aef 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfLoader.java @@ -31,8 +31,10 @@ */ package com.jme3.scene.plugins.gltf; -import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonObject; +import com.jme3.plugins.json.JsonPrimitive; +import com.jme3.plugins.json.JsonElement; import com.jme3.anim.*; import com.jme3.asset.*; import com.jme3.material.Material; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java index d0bf36a054..5570163582 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java @@ -35,7 +35,10 @@ import com.jme3.asset.AssetLoadException; import com.jme3.math.*; import com.jme3.scene.*; -import com.jme3.plugins.json.*; +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonObject; +import com.jme3.plugins.json.JsonPrimitive; +import com.jme3.plugins.json.JsonElement; import com.jme3.texture.Texture; import com.jme3.util.*; import java.io.*; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java index a3b0c5d5ae..b3df7dbc88 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/LightsPunctualExtensionLoader.java @@ -31,8 +31,9 @@ */ package com.jme3.scene.plugins.gltf; - import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonObject; +import com.jme3.plugins.json.JsonElement; import com.jme3.asset.AssetLoadException; import com.jme3.light.DirectionalLight; import com.jme3.light.Light; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java index dc9088d7e7..2ab3862db3 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/PBRSpecGlossExtensionLoader.java @@ -34,8 +34,7 @@ import com.jme3.asset.AssetKey; import java.io.IOException; -import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonElement; import static com.jme3.scene.plugins.gltf.GltfUtils.getAsColor; import static com.jme3.scene.plugins.gltf.GltfUtils.getAsFloat; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java index ae1cd841bb..ea643cea0c 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/TextureTransformExtensionLoader.java @@ -31,8 +31,9 @@ */ package com.jme3.scene.plugins.gltf; -import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonArray; +import com.jme3.plugins.json.JsonObject; +import com.jme3.plugins.json.JsonElement; import com.jme3.asset.AssetLoadException; import com.jme3.math.Matrix3f; import com.jme3.math.Vector3f; diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java index 9b58b0863a..cb45fcc533 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/UnlitExtensionLoader.java @@ -30,8 +30,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.jme3.scene.plugins.gltf; -import com.jme3.plugins.json.*; - +import com.jme3.plugins.json.JsonElement; import com.jme3.asset.AssetKey; /** From 32c0562253251c1341e3c94041b9ee4381adf171 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 12:12:51 +0200 Subject: [PATCH 7/9] make jme3-plugins-json an "implementation" dep --- jme3-plugins/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jme3-plugins/build.gradle b/jme3-plugins/build.gradle index 4eb9b17645..1d325314ee 100644 --- a/jme3-plugins/build.gradle +++ b/jme3-plugins/build.gradle @@ -11,7 +11,7 @@ sourceSets { dependencies { api project(':jme3-core') - api project(':jme3-plugins-json') + implementation project(':jme3-plugins-json') implementation project(':jme3-plugins-json-gson') testRuntimeOnly project(':jme3-desktop') } From 6388f90a5c580c5bde9245248c92699acb4fc922 Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Thu, 10 Aug 2023 12:20:04 +0200 Subject: [PATCH 8/9] Add missing import and documentation --- .../java/com/jme3/plugins/json/JsonPrimitive.java | 15 +++++++++++++++ .../com/jme3/scene/plugins/gltf/GltfUtils.java | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java index 59c73c6458..c350922d12 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java @@ -37,10 +37,25 @@ * @author Riccardo Balbo */ public interface JsonPrimitive { + /** + * Returns the wrapped primitive as a float + * + * @return the float value + */ public float getAsFloat(); + /** + * Returns the wrapped primitive as an int + * + * @return the int value + */ public int getAsInt(); + /* + * Returns the wrapped primitive as a boolean + * + * @return the boolean value + */ public boolean getAsBoolean(); } diff --git a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java index 5570163582..2586a99f15 100644 --- a/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java +++ b/jme3-plugins/src/gltf/java/com/jme3/scene/plugins/gltf/GltfUtils.java @@ -35,9 +35,10 @@ import com.jme3.asset.AssetLoadException; import com.jme3.math.*; import com.jme3.scene.*; +import com.jme3.plugins.json.Json; +import com.jme3.plugins.json.JsonParser; import com.jme3.plugins.json.JsonArray; import com.jme3.plugins.json.JsonObject; -import com.jme3.plugins.json.JsonPrimitive; import com.jme3.plugins.json.JsonElement; import com.jme3.texture.Texture; import com.jme3.util.*; @@ -61,6 +62,11 @@ private GltfUtils() { } + /** + * Parse a json input stream and returns a {@link JsonObject} + * @param stream the stream to parse + * @return the JsonObject + */ public static JsonObject parse(InputStream stream) { JsonParser parser = Json.create(); return parser.parse(stream); From 113a1a8eea6a3608695fd575e883c2bd975a642d Mon Sep 17 00:00:00 2001 From: Riccardo Balbo Date: Mon, 21 Aug 2023 16:49:29 +0200 Subject: [PATCH 9/9] Add Json Primitive methods, use isJsonNull in null check, --- .../java/com/jme3/plugins/gson/GsonArray.java | 2 +- .../com/jme3/plugins/gson/GsonElement.java | 17 +++++++++++ .../com/jme3/plugins/gson/GsonObject.java | 12 ++++---- .../com/jme3/plugins/gson/GsonPrimitive.java | 25 ++++++++++++++-- .../com/jme3/plugins/json/JsonElement.java | 13 +++++++++ .../com/jme3/plugins/json/JsonPrimitive.java | 29 +++++++++++++++++++ 6 files changed, 89 insertions(+), 9 deletions(-) diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java index 9d9cc27e1b..d3a839a5dc 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonArray.java @@ -69,7 +69,7 @@ public JsonElement next() { @Override public JsonElement get(int i) { com.google.gson.JsonElement el = arr().get(i); - return el == null ? null : new GsonElement(el); + return isNull(el) ? null : new GsonElement(el); } @Override diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java index 9aabe87a95..062f60e8ad 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonElement.java @@ -34,6 +34,7 @@ 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} @@ -45,6 +46,12 @@ class GsonElement implements JsonElement { 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(); @@ -65,6 +72,11 @@ public int getAsInt() { return element.getAsInt(); } + @Override + public Number getAsNumber() { + return element.getAsNumber(); + } + @Override public boolean getAsBoolean() { return element.getAsBoolean(); @@ -75,4 +87,9 @@ public JsonArray getAsJsonArray() { return new GsonArray(element.getAsJsonArray()); } + @Override + public JsonPrimitive getAsJsonPrimitive() { + return new GsonPrimitive(element.getAsJsonPrimitive()); + } + } diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java index 34bc6e3338..a69afad20b 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonObject.java @@ -47,7 +47,7 @@ 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; } @@ -55,13 +55,13 @@ private com.google.gson.JsonObject obj() { @Override public JsonArray getAsJsonArray(String string) { com.google.gson.JsonArray el = obj().getAsJsonArray(string); - return el == null ? null : new GsonArray(el); + return isNull(el) ? null : new GsonArray(el); } @Override public JsonObject getAsJsonObject(String string) { com.google.gson.JsonObject el = obj().getAsJsonObject(string); - return el == null ? null : new GsonObject(el); + return isNull(el) ? null : new GsonObject(el); } @Override @@ -72,7 +72,7 @@ public boolean has(String string) { @Override public JsonElement get(String string) { com.google.gson.JsonElement el = obj().get(string); - return el == null ? null : new GsonElement(el); + return isNull(el) ? null : new GsonElement(el); } @Override @@ -107,7 +107,7 @@ public GsonElement setValue(JsonElement value) { @Override public JsonPrimitive getAsJsonPrimitive(String string) { - com.google.gson.JsonPrimitive el = obj().getAsJsonPrimitive(string); - return el == null ? null : new GsonPrimitive(el); + com.google.gson.JsonPrimitive el= obj().getAsJsonPrimitive(string); + return isNull(el) ? null : new GsonPrimitive(el); } } \ No newline at end of file diff --git a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java index a78f4a2495..91399bcebb 100644 --- a/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java +++ b/jme3-plugins-json-gson/src/main/java/com/jme3/plugins/gson/GsonPrimitive.java @@ -36,8 +36,29 @@ /** * GSON implementation of {@link JsonPrimitive} */ -class GsonPrimitive extends GsonElement implements JsonPrimitive { - GsonPrimitive(com.google.gson.JsonElement element) { +public class GsonPrimitive extends GsonElement implements JsonPrimitive { + + public GsonPrimitive(com.google.gson.JsonPrimitive element) { super(element); } + + private com.google.gson.JsonPrimitive prim() { + return (com.google.gson.JsonPrimitive) element; + } + + @Override + public boolean isNumber() { + return prim().isNumber(); + } + + @Override + public boolean isBoolean() { + return prim().isBoolean(); + } + + @Override + public boolean isString() { + return prim().isString(); + } + } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java index 657a899876..ba80e84852 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonElement.java @@ -80,4 +80,17 @@ public interface JsonElement { */ public JsonArray getAsJsonArray(); + + /** + * Returns the object as a Number + * @return the Number + */ + Number getAsNumber(); + + + /** + * Returns the object as a JsonPrimitive + * @return + */ + JsonPrimitive getAsJsonPrimitive(); } diff --git a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java index c350922d12..ed55a8db77 100644 --- a/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java +++ b/jme3-plugins-json/src/main/java/com/jme3/plugins/json/JsonPrimitive.java @@ -57,5 +57,34 @@ public interface JsonPrimitive { * @return the boolean value */ public boolean getAsBoolean(); + + /** + * Check if the wrapped primitive is a number + * @return true if it is a number + */ + public boolean isNumber(); + /** + * Check if the wrapped primitive is a boolean + * @return true if it is a boolean + */ + public boolean isBoolean(); + + /** + * Check if the wrapped primitive is a string + * @return true if it is a string + */ + public boolean isString(); + + /** + * Returns the wrapped primitive as a string + * @return the string value + */ + public String getAsString(); + + /** + * Returns the wrapped primitive as a generic number + * @return the number value + */ + public Number getAsNumber(); }