2020package org .elasticsearch .common .geo ;
2121
2222import org .elasticsearch .ElasticsearchParseException ;
23+ import org .elasticsearch .common .Strings ;
24+ import org .elasticsearch .common .xcontent .ToXContent ;
2325import org .elasticsearch .common .xcontent .XContentBuilder ;
2426import org .elasticsearch .common .xcontent .XContentFactory ;
2527import org .elasticsearch .common .xcontent .XContentParseException ;
@@ -44,7 +46,11 @@ public void testGeoJsonParsing() throws Exception {
4446
4547 try (XContentParser parser = createParser (pointGeoJson )) {
4648 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 ));
4854 }
4955
5056 XContentBuilder pointGeoJsonWithZ = XContentFactory .jsonBuilder ()
@@ -77,7 +83,7 @@ public void testGeoJsonParsing() throws Exception {
7783 .endArray ()
7884 .endObject ();
7985
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 }));
8187 try (XContentParser parser = createParser (polygonGeoJson )) {
8288 parser .nextToken ();
8389 // Coerce should automatically close the polygon
@@ -101,7 +107,12 @@ public void testWKTParsing() throws Exception {
101107 parser .nextToken (); // Start object
102108 parser .nextToken (); // Field Name
103109 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 ));
105116 }
106117 }
107118
@@ -115,7 +126,20 @@ public void testNullParsing() throws Exception {
115126 parser .nextToken (); // Start object
116127 parser .nextToken (); // Field Name
117128 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+
119143 }
120144 }
121145
0 commit comments