Skip to content

Commit

Permalink
Overhaul the usage of json in SceneGraph so that we only need one jso…
Browse files Browse the repository at this point in the history
…n library in the CoreNLP distro - remove simple-json
  • Loading branch information
AngledLuffa committed Jul 17, 2023
1 parent 7f13d34 commit 357b1bb
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 153 deletions.
74 changes: 38 additions & 36 deletions src/edu/stanford/nlp/scenegraph/image/SceneGraphImage.java
Expand Up @@ -3,11 +3,16 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Serializable;
import java.io.StringReader;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonReader;
import javax.json.JsonValue;

import edu.stanford.nlp.io.IOUtils;
import edu.stanford.nlp.scenegraph.SceneGraphImageCleaner;
Expand Down Expand Up @@ -49,39 +54,37 @@ public SceneGraphImage() {
@SuppressWarnings("unchecked")
public static SceneGraphImage readFromJSON(String json) {
try {
SceneGraphImage img = new SceneGraphImage();
StringReader reader = new StringReader(json);
JsonReader parser = Json.createReader(reader);
JsonObject obj = parser.readObject();

JSONObject obj = (JSONObject) JSONValue.parse(json);
SceneGraphImage img = new SceneGraphImage();

JSONArray regions = (JSONArray) obj.get("regions");
JsonArray regions = obj.getJsonArray("regions");
if (regions != null) {
for (JSONObject region : (List<JSONObject>) regions) {
for (JsonObject region : regions.getValuesAs(JsonObject.class)) {
img.regions.add(SceneGraphImageRegion.fromJSONObject(img, region));
}
}

JSONArray objects = (JSONArray) obj.get("objects");
for (JSONObject object: (List<JSONObject>) objects) {
JsonArray objects = obj.getJsonArray("objects");
for (JsonObject object: objects.getValuesAs(JsonObject.class)) {
img.objects.add(SceneGraphImageObject.fromJSONObject(img, object));
}

JSONArray attributes = (JSONArray) obj.get("attributes");
for (JSONObject object: (List<JSONObject>) attributes) {
JsonArray attributes = obj.getJsonArray("attributes");
for (JsonObject object: attributes.getValuesAs(JsonObject.class)) {
img.addAttribute(SceneGraphImageAttribute.fromJSONObject(img, object));
}

JSONArray relationships = (JSONArray) obj.get("relationships");
for (JSONObject relation: (List<JSONObject>) relationships) {
JsonArray relationships = obj.getJsonArray("relationships");
for (JsonObject relation: relationships.getValuesAs(JsonObject.class)) {
img.addRelationship(SceneGraphImageRelationship.fromJSONObject(img, relation));
}

if (obj.get("id") instanceof Number) {
img.id = ((Number) obj.get("id")).intValue();
} else {
img.id = Integer.parseInt(((String) obj.get("id")));
}
Number height = ((Number) obj.get("height"));
Number width = ((Number) obj.get("width"));
img.id = obj.getInt("id");
Number height = obj.getInt("height");
Number width = obj.getInt("width");
if (height == null) {
throw new NullPointerException("Image does not have height");
}
Expand All @@ -91,52 +94,51 @@ public static SceneGraphImage readFromJSON(String json) {
img.height = height.intValue();
img.width = width.intValue();

img.url = (String) obj.get("url");
img.url = obj.getString("url");

return img;
} catch (RuntimeException e) {
System.err.println("Couldn't parse " + json);
throw new RuntimeException("Couldn't parse \n" + json, e);
}
}

@SuppressWarnings("unchecked")
public String toJSON() {
JSONObject json = new JSONObject();
json.put("id", this.id);
json.put("height", this.height);
json.put("width", this.width);
json.put("url", this.url);
JsonObjectBuilder json = Json.createObjectBuilder();
json.add("id", this.id);
json.add("height", this.height);
json.add("width", this.width);
json.add("url", this.url);

JSONArray attributes = new JSONArray();
JsonArrayBuilder attributes = Json.createArrayBuilder();
for (SceneGraphImageAttribute attr : this.attributes) {
attributes.add(attr.toJSONObject(this));
}

json.put("attributes", attributes);
json.add("attributes", attributes.build());

JSONArray objects = new JSONArray();
JsonArrayBuilder objects = Json.createArrayBuilder();
for (SceneGraphImageObject obj : this.objects) {
objects.add(obj.toJSONObject(this));
}

json.put("objects", objects);
json.add("objects", objects.build());

JSONArray regions = new JSONArray();
JsonArrayBuilder regions = Json.createArrayBuilder();
for (SceneGraphImageRegion region : this.regions) {
regions.add(region.toJSONObject(this));
}

json.put("regions", regions);
json.add("regions", regions.build());

JSONArray relationships = new JSONArray();
JsonArrayBuilder relationships = Json.createArrayBuilder();
for (SceneGraphImageRelationship relation : this.relationships) {
relationships.add(relation.toJSONObject(this));
}

json.put("relationships", relationships);
json.add("relationships", relationships.build());

return json.toJSONString();
return json.build().toString();
}


Expand Down
59 changes: 32 additions & 27 deletions src/edu/stanford/nlp/scenegraph/image/SceneGraphImageAttribute.java
Expand Up @@ -2,10 +2,15 @@

import java.io.PrintStream;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.util.Generics;
Expand All @@ -26,35 +31,35 @@ public class SceneGraphImageAttribute {


@SuppressWarnings("unchecked")
public static SceneGraphImageAttribute fromJSONObject(SceneGraphImage img, JSONObject obj) {
public static SceneGraphImageAttribute fromJSONObject(SceneGraphImage img, JsonObject obj) {
SceneGraphImageAttribute attr = new SceneGraphImageAttribute();

attr.image = img;
attr.attribute = (String) obj.get("attribute");
attr.object = (String) obj.get("object");
attr.predicate = (String) obj.get("predicate");
attr.attribute = obj.getString("attribute");
attr.object = obj.getString("object");
attr.predicate = obj.getString("predicate");

if (obj.get("region") != null) {
int regionId = ((Number) obj.get("region")).intValue() - 1;
int regionId = obj.getInt("region") - 1;
attr.region = img.regions.get(regionId);
}

int subjectId = ((Number) obj.get("subject")).intValue();
int subjectId = obj.getInt("subject");
attr.subject = img.objects.get(subjectId);

List<String> textList = (List<String>) obj.get("text");
List<String> textList = SceneGraphImageUtils.getJsonStringList(obj, "text");
attr.text = textList.toArray(new String[textList.size()]);

if (obj.containsKey("attributeGloss")) {
List<String> attributeGlossList = (List<String>) obj.get("attributeGloss");
List<String> attributeGlossList = SceneGraphImageUtils.getJsonStringList(obj, "attributeGloss");
attr.attributeGloss = Generics.newArrayList(attributeGlossList.size());
for (String str : attributeGlossList) {
attr.attributeGloss.add(SceneGraphImageUtils.labelFromString(str));
}
}

if (obj.containsKey("subjectGloss")) {
List<String> subjectGlossList = (List<String>) obj.get("subjectGloss");
List<String> subjectGlossList = SceneGraphImageUtils.getJsonStringList(obj, "subjectGloss");
attr.subjectGloss = Generics.newArrayList(subjectGlossList.size());
for (String str : subjectGlossList) {
attr.subjectGloss.add(SceneGraphImageUtils.labelFromString(str));
Expand All @@ -66,42 +71,42 @@ public static SceneGraphImageAttribute fromJSONObject(SceneGraphImage img, JSONO


@SuppressWarnings("unchecked")
public JSONObject toJSONObject(SceneGraphImage img) {
JSONObject obj = new JSONObject();
obj.put("attribute", this.attribute);
obj.put("object", this.object);
obj.put("predicate", this.predicate);
public JsonObject toJSONObject(SceneGraphImage img) {
JsonObjectBuilder obj = Json.createObjectBuilder();
obj.add("attribute", this.attribute);
obj.add("object", this.object);
obj.add("predicate", this.predicate);
if (this.region != null) {
obj.put("region", img.regions.indexOf(this.region) + 1);
obj.add("region", img.regions.indexOf(this.region) + 1);
}
obj.put("subject", img.objects.indexOf(this.subject));
obj.add("subject", img.objects.indexOf(this.subject));

JSONArray text = new JSONArray();
JsonArrayBuilder text = Json.createArrayBuilder();
for (String word : this.text) {
text.add(word);
}
obj.put("text", text);
obj.add("text", text.build());


if (this.attributeGloss != null) {
JSONArray attributeGloss = new JSONArray();
JsonArrayBuilder attributeGloss = Json.createArrayBuilder();
for (CoreLabel lbl : this.attributeGloss) {
attributeGloss.add(SceneGraphImageUtils.labelToString(lbl));
}
obj.put("attributeGloss", attributeGloss);
obj.put("attributeLemmaGloss", attributeLemmaGloss());
obj.add("attributeGloss", attributeGloss.build());
obj.add("attributeLemmaGloss", attributeLemmaGloss());
}

if (this.subjectGloss != null) {
JSONArray subjectGloss = new JSONArray();
JsonArrayBuilder subjectGloss = Json.createArrayBuilder();
for (CoreLabel lbl : this.subjectGloss) {
subjectGloss.add(SceneGraphImageUtils.labelToString(lbl));
}
obj.put("subjectGloss", subjectGloss);
obj.put("subjectLemmaGloss", subjectLemmaGloss());
obj.add("subjectGloss", subjectGloss.build());
obj.add("subjectLemmaGloss", subjectLemmaGloss());
}

return obj;
return obj.build();
}

@Override
Expand Down
64 changes: 34 additions & 30 deletions src/edu/stanford/nlp/scenegraph/image/SceneGraphImageObject.java
Expand Up @@ -4,8 +4,12 @@
import java.util.List;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonString;

import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.util.Generics;
Expand Down Expand Up @@ -35,74 +39,74 @@ public SceneGraphImageObject(SceneGraphImageBoundingBox boundingBox, List<String
}

@SuppressWarnings("unchecked")
public static SceneGraphImageObject fromJSONObject(SceneGraphImage img, JSONObject obj) {
public static SceneGraphImageObject fromJSONObject(SceneGraphImage img, JsonObject obj) {

List<String> names = (List<String>) obj.get("names");
List<JSONArray> labelArrays = (List<JSONArray>) obj.get("labels");
List<String> names = SceneGraphImageUtils.getJsonStringList(obj, "names");
JsonArray labelArrays = obj.getJsonArray("labels");
List<List<CoreLabel>> labelsList = null;
if (labelArrays != null) {
labelsList = Generics.newArrayList(labelArrays.size());
for (JSONArray arr : labelArrays) {
for (JsonArray arr : labelArrays.getValuesAs(JsonArray.class)) {
List<CoreLabel> tokens = Generics.newArrayList(arr.size());
for (String str : (List<String>) arr) {
tokens.add(SceneGraphImageUtils.labelFromString(str));
for (JsonString str : arr.getValuesAs(JsonString.class)) {
tokens.add(SceneGraphImageUtils.labelFromString(str.getString()));
}
labelsList.add(tokens);
}
}
JSONObject boundingBoxObj = (JSONObject) obj.get("bbox");
JsonObject boundingBoxObj = obj.getJsonObject("bbox");
if (boundingBoxObj == null) {
throw new NullPointerException("object did not have bbox field");
}

int h = ((Number) boundingBoxObj.get("h")).intValue();
int w = ((Number) boundingBoxObj.get("w")).intValue();
int x = ((Number) boundingBoxObj.get("x")).intValue();
int y = ((Number) boundingBoxObj.get("y")).intValue();
int h = boundingBoxObj.getInt("h");
int w = boundingBoxObj.getInt("w");
int x = boundingBoxObj.getInt("x");
int y = boundingBoxObj.getInt("y");

SceneGraphImageBoundingBox boundingBox = new SceneGraphImageBoundingBox(h, w, x, y);

return new SceneGraphImageObject(boundingBox, names, labelsList);
}

@SuppressWarnings("unchecked")
public JSONObject toJSONObject(SceneGraphImage sceneGraphImage) {
JSONObject obj = new JSONObject();
public JsonObject toJSONObject(SceneGraphImage sceneGraphImage) {
JsonObjectBuilder obj = Json.createObjectBuilder();

JSONObject bbox = new JSONObject();
bbox.put("h", this.boundingBox.h);
bbox.put("w", this.boundingBox.w);
bbox.put("x", this.boundingBox.x);
bbox.put("y", this.boundingBox.y);
JsonObjectBuilder bbox = Json.createObjectBuilder();
bbox.add("h", this.boundingBox.h);
bbox.add("w", this.boundingBox.w);
bbox.add("x", this.boundingBox.x);
bbox.add("y", this.boundingBox.y);

obj.put("bbox", bbox);
obj.add("bbox", bbox.build());

JSONArray names = new JSONArray();
JsonArrayBuilder names = Json.createArrayBuilder();
for (String name : this.names) {
names.add(name);
}

obj.put("names", names);
obj.add("names", names.build());


if (this.labels != null && ! this.labels.isEmpty()) {
JSONArray labelsList = new JSONArray();
JSONArray lemmataList = new JSONArray();
JsonArrayBuilder labelsList = Json.createArrayBuilder();
JsonArrayBuilder lemmataList = Json.createArrayBuilder();
for (List<CoreLabel> list : this.labels) {
JSONArray labels = new JSONArray();
JsonArrayBuilder labels = Json.createArrayBuilder();
for (CoreLabel lbl : list) {
labels.add(SceneGraphImageUtils.labelToString(lbl));
}
labelsList.add(labels);
labelsList.add(labels.build());
lemmataList.add(StringUtils.join(list.stream().map(x -> x.lemma() != null ? x.lemma() : x.word()), " "));
}
obj.put("labels", labelsList);
obj.put("lemmata", lemmataList);
obj.add("labels", labelsList.build());
obj.add("lemmata", lemmataList.build());
}



return obj;
return obj.build();
}

public boolean equals(Object other) {
Expand Down

0 comments on commit 357b1bb

Please sign in to comment.