Package org.opensearch.common.xcontent
Class XContentHelper
java.lang.Object
org.opensearch.common.xcontent.XContentHelper
Helper for xcontent utilities.
- Opensearch.internal:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.opensearch.core.common.bytes.BytesReference
childBytes
(org.opensearch.core.xcontent.XContentParser parser) Returns the contents of an object as an unparsed BytesReferencestatic String
convertToJson
(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson) Deprecated.static String
convertToJson
(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint) Deprecated.static String
convertToJson
(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.common.xcontent.XContentType xContentType) Deprecated.static String
convertToJson
(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.core.xcontent.MediaType mediaType) Converts the givenMediaType
to a json stringstatic String
convertToJson
(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, org.opensearch.core.xcontent.MediaType mediaType) static org.opensearch.common.collect.Tuple
<org.opensearch.common.xcontent.XContentType, Map<String, Object>> convertToMap
(org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered) Deprecated.this method relies on auto-detection of content type.static org.opensearch.common.collect.Tuple
<org.opensearch.common.xcontent.XContentType, Map<String, Object>> convertToMap
(org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered, org.opensearch.common.xcontent.XContentType xContentType) Deprecated.useconvertToMap(BytesReference, boolean, MediaType)
insteadstatic org.opensearch.common.collect.Tuple
<? extends org.opensearch.core.xcontent.MediaType, Map<String, Object>> convertToMap
(org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered, org.opensearch.core.xcontent.MediaType mediaType) Converts the given bytes into a map that is optionally ordered.convertToMap
(org.opensearch.core.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered) Convert a byte array in someXContent
format to aMap
.convertToMap
(org.opensearch.core.xcontent.XContent xContent, InputStream input, boolean ordered) Convert a string in someXContent
format to aMap
.convertToMap
(org.opensearch.core.xcontent.XContent xContent, String string, boolean ordered) Convert a string in someXContent
format to aMap
.static org.opensearch.core.xcontent.XContentParser
createParser
(org.opensearch.core.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.core.xcontent.DeprecationHandler deprecationHandler, org.opensearch.core.common.bytes.BytesReference bytes) Deprecated.usecreateParser(NamedXContentRegistry, DeprecationHandler, BytesReference, MediaType)
to avoid content type auto-detectionstatic org.opensearch.core.xcontent.XContentParser
createParser
(org.opensearch.core.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.core.xcontent.DeprecationHandler deprecationHandler, org.opensearch.core.common.bytes.BytesReference bytes, org.opensearch.core.xcontent.MediaType mediaType) 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 org.opensearch.core.common.bytes.BytesReference
toXContent
(org.opensearch.core.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, boolean humanReadable) Deprecated.static boolean
Updates the provided changes into the source.static void
writeRawField
(String field, org.opensearch.core.common.bytes.BytesReference source, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.core.xcontent.XContentBuilder builder, org.opensearch.core.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, MediaType)
.static void
writeRawField
(String field, org.opensearch.core.common.bytes.BytesReference source, org.opensearch.core.xcontent.XContentBuilder builder, org.opensearch.core.xcontent.ToXContent.Params params) Deprecated.usewriteRawField(String, BytesReference, XContentType, XContentBuilder, Params)
to avoid content type auto-detection
-
Constructor Details
-
XContentHelper
public XContentHelper()
-
-
Method Details
-
createParser
@Deprecated public static org.opensearch.core.xcontent.XContentParser createParser(org.opensearch.core.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.core.xcontent.DeprecationHandler deprecationHandler, org.opensearch.core.common.bytes.BytesReference bytes) throws IOException Deprecated.usecreateParser(NamedXContentRegistry, DeprecationHandler, BytesReference, MediaType)
to avoid content type auto-detectionCreates a parser based on the bytes provided- Throws:
IOException
-
createParser
public static org.opensearch.core.xcontent.XContentParser createParser(org.opensearch.core.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.core.xcontent.DeprecationHandler deprecationHandler, org.opensearch.core.common.bytes.BytesReference bytes, org.opensearch.core.xcontent.MediaType mediaType) throws IOException Creates a parser for the bytes using the supplied content-type- Throws:
IOException
-
convertToMap
@Deprecated public static org.opensearch.common.collect.Tuple<org.opensearch.common.xcontent.XContentType,Map<String, convertToMapObject>> (org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered) throws org.opensearch.OpenSearchParseException Deprecated.this method relies on auto-detection of content type. UseconvertToMap(BytesReference, boolean, MediaType)
instead with the properXContentType
Converts the given bytes into a map that is optionally ordered.- Throws:
org.opensearch.OpenSearchParseException
-
convertToMap
@Deprecated public static org.opensearch.common.collect.Tuple<org.opensearch.common.xcontent.XContentType,Map<String, convertToMapObject>> (org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered, org.opensearch.common.xcontent.XContentType xContentType) Deprecated.useconvertToMap(BytesReference, boolean, MediaType)
insteadConverts the given bytes into a map that is optionally ordered. The providedXContentType
must be non-null. -
convertToMap
public static org.opensearch.common.collect.Tuple<? extends org.opensearch.core.xcontent.MediaType,Map<String, convertToMapObject>> (org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered, org.opensearch.core.xcontent.MediaType mediaType) throws org.opensearch.OpenSearchParseException Converts the given bytes into a map that is optionally ordered. The providedXContentType
must be non-null.- Throws:
org.opensearch.OpenSearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.opensearch.core.xcontent.XContent xContent, String string, boolean ordered) throws org.opensearch.OpenSearchParseException Convert a string in someXContent
format to aMap
. Throws anOpenSearchParseException
if there is any error.- Throws:
org.opensearch.OpenSearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.opensearch.core.xcontent.XContent xContent, InputStream input, boolean ordered) throws org.opensearch.OpenSearchParseException Convert a string in someXContent
format to aMap
. Throws anOpenSearchParseException
if there is any error. Note that unlikeconvertToMap(BytesReference, boolean)
, this doesn't automatically uncompress the input.- Throws:
org.opensearch.OpenSearchParseException
-
convertToMap
public static Map<String,Object> convertToMap(org.opensearch.core.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered) throws org.opensearch.OpenSearchParseException Convert a byte array in someXContent
format to aMap
. Throws anOpenSearchParseException
if there is any error. Note that unlikeconvertToMap(BytesReference, boolean)
, this doesn't automatically uncompress the input.- Throws:
org.opensearch.OpenSearchParseException
-
convertToJson
@Deprecated public static String convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson) throws IOException Deprecated.- Throws:
IOException
-
convertToJson
@Deprecated public static String convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws IOException Deprecated.- Throws:
IOException
-
convertToJson
public static String convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, org.opensearch.core.xcontent.MediaType mediaType) 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
@Deprecated public static String convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.common.xcontent.XContentType xContentType) throws IOException Deprecated.Converts the XContentType to a json string- Throws:
IOException
-
convertToJson
public static String convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.core.xcontent.MediaType mediaType) throws IOException Converts the givenMediaType
to a json string- 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, org.opensearch.core.common.bytes.BytesReference source, org.opensearch.core.xcontent.XContentBuilder builder, org.opensearch.core.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, org.opensearch.core.common.bytes.BytesReference source, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.core.xcontent.XContentBuilder builder, org.opensearch.core.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, MediaType)
.- Throws:
IOException
-
toXContent
@Deprecated public static org.opensearch.core.common.bytes.BytesReference toXContent(org.opensearch.core.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, boolean humanReadable) throws IOException Deprecated.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
-
childBytes
public static org.opensearch.core.common.bytes.BytesReference childBytes(org.opensearch.core.xcontent.XContentParser parser) throws IOException Returns the contents of an object as an unparsed BytesReferenceThis 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
-
convertToJson(BytesReference, boolean, boolean, MediaType)
instead