Search in sources :

Example 1 with MapXContentParser

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);
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) IOException(java.io.IOException) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with MapXContentParser

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);
    }
}
Also used : MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) XContentParser(org.opensearch.common.xcontent.XContentParser) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser)

Example 3 with MapXContentParser

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);
    }
}
Also used : Geometry(org.opensearch.geometry.Geometry) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ParseException(java.text.ParseException) XContentParser(org.opensearch.common.xcontent.XContentParser) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser)

Example 4 with MapXContentParser

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());
            }
        }
    }
}
Also used : MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) XContentParserTests.generateRandomObject(org.opensearch.common.xcontent.XContentParserTests.generateRandomObject) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser)

Example 5 with MapXContentParser

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);
    }
}
Also used : MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) ArrayList(java.util.ArrayList) Point(org.opensearch.geometry.Point) IOException(java.io.IOException) Geometry(org.opensearch.geometry.Geometry) GeometryCollection(org.opensearch.geometry.GeometryCollection) OpenSearchParseException(org.opensearch.OpenSearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) OpenSearchParseException(org.opensearch.OpenSearchParseException) ParseException(java.text.ParseException) MapXContentParser(org.opensearch.common.xcontent.support.MapXContentParser) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

MapXContentParser (org.opensearch.common.xcontent.support.MapXContentParser)5 XContentParser (org.opensearch.common.xcontent.XContentParser)4 IOException (java.io.IOException)3 ParseException (java.text.ParseException)2 OpenSearchParseException (org.opensearch.OpenSearchParseException)2 Geometry (org.opensearch.geometry.Geometry)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 XContentParserTests.generateRandomObject (org.opensearch.common.xcontent.XContentParserTests.generateRandomObject)1 GeometryCollection (org.opensearch.geometry.GeometryCollection)1 Point (org.opensearch.geometry.Point)1