Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parser abstraction. #2060

Merged
merged 9 commits into from
Aug 21, 2023
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,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.*;
riccardobl marked this conversation as resolved.
Show resolved Hide resolved

/**
* GSON implementation of {@link JsonArray}.
*/
public class GsonArray extends GsonElement implements JsonArray {
public GsonArray(com.google.gson.JsonElement element) {
riccardobl marked this conversation as resolved.
Show resolved Hide resolved
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 el==null?null:new GsonElement(el);
}

@Override
public int size() {
return arr().size();
}

}
Original file line number Diff line number Diff line change
@@ -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.*;
riccardobl marked this conversation as resolved.
Show resolved Hide resolved

/**
* GSON implementation of {@link JsonElement}
*/
public class GsonElement implements JsonElement {
protected com.google.gson.JsonElement element;
riccardobl marked this conversation as resolved.
Show resolved Hide resolved

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

}
Original file line number Diff line number Diff line change
@@ -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.*;
riccardobl marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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() {
riccardobl marked this conversation as resolved.
Show resolved Hide resolved
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<String, JsonElement>[] entrySet() {
Set<Entry<String, com.google.gson.JsonElement>> entrySet = obj().entrySet();
riccardobl marked this conversation as resolved.
Show resolved Hide resolved
Entry<String, JsonElement>[] entries = new Entry[entrySet.size()];
int i = 0;
for (Entry<String, com.google.gson.JsonElement> entry : entrySet) {

Entry<String, JsonElement> e = new Entry<String, JsonElement>() {
riccardobl marked this conversation as resolved.
Show resolved Hide resolved
@Override
public String getKey() {
return entry.getKey();
}

@Override
public GsonElement getValue() {
return new GsonElement(entry.getValue());
}

@Override
public GsonElement setValue(JsonElement value) {
throw new UnsupportedOperationException("Unimplemented method 'setValue'");
}
};

entries[i++] = e;
}
return entries;

}

@Override
public JsonPrimitive getAsJsonPrimitive(String string) {
com.google.gson.JsonPrimitive el= obj().getAsJsonPrimitive(string);
riccardobl marked this conversation as resolved.
Show resolved Hide resolved
return el==null?null:new GsonPrimitive(el);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
14 changes: 14 additions & 0 deletions jme3-plugins-json/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sourceSets {
main {
java {

srcDir 'src/main/java'

}
}
}

dependencies {


}