use of org.opensearch.common.xcontent.support.MapXContentParser in project OpenSearch by opensearch-project.
the class GeoUtils method parseGeoPoint.
/**
* Parses the value as a geopoint. The following types of values are supported:
* <p>
* Object: has to contain either lat and lon or geohash fields
* <p>
* String: expected to be in "latitude, longitude" format or a geohash
* <p>
* Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
*/
public static GeoPoint parseGeoPoint(Object value, GeoPoint point, final boolean ignoreZValue) throws OpenSearchParseException {
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("null_value", value), null)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
return parseGeoPoint(parser, point, ignoreZValue);
} catch (IOException ex) {
throw new OpenSearchParseException("error parsing geopoint", ex);
}
}
use of org.opensearch.common.xcontent.support.MapXContentParser in project OpenSearch by opensearch-project.
the class ShapeParser method parse.
static ShapeBuilder parse(Object value) throws IOException {
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("value", value), null)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
return parse(parser);
}
}
use of org.opensearch.common.xcontent.support.MapXContentParser in project OpenSearch by opensearch-project.
the class GeoShapeParser method parseAndFormatObject.
@Override
public Object parseAndFormatObject(Object value, String format) {
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("dummy_field", value), XContentType.JSON)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
GeometryFormat<Geometry> geometryFormat = geometryParser.geometryFormat(parser);
if (geometryFormat.name().equals(format)) {
return value;
}
Geometry geometry = geometryFormat.fromXContent(parser);
return format(geometry, format);
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
use of org.opensearch.common.xcontent.support.MapXContentParser in project OpenSearch by opensearch-project.
the class MapXContentParserTests method compareTokens.
public void compareTokens(CheckedConsumer<XContentBuilder, IOException> consumer, XContentType xContentType) throws IOException {
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
consumer.accept(builder);
final Map<String, Object> map;
try (XContentParser parser = createParser(xContentType.xContent(), BytesReference.bytes(builder))) {
map = parser.mapOrdered();
}
try (XContentParser parser = createParser(xContentType.xContent(), BytesReference.bytes(builder))) {
try (XContentParser mapParser = new MapXContentParser(xContentRegistry(), LoggingDeprecationHandler.INSTANCE, map, xContentType)) {
assertEquals(parser.contentType(), mapParser.contentType());
XContentParser.Token token;
assertEquals(parser.currentToken(), mapParser.currentToken());
assertEquals(parser.currentName(), mapParser.currentName());
do {
token = parser.nextToken();
XContentParser.Token mapToken = mapParser.nextToken();
assertEquals(token, mapToken);
assertEquals(parser.currentName(), mapParser.currentName());
if (token != null && (token.isValue() || token == XContentParser.Token.VALUE_NULL)) {
if (xContentType != XContentType.YAML || token != XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
// YAML struggles with converting byte arrays into text, because it
// does weird base64 decoding to the values. We don't do this
// weirdness in the MapXContentParser, so don't try to stringify it.
// The .binaryValue() comparison below still works correctly though.
assertEquals(parser.textOrNull(), mapParser.textOrNull());
}
switch(token) {
case VALUE_STRING:
assertEquals(parser.text(), mapParser.text());
break;
case VALUE_NUMBER:
assertEquals(parser.numberType(), mapParser.numberType());
assertEquals(parser.numberValue(), mapParser.numberValue());
if (parser.numberType() == XContentParser.NumberType.LONG || parser.numberType() == XContentParser.NumberType.INT) {
assertEquals(parser.longValue(), mapParser.longValue());
if (parser.longValue() <= Integer.MAX_VALUE && parser.longValue() >= Integer.MIN_VALUE) {
assertEquals(parser.intValue(), mapParser.intValue());
if (parser.longValue() <= Short.MAX_VALUE && parser.longValue() >= Short.MIN_VALUE) {
assertEquals(parser.shortValue(), mapParser.shortValue());
}
}
} else {
assertEquals(parser.doubleValue(), mapParser.doubleValue(), 0.000001);
}
break;
case VALUE_BOOLEAN:
assertEquals(parser.booleanValue(), mapParser.booleanValue());
break;
case VALUE_EMBEDDED_OBJECT:
assertArrayEquals(parser.binaryValue(), mapParser.binaryValue());
break;
case VALUE_NULL:
assertNull(mapParser.textOrNull());
break;
}
assertEquals(parser.currentName(), mapParser.currentName());
assertEquals(parser.isClosed(), mapParser.isClosed());
} else if (token == XContentParser.Token.START_ARRAY || token == XContentParser.Token.START_OBJECT) {
if (randomInt(5) == 0) {
parser.skipChildren();
mapParser.skipChildren();
}
}
} while (token != null);
assertEquals(parser.nextToken(), mapParser.nextToken());
parser.close();
mapParser.close();
assertEquals(parser.isClosed(), mapParser.isClosed());
assertTrue(mapParser.isClosed());
}
}
}
}
use of org.opensearch.common.xcontent.support.MapXContentParser in project OpenSearch by opensearch-project.
the class GeometryParser method parseGeometry.
/**
* Parses the value as a {@link Geometry}. The following types of values are supported:
* <p>
* Object: has to contain either lat and lon or geohash fields
* <p>
* String: expected to be in "latitude, longitude" format, a geohash or WKT
* <p>
* Array: two or more elements, the first element is longitude, the second is latitude, the rest is ignored if ignoreZValue is true
* <p>
* Json structure: valid geojson definition
*/
public Geometry parseGeometry(Object value) throws OpenSearchParseException {
if (value instanceof List) {
List<?> values = (List<?>) value;
if (values.size() == 2 && values.get(0) instanceof Number) {
GeoPoint point = GeoUtils.parseGeoPoint(values, ignoreZValue);
return new Point(point.lon(), point.lat());
} else {
List<Geometry> geometries = new ArrayList<>(values.size());
for (Object object : values) {
geometries.add(parseGeometry(object));
}
return new GeometryCollection<>(geometries);
}
}
try (XContentParser parser = new MapXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, Collections.singletonMap("null_value", value), null)) {
// start object
parser.nextToken();
// field name
parser.nextToken();
// field value
parser.nextToken();
if (isPoint(value)) {
GeoPoint point = GeoUtils.parseGeoPoint(parser, new GeoPoint(), ignoreZValue);
return new Point(point.lon(), point.lat());
} else {
return parse(parser);
}
} catch (IOException | ParseException ex) {
throw new OpenSearchParseException("error parsing geometry ", ex);
}
}
Aggregations