Skip to content

Commit 334687d

Browse files
committed
Need to remove the simple-json use from SceneGraph (not the /image version) as well
1 parent cc7983e commit 334687d

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

itest/src/edu/stanford/nlp/scenegraph/RuleBasedParserITest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package edu.stanford.nlp.scenegraph;
22

3+
import java.io.StringReader;
4+
35
import java.util.Arrays;
46
import java.util.Collections;
57
import java.util.HashSet;
68
import java.util.List;
79
import java.util.Set;
810

11+
import javax.json.Json;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonReader;
14+
915
import org.junit.Test;
1016
import org.junit.AfterClass;
1117
import org.junit.BeforeClass;
@@ -125,8 +131,22 @@ public void testJson() {
125131
String text = "A smiling man is riding a horse.";
126132
SceneGraph scene = parser.parse(text);
127133

128-
String expectedJSON = "{\"relationships\":[{\"predicate\":\"ride\",\"subject\":0,\"text\":[\"man\",\"ride\",\"horse\"],\"object\":1}],\"phrase\":\"A smiling man is riding a horse.\",\"objects\":[{\"names\":[\"man\"]},{\"names\":[\"horse\"]}],\"attributes\":[{\"predicate\":\"is\",\"subject\":0,\"attribute\":\"smile\",\"text\":[\"man\",\"is\",\"smile\"],\"object\":\"smile\"}],\"id\":1,\"url\":\"www.stanford.edu\"}";
129-
assertEquals(expectedJSON, scene.toJSON(1, "www.stanford.edu", text));
134+
// This was how the simple-json library output the json
135+
// Actually, since json is order-free, we need to process the
136+
// expected results and the actual results back into maps and
137+
// compare those maps
138+
String expectedJSONtext = "{\"relationships\":[{\"predicate\":\"ride\",\"subject\":0,\"text\":[\"man\",\"ride\",\"horse\"],\"object\":1}],\"phrase\":\"A smiling man is riding a horse.\",\"objects\":[{\"names\":[\"man\"]},{\"names\":[\"horse\"]}],\"attributes\":[{\"predicate\":\"is\",\"subject\":0,\"attribute\":\"smile\",\"text\":[\"man\",\"is\",\"smile\"],\"object\":\"smile\"}],\"id\":1,\"url\":\"www.stanford.edu\"}";
139+
140+
StringReader reader = new StringReader(expectedJSONtext);
141+
JsonReader parser = Json.createReader(reader);
142+
JsonObject expectedJSON = parser.readObject();
143+
144+
String convertedText = scene.toJSON(1, "www.stanford.edu", text);
145+
reader = new StringReader(convertedText);
146+
parser = Json.createReader(reader);
147+
JsonObject converted = parser.readObject();
148+
149+
assertEquals(expectedJSON, converted);
130150

131151
// The json for the nodes is just the word of the node
132152
List<String> expectedNodes = Arrays.asList("man", "horse");

src/edu/stanford/nlp/scenegraph/SceneGraph.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
import org.json.simple.JSONArray;
9-
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;
1013

1114
import edu.stanford.nlp.graph.DirectedMultiGraph;
1215
import edu.stanford.nlp.ling.IndexedWord;
@@ -119,63 +122,63 @@ public SceneGraphNode getOrAddNode(IndexedWord value) {
119122

120123
@SuppressWarnings("unchecked")
121124
public String toJSON(int imageID, String url, String phrase) {
122-
JSONObject obj = new JSONObject();
123-
obj.put("id", imageID);
124-
obj.put("url", url);
125-
obj.put("phrase", phrase);
125+
JsonObjectBuilder obj = Json.createObjectBuilder();
126+
obj.add("id", imageID);
127+
obj.add("url", url);
128+
obj.add("phrase", phrase);
126129

127130
List<SceneGraphNode> objects = this.nodeListSorted();
128131

129-
JSONArray attrs = new JSONArray();
132+
JsonArrayBuilder attrs = Json.createArrayBuilder();
130133
for (SceneGraphNode node : objects) {
131134
for (SceneGraphAttribute attr : node.getAttributes()) {
132-
JSONObject attrObj = new JSONObject();
133-
attrObj.put("attribute", attr.toString());
134-
attrObj.put("object", attr.toString());
135-
attrObj.put("predicate", "is");
136-
attrObj.put("subject", objects.indexOf(node));
137-
JSONArray text = new JSONArray();
135+
JsonObjectBuilder attrObj = Json.createObjectBuilder();
136+
attrObj.add("attribute", attr.toString());
137+
attrObj.add("object", attr.toString());
138+
attrObj.add("predicate", "is");
139+
attrObj.add("subject", objects.indexOf(node));
140+
JsonArrayBuilder text = Json.createArrayBuilder();
138141
text.add(node.toJSONString());
139142
text.add("is");
140143
text.add(attr.toString());
141-
attrObj.put("text", text);
142-
attrs.add(attrObj);
144+
attrObj.add("text", text.build());
145+
attrs.add(attrObj.build());
143146
}
144147
}
145148

146-
obj.put("attributes", attrs);
149+
obj.add("attributes", attrs.build());
147150

148-
JSONArray relns = new JSONArray();
151+
JsonArrayBuilder relns = Json.createArrayBuilder();
149152

150153
for (SceneGraphRelation reln : this.relationListSorted()) {
151-
JSONObject relnObj = new JSONObject();
152-
relnObj.put("predicate", reln.getRelation());
153-
relnObj.put("subject", objects.indexOf(reln.getSource()));
154-
relnObj.put("object", objects.indexOf(reln.getTarget()));
155-
JSONArray text = new JSONArray();
154+
JsonObjectBuilder relnObj = Json.createObjectBuilder();
155+
relnObj.add("predicate", reln.getRelation());
156+
relnObj.add("subject", objects.indexOf(reln.getSource()));
157+
relnObj.add("object", objects.indexOf(reln.getTarget()));
158+
JsonArrayBuilder text = Json.createArrayBuilder();
156159
text.add(reln.getSource().toJSONString());
157160
text.add(reln.getRelation());
158161
text.add(reln.getTarget().toJSONString());
159-
relnObj.put("text", text);
160-
relns.add(relnObj);
162+
relnObj.add("text", text.build());
163+
relns.add(relnObj.build());
161164
}
162165

163-
obj.put("relationships", relns);
166+
obj.add("relationships", relns.build());
164167

165168

166-
JSONArray objs = new JSONArray();
169+
JsonArrayBuilder objs = Json.createArrayBuilder();
167170
for (SceneGraphNode node : objects) {
168-
JSONObject objObj = new JSONObject();
169-
JSONArray names = new JSONArray();
171+
JsonObjectBuilder objObj = Json.createObjectBuilder();
172+
JsonArrayBuilder names = Json.createArrayBuilder();
170173
names.add(node.toJSONString());
171-
objObj.put("names", names);
172-
objs.add(objObj);
174+
objObj.add("names", names.build());
175+
objs.add(objObj.build());
173176
}
174177

175-
obj.put("objects", objs);
178+
obj.add("objects", objs.build());
176179

177180

178-
return obj.toJSONString();
181+
return obj.build().toString();
179182
}
180183

181184

0 commit comments

Comments
 (0)