20
20
package org .elasticsearch .common .geo ;
21
21
22
22
import org .elasticsearch .ElasticsearchParseException ;
23
+ import org .elasticsearch .common .Strings ;
24
+ import org .elasticsearch .common .xcontent .ToXContent ;
23
25
import org .elasticsearch .common .xcontent .XContentBuilder ;
24
26
import org .elasticsearch .common .xcontent .XContentFactory ;
25
27
import org .elasticsearch .common .xcontent .XContentParseException ;
@@ -44,7 +46,11 @@ public void testGeoJsonParsing() throws Exception {
44
46
45
47
try (XContentParser parser = createParser (pointGeoJson )) {
46
48
parser .nextToken ();
47
- assertEquals (new Point (0 , 100 ), new GeometryParser (true , randomBoolean (), randomBoolean ()).parse (parser ));
49
+ GeometryFormat format = new GeometryParser (true , randomBoolean (), randomBoolean ()).geometryFormat (parser );
50
+ assertEquals (new Point (0 , 100 ), format .fromXContent (parser ));
51
+ XContentBuilder newGeoJson = XContentFactory .jsonBuilder ();
52
+ format .toXContent (new Point (10 , 100 ), newGeoJson , ToXContent .EMPTY_PARAMS );
53
+ assertEquals ("{\" type\" :\" Point\" ,\" coordinates\" :[100.0,10.0]}" , Strings .toString (newGeoJson ));
48
54
}
49
55
50
56
XContentBuilder pointGeoJsonWithZ = XContentFactory .jsonBuilder ()
@@ -77,7 +83,7 @@ public void testGeoJsonParsing() throws Exception {
77
83
.endArray ()
78
84
.endObject ();
79
85
80
- Polygon p = new Polygon (new LinearRing (new double [] {1d , 1d , 0d , 0d , 1d }, new double [] {100d , 101d , 101d , 100d , 100d }));
86
+ Polygon p = new Polygon (new LinearRing (new double []{1d , 1d , 0d , 0d , 1d }, new double []{100d , 101d , 101d , 100d , 100d }));
81
87
try (XContentParser parser = createParser (polygonGeoJson )) {
82
88
parser .nextToken ();
83
89
// Coerce should automatically close the polygon
@@ -101,7 +107,12 @@ public void testWKTParsing() throws Exception {
101
107
parser .nextToken (); // Start object
102
108
parser .nextToken (); // Field Name
103
109
parser .nextToken (); // Field Value
104
- assertEquals (new Point (0 , 100 ), new GeometryParser (true , randomBoolean (), randomBoolean ()).parse (parser ));
110
+ GeometryFormat format = new GeometryParser (true , randomBoolean (), randomBoolean ()).geometryFormat (parser );
111
+ assertEquals (new Point (0 , 100 ), format .fromXContent (parser ));
112
+ XContentBuilder newGeoJson = XContentFactory .jsonBuilder ().startObject ().field ("val" );
113
+ format .toXContent (new Point (10 , 100 ), newGeoJson , ToXContent .EMPTY_PARAMS );
114
+ newGeoJson .endObject ();
115
+ assertEquals ("{\" val\" :\" point (100.0 10.0)\" }" , Strings .toString (newGeoJson ));
105
116
}
106
117
}
107
118
@@ -115,7 +126,20 @@ public void testNullParsing() throws Exception {
115
126
parser .nextToken (); // Start object
116
127
parser .nextToken (); // Field Name
117
128
parser .nextToken (); // Field Value
118
- assertNull (new GeometryParser (true , randomBoolean (), randomBoolean ()).parse (parser ));
129
+ GeometryFormat format = new GeometryParser (true , randomBoolean (), randomBoolean ()).geometryFormat (parser );
130
+ assertNull (format .fromXContent (parser ));
131
+
132
+ XContentBuilder newGeoJson = XContentFactory .jsonBuilder ().startObject ().field ("val" );
133
+ // if we serialize non-null value - it should be serialized as geojson
134
+ format .toXContent (new Point (10 , 100 ), newGeoJson , ToXContent .EMPTY_PARAMS );
135
+ newGeoJson .endObject ();
136
+ assertEquals ("{\" val\" :{\" type\" :\" Point\" ,\" coordinates\" :[100.0,10.0]}}" , Strings .toString (newGeoJson ));
137
+
138
+ newGeoJson = XContentFactory .jsonBuilder ().startObject ().field ("val" );
139
+ format .toXContent (null , newGeoJson , ToXContent .EMPTY_PARAMS );
140
+ newGeoJson .endObject ();
141
+ assertEquals ("{\" val\" :null}" , Strings .toString (newGeoJson ));
142
+
119
143
}
120
144
}
121
145
0 commit comments