3
3
import java .io .BufferedReader ;
4
4
import java .io .IOException ;
5
5
import java .io .Serializable ;
6
+ import java .io .StringReader ;
6
7
import java .util .List ;
7
8
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 ;
11
16
12
17
import edu .stanford .nlp .io .IOUtils ;
13
18
import edu .stanford .nlp .scenegraph .SceneGraphImageCleaner ;
@@ -49,39 +54,37 @@ public SceneGraphImage() {
49
54
@ SuppressWarnings ("unchecked" )
50
55
public static SceneGraphImage readFromJSON (String json ) {
51
56
try {
52
- SceneGraphImage img = new SceneGraphImage ();
57
+ StringReader reader = new StringReader (json );
58
+ JsonReader parser = Json .createReader (reader );
59
+ JsonObject obj = parser .readObject ();
53
60
54
- JSONObject obj = ( JSONObject ) JSONValue . parse ( json );
61
+ SceneGraphImage img = new SceneGraphImage ( );
55
62
56
- JSONArray regions = ( JSONArray ) obj .get ("regions" );
63
+ JsonArray regions = obj .getJsonArray ("regions" );
57
64
if (regions != null ) {
58
- for (JSONObject region : ( List < JSONObject >) regions ) {
65
+ for (JsonObject region : regions . getValuesAs ( JsonObject . class ) ) {
59
66
img .regions .add (SceneGraphImageRegion .fromJSONObject (img , region ));
60
67
}
61
68
}
62
69
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 ) ) {
65
72
img .objects .add (SceneGraphImageObject .fromJSONObject (img , object ));
66
73
}
67
74
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 ) ) {
70
77
img .addAttribute (SceneGraphImageAttribute .fromJSONObject (img , object ));
71
78
}
72
79
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 ) ) {
75
82
img .addRelationship (SceneGraphImageRelationship .fromJSONObject (img , relation ));
76
83
}
77
84
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" );
85
88
if (height == null ) {
86
89
throw new NullPointerException ("Image does not have height" );
87
90
}
@@ -91,52 +94,51 @@ public static SceneGraphImage readFromJSON(String json) {
91
94
img .height = height .intValue ();
92
95
img .width = width .intValue ();
93
96
94
- img .url = ( String ) obj .get ("url" );
97
+ img .url = obj .getString ("url" );
95
98
96
99
return img ;
97
100
} catch (RuntimeException e ) {
98
- System .err .println ("Couldn't parse " + json );
99
101
throw new RuntimeException ("Couldn't parse \n " + json , e );
100
102
}
101
103
}
102
104
103
105
@ SuppressWarnings ("unchecked" )
104
106
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 );
110
112
111
- JSONArray attributes = new JSONArray ();
113
+ JsonArrayBuilder attributes = Json . createArrayBuilder ();
112
114
for (SceneGraphImageAttribute attr : this .attributes ) {
113
115
attributes .add (attr .toJSONObject (this ));
114
116
}
115
117
116
- json .put ("attributes" , attributes );
118
+ json .add ("attributes" , attributes . build () );
117
119
118
- JSONArray objects = new JSONArray ();
120
+ JsonArrayBuilder objects = Json . createArrayBuilder ();
119
121
for (SceneGraphImageObject obj : this .objects ) {
120
122
objects .add (obj .toJSONObject (this ));
121
123
}
122
124
123
- json .put ("objects" , objects );
125
+ json .add ("objects" , objects . build () );
124
126
125
- JSONArray regions = new JSONArray ();
127
+ JsonArrayBuilder regions = Json . createArrayBuilder ();
126
128
for (SceneGraphImageRegion region : this .regions ) {
127
129
regions .add (region .toJSONObject (this ));
128
130
}
129
131
130
- json .put ("regions" , regions );
132
+ json .add ("regions" , regions . build () );
131
133
132
- JSONArray relationships = new JSONArray ();
134
+ JsonArrayBuilder relationships = Json . createArrayBuilder ();
133
135
for (SceneGraphImageRelationship relation : this .relationships ) {
134
136
relationships .add (relation .toJSONObject (this ));
135
137
}
136
138
137
- json .put ("relationships" , relationships );
139
+ json .add ("relationships" , relationships . build () );
138
140
139
- return json .toJSONString ();
141
+ return json .build (). toString ();
140
142
}
141
143
142
144
0 commit comments