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 file abstraction improvements #2344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -39,42 +39,49 @@
/**
* GSON implementation of {@link JsonArray}.
*/
class GsonArray extends GsonElement implements JsonArray {
class GsonArray extends GsonElement<com.google.gson.JsonArray> implements JsonArray {

GsonArray(com.google.gson.JsonElement element) {
GsonArray(com.google.gson.JsonArray element) {
super(element);
}

private com.google.gson.JsonArray arr() {
return element.getAsJsonArray();
@Override
public String getAsString() {
return element.getAsString();
}

@Override
public float getAsFloat() {
return element.getAsFloat();
}

@Override
public Iterator<JsonElement> iterator() {
return new Iterator<JsonElement>() {
Iterator<com.google.gson.JsonElement> it = arr().iterator();
public int getAsInt() {
return element.getAsInt();
}

@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public Number getAsNumber() {
return element.getAsNumber();
}

@Override
public JsonElement next() {
return new GsonElement(it.next()).autoCast();
}
};
@Override
public boolean getAsBoolean() {
return element.getAsBoolean();
}

@Override
public Iterator<JsonElement> iterator() {
return GsonUtils.wrap(element.iterator());
}

@Override
public JsonElement get(int i) {
com.google.gson.JsonElement el = arr().get(i);
return isNull(el)?null:new GsonElement(el).autoCast();
return GsonUtils.wrap(element.get(i));
}

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

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -41,13 +41,34 @@
/**
* GSON implementation of {@link JsonElement}
*/
class GsonElement implements JsonElement {
com.google.gson.JsonElement element;
class GsonElement<T extends com.google.gson.JsonElement> implements JsonElement {

final T element;

GsonElement(com.google.gson.JsonElement element) {
GsonElement(T element) {
this.element = element;
}

@Override
public boolean isJsonArray() {
return element.isJsonArray();
}

@Override
public boolean isJsonObject() {
return element.isJsonObject();
}

@Override
public boolean isJsonPrimitive() {
return element.isJsonPrimitive();
}

@Override
public boolean isJsonNull() {
return element.isJsonNull();
}

@Override
public int hashCode() {
return Objects.hashCode(this.element);
Expand All @@ -65,6 +86,7 @@ public boolean equals(Object obj) {
return Objects.equals(this.element, other.element);
}

@Deprecated
protected boolean isNull(com.google.gson.JsonElement element) {
if (element == null) return true;
if (element.isJsonNull()) return true;
Expand All @@ -73,32 +95,32 @@ protected boolean isNull(com.google.gson.JsonElement element) {

@Override
public String getAsString() {
return element.getAsString();
throw new UnsupportedOperationException("Class " + getClass().getSimpleName() + " does not support this option");
}

@Override
public JsonObject getAsJsonObject() {
return new GsonObject(element.getAsJsonObject());
}


@Override
public float getAsFloat() {
return element.getAsFloat();
throw new UnsupportedOperationException("Class " + getClass().getSimpleName() + " does not support this option");
}

@Override
public int getAsInt() {
return element.getAsInt();
throw new UnsupportedOperationException("Class " + getClass().getSimpleName() + " does not support this option");
}

@Override
public Number getAsNumber() {
return element.getAsNumber();
throw new UnsupportedOperationException("Class " + getClass().getSimpleName() + " does not support this option");
}

@Override
public boolean getAsBoolean() {
return element.getAsBoolean();
throw new UnsupportedOperationException("Class " + getClass().getSimpleName() + " does not support this option");
}

@Override
public JsonObject getAsJsonObject() {
return new GsonObject(element.getAsJsonObject());
}

@Override
Expand All @@ -111,6 +133,12 @@ public JsonPrimitive getAsJsonPrimitive() {
return new GsonPrimitive(element.getAsJsonPrimitive());
}

@Override
public String toString() {
return Objects.toString(element);
}

@Deprecated
@SuppressWarnings("unchecked")
public <T extends JsonElement> T autoCast() {
if(isNull(element)) return null;
Expand All @@ -119,5 +147,4 @@ public <T extends JsonElement> T autoCast() {
if (element.isJsonPrimitive()) return (T) new GsonPrimitive(element.getAsJsonPrimitive());
return (T) new GsonElement(element);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2009-2025 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.JsonNull;

/**
* GSON implementation of {@link JsonNull}
* @author wil
*/
class GsonNull extends GsonElement<com.google.gson.JsonNull> implements JsonNull {

/** A single instance for null JSON objects. */
static final GsonNull NULL = new GsonNull(com.google.gson.JsonNull.INSTANCE);

/**
* Class constructor
* @param element JSON element - GSON
*/
private GsonNull(com.google.gson.JsonNull element) {
super(element);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2023 jMonkeyEngine
* Copyright (c) 2009-2025 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -42,72 +42,39 @@
/**
* GSON implementation of {@link JsonObject}
*/
class GsonObject extends GsonElement implements JsonObject {
class GsonObject extends GsonElement<com.google.gson.JsonObject> implements JsonObject {

GsonObject(com.google.gson.JsonObject gsonObject) {
super(gsonObject);
}

private com.google.gson.JsonObject obj() {
return (com.google.gson.JsonObject) element;
@Override
public JsonPrimitive getAsJsonPrimitive(String string) {
return GsonUtils.wrap(element.getAsJsonPrimitive(string));
}

@Override
public JsonArray getAsJsonArray(String string) {
com.google.gson.JsonArray el = obj().getAsJsonArray(string);
return isNull(el) ? null : new GsonArray(el);
return GsonUtils.wrap(element.getAsJsonArray(string));
}

@Override
public JsonObject getAsJsonObject(String string) {
com.google.gson.JsonObject el = obj().getAsJsonObject(string);
return isNull(el) ? null : new GsonObject(el);
return GsonUtils.wrap(element.getAsJsonObject(string));
}

@Override
public boolean has(String string) {
return obj().has(string);
return element.has(string);
}

@Override
public JsonElement get(String string) {
com.google.gson.JsonElement el = obj().get(string);
return isNull(el)?null:new GsonElement(el).autoCast();
return GsonUtils.wrap(element.get(string));
}

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

@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);
public Set<Entry<String, JsonElement>> entrySet() {
return GsonUtils.wrap(element.entrySet());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@
import java.io.InputStreamReader;

import com.jme3.plugins.json.JsonObject;
import com.jme3.plugins.json.JsonElement;
import com.jme3.plugins.json.JsonParser;

/**
* GSON implementation of {@link JsonParser}
*/
public class GsonParser implements JsonParser {

@Override
public JsonElement parseJson(InputStream stream) {
return GsonUtils.wrap(com.google.gson.JsonParser.parseReader(new com.google.gson.stream.JsonReader(new InputStreamReader(stream))));
}

@Override
public JsonObject parse(InputStream stream) {
return new GsonObject(com.google.gson.JsonParser.parseReader(new com.google.gson.stream.JsonReader(new InputStreamReader(stream))).getAsJsonObject());
return parseJson(stream).getAsJsonObject();
}
}
Loading
Loading