Skip to content

Commit

Permalink
JSON parser abstraction. (#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Aug 21, 2023
1 parent f915e56 commit 4c87531
Show file tree
Hide file tree
Showing 26 changed files with 1,038 additions and 20 deletions.
16 changes: 16 additions & 0 deletions jme3-plugins-json-gson/build.gradle
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'

}
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();
}

}
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());
}

}
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();
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>() {
@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 isNull(el) ? null : new GsonPrimitive(el);
}
}
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());
}
}

0 comments on commit 4c87531

Please sign in to comment.