Class XContentHelper
java.lang.Object
org.elasticsearch.common.xcontent.XContentHelper
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic BytesReference
childBytes
(org.elasticsearch.xcontent.XContentParser parser) Returns the contents of an object as an unparsed BytesReference This is useful for things like mappings where we're copying bytes around but don't actually need to parse their contents, and so avoids building large maps of maps unnecessarilystatic String
convertToJson
(BytesReference bytes, boolean reformatJson) Deprecated.static String
convertToJson
(BytesReference bytes, boolean reformatJson, boolean prettyPrint) Deprecated.static String
convertToJson
(BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.elasticsearch.xcontent.XContentType xContentType) static String
convertToJson
(BytesReference bytes, boolean reformatJson, org.elasticsearch.xcontent.XContentType xContentType) convertToMap
(BytesReference bytes, boolean ordered) Deprecated.this method relies on auto-detection of content type.convertToMap
(BytesReference bytes, boolean ordered, org.elasticsearch.xcontent.XContentType xContentType) Exactly the same asconvertToMap(BytesReference, boolean, XContentType, Set, Set)
but none of the fields are filteredconvertToMap
(BytesReference bytes, boolean ordered, org.elasticsearch.xcontent.XContentType xContentType, Set<String> include, Set<String> exclude) Converts the given bytes into a map that is optionally ordered.convertToMap
(org.elasticsearch.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered) Convert a byte array in someXContent
format to aMap
.convertToMap
(org.elasticsearch.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered, Set<String> include, Set<String> exclude) Convert a byte array in someXContent
format to aMap
.convertToMap
(org.elasticsearch.xcontent.XContent xContent, InputStream input, boolean ordered) The same asconvertToMap(XContent, byte[], int, int, boolean, Set, Set)
but none of the fields are filtered.convertToMap
(org.elasticsearch.xcontent.XContent xContent, InputStream input, boolean ordered, Set<String> include, Set<String> exclude) Convert a string in someXContent
format to aMap
.convertToMap
(org.elasticsearch.xcontent.XContent xContent, String string, boolean ordered) Convert a string in someXContent
format to aMap
.static org.elasticsearch.xcontent.XContentParser
createParser
(org.elasticsearch.xcontent.NamedXContentRegistry registry, org.elasticsearch.xcontent.DeprecationHandler deprecation, BytesReference bytes) Deprecated.usecreateParser(XContentParserConfiguration, BytesReference, XContentType)
to avoid content type auto-detectionstatic org.elasticsearch.xcontent.XContentParser
createParser
(org.elasticsearch.xcontent.NamedXContentRegistry registry, org.elasticsearch.xcontent.DeprecationHandler deprecation, BytesReference bytes, org.elasticsearch.xcontent.XContentType xContentType) static org.elasticsearch.xcontent.XContentParser
createParser
(org.elasticsearch.xcontent.XContentParserConfiguration config, BytesReference bytes) Deprecated.usecreateParser(XContentParserConfiguration, BytesReference, XContentType)
to avoid content type auto-detectionstatic org.elasticsearch.xcontent.XContentParser
createParser
(org.elasticsearch.xcontent.XContentParserConfiguration config, BytesReference bytes, org.elasticsearch.xcontent.XContentType xContentType) Creates a parser for the bytes using the supplied content-typestatic void
Merges the defaults provided as the second parameter into the content of the first.static String
stripWhitespace
(String json) Accepts a JSON string, parses it and prints it without pretty-printing it.static BytesReference
toXContent
(org.elasticsearch.xcontent.ToXContent toXContent, org.elasticsearch.xcontent.XContentType xContentType, boolean humanReadable) Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
.static BytesReference
toXContent
(org.elasticsearch.xcontent.ToXContent toXContent, org.elasticsearch.xcontent.XContentType xContentType, org.elasticsearch.xcontent.ToXContent.Params params, boolean humanReadable) Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
.static boolean
Updates the provided changes into the source.static void
writeRawField
(String field, BytesReference source, org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.ToXContent.Params params) Deprecated.usewriteRawField(String, BytesReference, XContentType, XContentBuilder, Params)
to avoid content type auto-detectionstatic void
writeRawField
(String field, BytesReference source, org.elasticsearch.xcontent.XContentType xContentType, org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.ToXContent.Params params) Writes a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing usingXContentBuilder.rawField(String, InputStream, XContentType)
.static void
writeTo
(StreamOutput out, org.elasticsearch.xcontent.XContentType xContentType) Serialises new XContentType VND_ values in a bwc manner TODO remove in ES v9static org.elasticsearch.xcontent.XContentType
xContentType
(BytesReference bytes) Deprecated.the content type should not be guessed except for few cases where we effectively don't know the content type.static org.elasticsearch.xcontent.XContentType
Deprecated.the content type should not be guessed except for few cases where we effectively don't know the content type.
-
Constructor Details
-
XContentHelper
public XContentHelper()
-
-
Method Details
-
createParser
@Deprecated public static org.elasticsearch.xcontent.XContentParser createParser(org.elasticsearch.xcontent.NamedXContentRegistry registry, org.elasticsearch.xcontent.DeprecationHandler deprecation, BytesReference bytes) throws IOException Deprecated.usecreateParser(XContentParserConfiguration, BytesReference, XContentType)
to avoid content type auto-detectionCreates a parser based on the bytes provided- Throws:
IOException
-
createParser
@Deprecated public static org.elasticsearch.xcontent.XContentParser createParser(org.elasticsearch.xcontent.XContentParserConfiguration config, BytesReference bytes) throws IOException Deprecated.usecreateParser(XContentParserConfiguration, BytesReference, XContentType)
to avoid content type auto-detectionCreates a parser based on the bytes provided- Throws:
IOException
-
createParser
@Deprecated public static org.elasticsearch.xcontent.XContentParser createParser(org.elasticsearch.xcontent.NamedXContentRegistry registry, org.elasticsearch.xcontent.DeprecationHandler deprecation, BytesReference bytes, org.elasticsearch.xcontent.XContentType xContentType) throws IOException Creates a parser for the bytes provided- Throws:
IOException
-
createParser
public static org.elasticsearch.xcontent.XContentParser createParser(org.elasticsearch.xcontent.XContentParserConfiguration config, BytesReference bytes, org.elasticsearch.xcontent.XContentType xContentType) throws IOException Creates a parser for the bytes using the supplied content-type- Throws:
IOException
-
convertToMap
@Deprecated public static org.elasticsearch.core.Tuple<org.elasticsearch.xcontent.XContentType,Map<String, convertToMapObject>> (BytesReference bytes, boolean ordered) throws ElasticsearchParseException Deprecated.this method relies on auto-detection of content type. UseconvertToMap(BytesReference, boolean, XContentType)
instead with the properXContentType
Converts the given bytes into a map that is optionally ordered.Important: This can lose precision on numbers with a decimal point. It converts numbers like
"n": 1234.567
to adouble
which only has 52 bits of precision in the mantissa. This will come up most frequently when folks write nanosecond precision dates as a decimal number.- Throws:
ElasticsearchParseException
-
convertToMap
public static org.elasticsearch.core.Tuple<org.elasticsearch.xcontent.XContentType,Map<String, convertToMapObject>> (BytesReference bytes, boolean ordered, org.elasticsearch.xcontent.XContentType xContentType) Exactly the same asconvertToMap(BytesReference, boolean, XContentType, Set, Set)
but none of the fields are filtered -
convertToMap
public static org.elasticsearch.core.Tuple<org.elasticsearch.xcontent.XContentType,Map<String, convertToMapObject>> (BytesReference bytes, boolean ordered, org.elasticsearch.xcontent.XContentType xContentType, @Nullable Set<String> include, @Nullable Set<String> exclude) throws ElasticsearchParseException Converts the given bytes into a map that is optionally ordered. The providedXContentType
must be non-null.Important: This can lose precision on numbers with a decimal point. It converts numbers like
"n": 1234.567
to adouble
which only has 52 bits of precision in the mantissa. This will come up most frequently when folks write nanosecond precision dates as a decimal number.- Throws:
ElasticsearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.XContent xContent, String string, boolean ordered) throws ElasticsearchParseException Convert a string in someXContent
format to aMap
. Throws anElasticsearchParseException
if there is any error.- Throws:
ElasticsearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.XContent xContent, InputStream input, boolean ordered) throws ElasticsearchParseException The same asconvertToMap(XContent, byte[], int, int, boolean, Set, Set)
but none of the fields are filtered.- Throws:
ElasticsearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.XContent xContent, InputStream input, boolean ordered, @Nullable Set<String> include, @Nullable Set<String> exclude) throws ElasticsearchParseException Convert a string in someXContent
format to aMap
. Throws anElasticsearchParseException
if there is any error. Note that unlikeconvertToMap(BytesReference, boolean)
, this doesn't automatically uncompress the input. Additionally, fields may be included or excluded from the parsing.- Throws:
ElasticsearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered) throws ElasticsearchParseException Convert a byte array in someXContent
format to aMap
. Throws anElasticsearchParseException
if there is any error. Note that unlikeconvertToMap(BytesReference, boolean)
, this doesn't automatically uncompress the input.- Throws:
ElasticsearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.elasticsearch.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered, @Nullable Set<String> include, @Nullable Set<String> exclude) throws ElasticsearchParseException Convert a byte array in someXContent
format to aMap
. Throws anElasticsearchParseException
if there is any error. Note that unlikeconvertToMap(BytesReference, boolean)
, this doesn't automatically uncompress the input. UnlikeconvertToMap(XContent, byte[], int, int, boolean)
this optionally accepts fields to include or exclude during XContent parsing.- Throws:
ElasticsearchParseException
-
convertToJson
@Deprecated public static String convertToJson(BytesReference bytes, boolean reformatJson) throws IOException Deprecated.- Throws:
IOException
-
convertToJson
@Deprecated public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException Deprecated.- Throws:
IOException
-
convertToJson
public static String convertToJson(BytesReference bytes, boolean reformatJson, org.elasticsearch.xcontent.XContentType xContentType) throws IOException - Throws:
IOException
-
stripWhitespace
Accepts a JSON string, parses it and prints it without pretty-printing it. This is useful where a piece of JSON is formatted for legibility, but needs to be stripped of unnecessary whitespace e.g. for comparison in a test.- Parameters:
json
- the JSON to format- Returns:
- reformatted JSON
- Throws:
IOException
- if the reformatting fails, e.g. because the JSON is not well-formed
-
convertToJson
public static String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.elasticsearch.xcontent.XContentType xContentType) throws IOException - Throws:
IOException
-
update
public static boolean update(Map<String, Object> source, Map<String, Object> changes, boolean checkUpdatesAreUnequal) Updates the provided changes into the source. If the key exists in the changes, it overrides the one in source unless both are Maps, in which case it recursively updated it.- Parameters:
source
- the original map to be updatedchanges
- the changes to update into updatedcheckUpdatesAreUnequal
- should this method check if updates to the same key (that are not both maps) are unequal? This is just a .equals check on the objects, but that can take some time on long strings.- Returns:
- true if the source map was modified
-
mergeDefaults
Merges the defaults provided as the second parameter into the content of the first. Only does recursive merge for inner maps. -
writeRawField
@Deprecated public static void writeRawField(String field, BytesReference source, org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.ToXContent.Params params) throws IOException Deprecated.usewriteRawField(String, BytesReference, XContentType, XContentBuilder, Params)
to avoid content type auto-detectionWrites a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing usingXContentBuilder.rawField(String, InputStream)
.- Throws:
IOException
-
writeRawField
public static void writeRawField(String field, BytesReference source, org.elasticsearch.xcontent.XContentType xContentType, org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.ToXContent.Params params) throws IOException Writes a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing usingXContentBuilder.rawField(String, InputStream, XContentType)
.- Throws:
IOException
-
toXContent
public static BytesReference toXContent(org.elasticsearch.xcontent.ToXContent toXContent, org.elasticsearch.xcontent.XContentType xContentType, boolean humanReadable) throws IOException Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
. Wraps the output into a new anonymous object according to the value returned by theToXContent.isFragment()
method returns.- Throws:
IOException
-
toXContent
public static BytesReference toXContent(org.elasticsearch.xcontent.ToXContent toXContent, org.elasticsearch.xcontent.XContentType xContentType, org.elasticsearch.xcontent.ToXContent.Params params, boolean humanReadable) throws IOException Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
. Wraps the output into a new anonymous object according to the value returned by theToXContent.isFragment()
method returns.- Throws:
IOException
-
xContentTypeMayCompressed
@Deprecated public static org.elasticsearch.xcontent.XContentType xContentTypeMayCompressed(BytesReference bytes) Deprecated.the content type should not be guessed except for few cases where we effectively don't know the content type. The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. This method is deprecated to prevent usages of it from spreading further without specific reasons.Guesses the content type based on the provided bytes which may be compressed. -
xContentType
@Deprecated public static org.elasticsearch.xcontent.XContentType xContentType(BytesReference bytes) Deprecated.the content type should not be guessed except for few cases where we effectively don't know the content type. The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed. This method is deprecated to prevent usages of it from spreading further without specific reasons.Guesses the content type based on the provided bytes. -
childBytes
public static BytesReference childBytes(org.elasticsearch.xcontent.XContentParser parser) throws IOException Returns the contents of an object as an unparsed BytesReference This is useful for things like mappings where we're copying bytes around but don't actually need to parse their contents, and so avoids building large maps of maps unnecessarily- Throws:
IOException
-
writeTo
public static void writeTo(StreamOutput out, org.elasticsearch.xcontent.XContentType xContentType) throws IOException Serialises new XContentType VND_ values in a bwc manner TODO remove in ES v9- Parameters:
out
- stream output of the destination nodexContentType
- an instance to serialize- Throws:
IOException
-