Skip to content

Commit 357b1bb

Browse files
committed
Overhaul the usage of json in SceneGraph so that we only need one json library in the CoreNLP distro - remove simple-json
1 parent 7f13d34 commit 357b1bb

6 files changed

+190
-153
lines changed

src/edu/stanford/nlp/scenegraph/image/SceneGraphImage.java

+38-36
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import java.io.BufferedReader;
44
import java.io.IOException;
55
import java.io.Serializable;
6+
import java.io.StringReader;
67
import java.util.List;
78

8-
import org.json.simple.JSONArray;
9-
import org.json.simple.JSONObject;
10-
import org.json.simple.JSONValue;
9+
import javax.json.Json;
10+
import javax.json.JsonArray;
11+
import javax.json.JsonArrayBuilder;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonObjectBuilder;
14+
import javax.json.JsonReader;
15+
import javax.json.JsonValue;
1116

1217
import edu.stanford.nlp.io.IOUtils;
1318
import edu.stanford.nlp.scenegraph.SceneGraphImageCleaner;
@@ -49,39 +54,37 @@ public SceneGraphImage() {
4954
@SuppressWarnings("unchecked")
5055
public static SceneGraphImage readFromJSON(String json) {
5156
try {
52-
SceneGraphImage img = new SceneGraphImage();
57+
StringReader reader = new StringReader(json);
58+
JsonReader parser = Json.createReader(reader);
59+
JsonObject obj = parser.readObject();
5360

54-
JSONObject obj = (JSONObject) JSONValue.parse(json);
61+
SceneGraphImage img = new SceneGraphImage();
5562

56-
JSONArray regions = (JSONArray) obj.get("regions");
63+
JsonArray regions = obj.getJsonArray("regions");
5764
if (regions != null) {
58-
for (JSONObject region : (List<JSONObject>) regions) {
65+
for (JsonObject region : regions.getValuesAs(JsonObject.class)) {
5966
img.regions.add(SceneGraphImageRegion.fromJSONObject(img, region));
6067
}
6168
}
6269

63-
JSONArray objects = (JSONArray) obj.get("objects");
64-
for (JSONObject object: (List<JSONObject>) objects) {
70+
JsonArray objects = obj.getJsonArray("objects");
71+
for (JsonObject object: objects.getValuesAs(JsonObject.class)) {
6572
img.objects.add(SceneGraphImageObject.fromJSONObject(img, object));
6673
}
6774

68-
JSONArray attributes = (JSONArray) obj.get("attributes");
69-
for (JSONObject object: (List<JSONObject>) attributes) {
75+
JsonArray attributes = obj.getJsonArray("attributes");
76+
for (JsonObject object: attributes.getValuesAs(JsonObject.class)) {
7077
img.addAttribute(SceneGraphImageAttribute.fromJSONObject(img, object));
7178
}
7279

73-
JSONArray relationships = (JSONArray) obj.get("relationships");
74-
for (JSONObject relation: (List<JSONObject>) relationships) {
80+
JsonArray relationships = obj.getJsonArray("relationships");
81+
for (JsonObject relation: relationships.getValuesAs(JsonObject.class)) {
7582
img.addRelationship(SceneGraphImageRelationship.fromJSONObject(img, relation));
7683
}
7784

78-
if (obj.get("id") instanceof Number) {
79-
img.id = ((Number) obj.get("id")).intValue();
80-
} else {
81-
img.id = Integer.parseInt(((String) obj.get("id")));
82-
}
83-
Number height = ((Number) obj.get("height"));
84-
Number width = ((Number) obj.get("width"));
85+
img.id = obj.getInt("id");
86+
Number height = obj.getInt("height");
87+
Number width = obj.getInt("width");
8588
if (height == null) {
8689
throw new NullPointerException("Image does not have height");
8790
}
@@ -91,52 +94,51 @@ public static SceneGraphImage readFromJSON(String json) {
9194
img.height = height.intValue();
9295
img.width = width.intValue();
9396

94-
img.url = (String) obj.get("url");
97+
img.url = obj.getString("url");
9598

9699
return img;
97100
} catch (RuntimeException e) {
98-
System.err.println("Couldn't parse " + json);
99101
throw new RuntimeException("Couldn't parse \n" + json, e);
100102
}
101103
}
102104

103105
@SuppressWarnings("unchecked")
104106
public String toJSON() {
105-
JSONObject json = new JSONObject();
106-
json.put("id", this.id);
107-
json.put("height", this.height);
108-
json.put("width", this.width);
109-
json.put("url", this.url);
107+
JsonObjectBuilder json = Json.createObjectBuilder();
108+
json.add("id", this.id);
109+
json.add("height", this.height);
110+
json.add("width", this.width);
111+
json.add("url", this.url);
110112

111-
JSONArray attributes = new JSONArray();
113+
JsonArrayBuilder attributes = Json.createArrayBuilder();
112114
for (SceneGraphImageAttribute attr : this.attributes) {
113115
attributes.add(attr.toJSONObject(this));
114116
}
115117

116-
json.put("attributes", attributes);
118+
json.add("attributes", attributes.build());
117119

118-
JSONArray objects = new JSONArray();
120+
JsonArrayBuilder objects = Json.createArrayBuilder();
119121
for (SceneGraphImageObject obj : this.objects) {
120122
objects.add(obj.toJSONObject(this));
121123
}
122124

123-
json.put("objects", objects);
125+
json.add("objects", objects.build());
124126

125-
JSONArray regions = new JSONArray();
127+
JsonArrayBuilder regions = Json.createArrayBuilder();
126128
for (SceneGraphImageRegion region : this.regions) {
127129
regions.add(region.toJSONObject(this));
128130
}
129131

130-
json.put("regions", regions);
132+
json.add("regions", regions.build());
131133

132-
JSONArray relationships = new JSONArray();
134+
JsonArrayBuilder relationships = Json.createArrayBuilder();
133135
for (SceneGraphImageRelationship relation : this.relationships) {
134136
relationships.add(relation.toJSONObject(this));
135137
}
136138

137-
json.put("relationships", relationships);
139+
json.add("relationships", relationships.build());
138140

139-
return json.toJSONString();
141+
return json.build().toString();
140142
}
141143

142144

src/edu/stanford/nlp/scenegraph/image/SceneGraphImageAttribute.java

+32-27
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import java.io.PrintStream;
44
import java.util.Arrays;
5+
import java.util.ArrayList;
56
import java.util.List;
67

7-
import org.json.simple.JSONArray;
8-
import org.json.simple.JSONObject;
8+
import javax.json.Json;
9+
import javax.json.JsonArray;
10+
import javax.json.JsonArrayBuilder;
11+
import javax.json.JsonObject;
12+
import javax.json.JsonObjectBuilder;
13+
import javax.json.JsonString;
914

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

2732

2833
@SuppressWarnings("unchecked")
29-
public static SceneGraphImageAttribute fromJSONObject(SceneGraphImage img, JSONObject obj) {
34+
public static SceneGraphImageAttribute fromJSONObject(SceneGraphImage img, JsonObject obj) {
3035
SceneGraphImageAttribute attr = new SceneGraphImageAttribute();
3136

3237
attr.image = img;
33-
attr.attribute = (String) obj.get("attribute");
34-
attr.object = (String) obj.get("object");
35-
attr.predicate = (String) obj.get("predicate");
38+
attr.attribute = obj.getString("attribute");
39+
attr.object = obj.getString("object");
40+
attr.predicate = obj.getString("predicate");
3641

3742
if (obj.get("region") != null) {
38-
int regionId = ((Number) obj.get("region")).intValue() - 1;
43+
int regionId = obj.getInt("region") - 1;
3944
attr.region = img.regions.get(regionId);
4045
}
4146

42-
int subjectId = ((Number) obj.get("subject")).intValue();
47+
int subjectId = obj.getInt("subject");
4348
attr.subject = img.objects.get(subjectId);
4449

45-
List<String> textList = (List<String>) obj.get("text");
50+
List<String> textList = SceneGraphImageUtils.getJsonStringList(obj, "text");
4651
attr.text = textList.toArray(new String[textList.size()]);
4752

4853
if (obj.containsKey("attributeGloss")) {
49-
List<String> attributeGlossList = (List<String>) obj.get("attributeGloss");
54+
List<String> attributeGlossList = SceneGraphImageUtils.getJsonStringList(obj, "attributeGloss");
5055
attr.attributeGloss = Generics.newArrayList(attributeGlossList.size());
5156
for (String str : attributeGlossList) {
5257
attr.attributeGloss.add(SceneGraphImageUtils.labelFromString(str));
5358
}
5459
}
5560

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

6772

6873
@SuppressWarnings("unchecked")
69-
public JSONObject toJSONObject(SceneGraphImage img) {
70-
JSONObject obj = new JSONObject();
71-
obj.put("attribute", this.attribute);
72-
obj.put("object", this.object);
73-
obj.put("predicate", this.predicate);
74+
public JsonObject toJSONObject(SceneGraphImage img) {
75+
JsonObjectBuilder obj = Json.createObjectBuilder();
76+
obj.add("attribute", this.attribute);
77+
obj.add("object", this.object);
78+
obj.add("predicate", this.predicate);
7479
if (this.region != null) {
75-
obj.put("region", img.regions.indexOf(this.region) + 1);
80+
obj.add("region", img.regions.indexOf(this.region) + 1);
7681
}
77-
obj.put("subject", img.objects.indexOf(this.subject));
82+
obj.add("subject", img.objects.indexOf(this.subject));
7883

79-
JSONArray text = new JSONArray();
84+
JsonArrayBuilder text = Json.createArrayBuilder();
8085
for (String word : this.text) {
8186
text.add(word);
8287
}
83-
obj.put("text", text);
88+
obj.add("text", text.build());
8489

8590

8691
if (this.attributeGloss != null) {
87-
JSONArray attributeGloss = new JSONArray();
92+
JsonArrayBuilder attributeGloss = Json.createArrayBuilder();
8893
for (CoreLabel lbl : this.attributeGloss) {
8994
attributeGloss.add(SceneGraphImageUtils.labelToString(lbl));
9095
}
91-
obj.put("attributeGloss", attributeGloss);
92-
obj.put("attributeLemmaGloss", attributeLemmaGloss());
96+
obj.add("attributeGloss", attributeGloss.build());
97+
obj.add("attributeLemmaGloss", attributeLemmaGloss());
9398
}
9499

95100
if (this.subjectGloss != null) {
96-
JSONArray subjectGloss = new JSONArray();
101+
JsonArrayBuilder subjectGloss = Json.createArrayBuilder();
97102
for (CoreLabel lbl : this.subjectGloss) {
98103
subjectGloss.add(SceneGraphImageUtils.labelToString(lbl));
99104
}
100-
obj.put("subjectGloss", subjectGloss);
101-
obj.put("subjectLemmaGloss", subjectLemmaGloss());
105+
obj.add("subjectGloss", subjectGloss.build());
106+
obj.add("subjectLemmaGloss", subjectLemmaGloss());
102107
}
103108

104-
return obj;
109+
return obj.build();
105110
}
106111

107112
@Override

src/edu/stanford/nlp/scenegraph/image/SceneGraphImageObject.java

+34-30
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
import java.util.List;
55
import java.util.Set;
66

7-
import org.json.simple.JSONArray;
8-
import org.json.simple.JSONObject;
7+
import javax.json.Json;
8+
import javax.json.JsonArray;
9+
import javax.json.JsonArrayBuilder;
10+
import javax.json.JsonObject;
11+
import javax.json.JsonObjectBuilder;
12+
import javax.json.JsonString;
913

1014
import edu.stanford.nlp.ling.CoreLabel;
1115
import edu.stanford.nlp.util.Generics;
@@ -35,74 +39,74 @@ public SceneGraphImageObject(SceneGraphImageBoundingBox boundingBox, List<String
3539
}
3640

3741
@SuppressWarnings("unchecked")
38-
public static SceneGraphImageObject fromJSONObject(SceneGraphImage img, JSONObject obj) {
42+
public static SceneGraphImageObject fromJSONObject(SceneGraphImage img, JsonObject obj) {
3943

40-
List<String> names = (List<String>) obj.get("names");
41-
List<JSONArray> labelArrays = (List<JSONArray>) obj.get("labels");
44+
List<String> names = SceneGraphImageUtils.getJsonStringList(obj, "names");
45+
JsonArray labelArrays = obj.getJsonArray("labels");
4246
List<List<CoreLabel>> labelsList = null;
4347
if (labelArrays != null) {
4448
labelsList = Generics.newArrayList(labelArrays.size());
45-
for (JSONArray arr : labelArrays) {
49+
for (JsonArray arr : labelArrays.getValuesAs(JsonArray.class)) {
4650
List<CoreLabel> tokens = Generics.newArrayList(arr.size());
47-
for (String str : (List<String>) arr) {
48-
tokens.add(SceneGraphImageUtils.labelFromString(str));
51+
for (JsonString str : arr.getValuesAs(JsonString.class)) {
52+
tokens.add(SceneGraphImageUtils.labelFromString(str.getString()));
4953
}
5054
labelsList.add(tokens);
5155
}
5256
}
53-
JSONObject boundingBoxObj = (JSONObject) obj.get("bbox");
57+
JsonObject boundingBoxObj = obj.getJsonObject("bbox");
5458
if (boundingBoxObj == null) {
5559
throw new NullPointerException("object did not have bbox field");
5660
}
5761

58-
int h = ((Number) boundingBoxObj.get("h")).intValue();
59-
int w = ((Number) boundingBoxObj.get("w")).intValue();
60-
int x = ((Number) boundingBoxObj.get("x")).intValue();
61-
int y = ((Number) boundingBoxObj.get("y")).intValue();
62+
int h = boundingBoxObj.getInt("h");
63+
int w = boundingBoxObj.getInt("w");
64+
int x = boundingBoxObj.getInt("x");
65+
int y = boundingBoxObj.getInt("y");
6266

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

6569
return new SceneGraphImageObject(boundingBox, names, labelsList);
6670
}
6771

6872
@SuppressWarnings("unchecked")
69-
public JSONObject toJSONObject(SceneGraphImage sceneGraphImage) {
70-
JSONObject obj = new JSONObject();
73+
public JsonObject toJSONObject(SceneGraphImage sceneGraphImage) {
74+
JsonObjectBuilder obj = Json.createObjectBuilder();
7175

72-
JSONObject bbox = new JSONObject();
73-
bbox.put("h", this.boundingBox.h);
74-
bbox.put("w", this.boundingBox.w);
75-
bbox.put("x", this.boundingBox.x);
76-
bbox.put("y", this.boundingBox.y);
76+
JsonObjectBuilder bbox = Json.createObjectBuilder();
77+
bbox.add("h", this.boundingBox.h);
78+
bbox.add("w", this.boundingBox.w);
79+
bbox.add("x", this.boundingBox.x);
80+
bbox.add("y", this.boundingBox.y);
7781

78-
obj.put("bbox", bbox);
82+
obj.add("bbox", bbox.build());
7983

80-
JSONArray names = new JSONArray();
84+
JsonArrayBuilder names = Json.createArrayBuilder();
8185
for (String name : this.names) {
8286
names.add(name);
8387
}
8488

85-
obj.put("names", names);
89+
obj.add("names", names.build());
8690

8791

8892
if (this.labels != null && ! this.labels.isEmpty()) {
89-
JSONArray labelsList = new JSONArray();
90-
JSONArray lemmataList = new JSONArray();
93+
JsonArrayBuilder labelsList = Json.createArrayBuilder();
94+
JsonArrayBuilder lemmataList = Json.createArrayBuilder();
9195
for (List<CoreLabel> list : this.labels) {
92-
JSONArray labels = new JSONArray();
96+
JsonArrayBuilder labels = Json.createArrayBuilder();
9397
for (CoreLabel lbl : list) {
9498
labels.add(SceneGraphImageUtils.labelToString(lbl));
9599
}
96-
labelsList.add(labels);
100+
labelsList.add(labels.build());
97101
lemmataList.add(StringUtils.join(list.stream().map(x -> x.lemma() != null ? x.lemma() : x.word()), " "));
98102
}
99-
obj.put("labels", labelsList);
100-
obj.put("lemmata", lemmataList);
103+
obj.add("labels", labelsList.build());
104+
obj.add("lemmata", lemmataList.build());
101105
}
102106

103107

104108

105-
return obj;
109+
return obj.build();
106110
}
107111

108112
public boolean equals(Object other) {

0 commit comments

Comments
 (0)