private ShapeBuilder getShapeBuilderFromJson(String json) throws IOException { XContentParser parser = null; parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, json); parser.nextToken(); return ShapeParser.parse(parser); }
protected void updateRequestWithCollapse(Select select, SearchRequestBuilder request) throws SqlParseException { JsonFactory jsonFactory = new JsonFactory(); for (Hint hint : select.getHints()) { if (hint.getType() == HintType.COLLAPSE && hint.getParams() != null && 0 < hint.getParams().length) { try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, jsonFactory.createParser(hint.getParams()[0].toString()))) { request.setCollapse(CollapseBuilder.fromXContent(parser)); } catch (IOException e) { throw new SqlParseException("could not parse collapse hint: " + e.getMessage()); } } } }
} else { List<BucketOrder> orderElements = new ArrayList<>(); try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(value))) { XContentParser.Token currentToken = parser.nextToken(); if (currentToken == XContentParser.Token.START_OBJECT) { orderElements.add(InternalOrder.Parser.parseOrderParam(parser)); } else if (currentToken == XContentParser.Token.START_ARRAY) { for (currentToken = parser.nextToken(); currentToken != XContentParser.Token.END_ARRAY; currentToken = parser.nextToken()) { if (currentToken == XContentParser.Token.START_OBJECT) { orderElements.add(InternalOrder.Parser.parseOrderParam(parser)); } else { throw new ParsingException(parser.getTokenLocation(), "Invalid token in order array"); break; case "include": try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, value)) { parser.nextToken(); include = IncludeExclude.parseInclude(parser); try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, value)) { parser.nextToken(); exclude = IncludeExclude.parseExclude(parser);
/** * Parse the script configured in the given settings. */ public static Script parse(Settings settings) { try (XContentBuilder builder = JsonXContent.contentBuilder()){ builder.startObject(); settings.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); try (InputStream stream = BytesReference.bytes(builder).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { return parse(parser); } } catch (IOException e) { // it should not happen since we are not actually reading from a stream but an in-memory byte[] throw new IllegalStateException(e); } }
private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); if (pretty) { builder.prettyPrint(); } if (human) { builder.humanReadable(true); } return builder; }
private boolean mayWriteRawData(XContentType contentType) { // When the current generator is filtered (ie filter != null) // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; }
try (JsonXContentParser parser = new JsonXContentParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY, true, Collections.emptyList()).getNamedXContents()), LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(paramer.clauses))) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { QueryBuilder query = SpanNearQueryBuilder.parseInnerQueryBuilder(parser); if (!(query instanceof SpanQueryBuilder)) { throw new ParsingException(parser.getTokenLocation(), "spanNear [clauses] must be of type span query");
private boolean mayWriteRawData(XContentType contentType) { // When the current generator is filtered (ie filter != null) // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; }
@Test public void testComplianceLicenseMap() throws Exception { SearchGuardLicense license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license1.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertFalse(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), new Feature[0]); license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license3.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertFalse(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), new Feature[0]); license = new SearchGuardLicense(XContentHelper .convertToMap(new BytesArray(FileHelper.loadFile("license2.json")), false, JsonXContent.jsonXContent.type()).v2(), cs); Assert.assertTrue(license.hasFeature(Feature.COMPLIANCE)); Assert.assertArrayEquals(license.getFeatures(), Feature.values()); }
/** * 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, final boolean ignoreZValue) throws ElasticsearchParseException { try { XContentBuilder content = JsonXContent.contentBuilder(); content.startObject(); content.field("null_value", value); content.endObject(); try (InputStream stream = BytesReference.bytes(content).streamInput(); XContentParser parser = JsonXContent.jsonXContent.createParser( NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, stream)) { parser.nextToken(); // start object parser.nextToken(); // field name parser.nextToken(); // field value return parseGeoPoint(parser, new GeoPoint(), ignoreZValue); } } catch (IOException ex) { throw new ElasticsearchParseException("error parsing geopoint", ex); } }
@Override public String toString() { try { return Strings.toString(toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS)); } catch (IOException e) { return super.toString(); } }
private static String convertToYaml(String type, BytesReference bytes, boolean prettyPrint) throws IOException { try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, bytes.streamInput())) { parser.nextToken(); parser.nextToken(); if(!type.equals((parser.currentName()))) { return null; } parser.nextToken(); XContentBuilder builder = XContentFactory.yamlBuilder(); if (prettyPrint) { builder.prettyPrint(); } builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML); return Strings.toString(builder); } }
private boolean mayWriteRawData(XContentType contentType) { // When the current generator is filtered (ie filter != null) // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; }
case "extended_bounds": ExtendedBounds extendedBounds = null; try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(value))) { extendedBounds = ExtendedBounds.PARSER.parse(parser, null); } catch (IOException ex) {
/** * Helper to declare an object that will be parsed into a {@link BytesReference} */ public void declareRawObject(final AbstractObjectParser<Value, Context> parser, final BiConsumer<Value, BytesReference> consumer, final ParseField field) { final CheckedFunction<XContentParser, BytesReference, IOException> bytesParser = p -> { try (XContentBuilder builder = JsonXContent.contentBuilder()) { builder.copyCurrentStructure(p); return BytesReference.bytes(builder); } }; parser.declareField(consumer, bytesParser, field, ValueType.OBJECT); }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field("index_patterns", indexPatterns); builder.field("order", order); if (version != null) { builder.field("version", version); } builder.startObject("settings"); settings.toXContent(builder, params); builder.endObject(); builder.startObject("mappings"); for (Map.Entry<String, String> entry : mappings.entrySet()) { builder.field(entry.getKey()); XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, entry.getValue()); builder.copyCurrentStructure(parser); } builder.endObject(); builder.startObject("aliases"); for (Alias alias : aliases) { alias.toXContent(builder, params); } builder.endObject(); return builder; } }
private boolean mayWriteRawData(XContentType contentType) { // When the current generator is filtered (ie filter != null) // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; }
InnerHitBuilder ihb = null; if (condition.getInnerHits() != null) { try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(condition.getInnerHits()))) { ihb = InnerHitBuilder.fromXContent(parser); } catch (IOException e) {
@Override public String convert(QueryBuilder anItem) { try { BytesReference bytes = anItem.toXContent(JsonXContent.contentBuilder(), new ToXContent.MapParams(null)) .bytes(); return bytes.utf8ToString(); } catch (IOException e) { log.error("Unable to transform querybuilder to a json string due to an exception", e); throw new RuntimeException("Unable to transform querybuilder to a json string due to an exception", e); } } }
protected XContentBuilder initContentBuilder() throws IOException { return JsonXContent.contentBuilder(); }