Package org.opensearch.common.xcontent
Class XContentHelper
java.lang.Object
org.opensearch.common.xcontent.XContentHelper
public class XContentHelper
extends java.lang.Object
-
Constructor Summary
Constructors Constructor Description XContentHelper()
-
Method Summary
Modifier and Type Method Description static BytesReference
childBytes(org.opensearch.common.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 java.lang.String
convertToJson(BytesReference bytes, boolean reformatJson)
Deprecated.static java.lang.String
convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint)
Deprecated.static java.lang.String
convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.common.xcontent.XContentType xContentType)
static java.lang.String
convertToJson(BytesReference bytes, boolean reformatJson, org.opensearch.common.xcontent.XContentType xContentType)
static org.opensearch.common.collect.Tuple<org.opensearch.common.xcontent.XContentType,java.util.Map<java.lang.String,java.lang.Object>>
convertToMap(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,java.util.Map<java.lang.String,java.lang.Object>>
convertToMap(BytesReference bytes, boolean ordered, org.opensearch.common.xcontent.XContentType xContentType)
Converts the given bytes into a map that is optionally ordered.static java.util.Map<java.lang.String,java.lang.Object>
convertToMap(org.opensearch.common.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered)
Convert a byte array in someXContent
format to aMap
.static java.util.Map<java.lang.String,java.lang.Object>
convertToMap(org.opensearch.common.xcontent.XContent xContent, java.io.InputStream input, boolean ordered)
Convert a string in someXContent
format to aMap
.static java.util.Map<java.lang.String,java.lang.Object>
convertToMap(org.opensearch.common.xcontent.XContent xContent, java.lang.String string, boolean ordered)
Convert a string in someXContent
format to aMap
.static org.opensearch.common.xcontent.XContentParser
createParser(org.opensearch.common.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.common.xcontent.DeprecationHandler deprecationHandler, BytesReference bytes)
Deprecated.usecreateParser(NamedXContentRegistry, DeprecationHandler, BytesReference, XContentType)
to avoid content type auto-detectionstatic org.opensearch.common.xcontent.XContentParser
createParser(org.opensearch.common.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.common.xcontent.DeprecationHandler deprecationHandler, BytesReference bytes, org.opensearch.common.xcontent.XContentType xContentType)
Creates a parser for the bytes using the supplied content-typestatic void
mergeDefaults(java.util.Map<java.lang.String,java.lang.Object> content, java.util.Map<java.lang.String,java.lang.Object> defaults)
Merges the defaults provided as the second parameter into the content of the first.static java.lang.String
stripWhitespace(java.lang.String json)
Accepts a JSON string, parses it and prints it without pretty-printing it.static BytesReference
toXContent(org.opensearch.common.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, boolean humanReadable)
Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
.static BytesReference
toXContent(org.opensearch.common.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.common.xcontent.ToXContent.Params params, boolean humanReadable)
Returns the bytes that represent the XContent output of the providedToXContent
object, using the providedXContentType
.static boolean
update(java.util.Map<java.lang.String,java.lang.Object> source, java.util.Map<java.lang.String,java.lang.Object> changes, boolean checkUpdatesAreUnequal)
Updates the provided changes into the source.static void
writeRawField(java.lang.String field, BytesReference source, org.opensearch.common.xcontent.XContentBuilder builder, org.opensearch.common.xcontent.ToXContent.Params params)
Deprecated.usewriteRawField(String, BytesReference, XContentType, XContentBuilder, Params)
to avoid content type auto-detectionstatic void
writeRawField(java.lang.String field, BytesReference source, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.common.xcontent.XContentBuilder builder, org.opensearch.common.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 org.opensearch.common.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.
-
Constructor Details
-
XContentHelper
public XContentHelper()
-
-
Method Details
-
createParser
@Deprecated public static org.opensearch.common.xcontent.XContentParser createParser(org.opensearch.common.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.common.xcontent.DeprecationHandler deprecationHandler, BytesReference bytes) throws java.io.IOExceptionDeprecated.usecreateParser(NamedXContentRegistry, DeprecationHandler, BytesReference, XContentType)
to avoid content type auto-detectionCreates a parser based on the bytes provided- Throws:
java.io.IOException
-
createParser
public static org.opensearch.common.xcontent.XContentParser createParser(org.opensearch.common.xcontent.NamedXContentRegistry xContentRegistry, org.opensearch.common.xcontent.DeprecationHandler deprecationHandler, BytesReference bytes, org.opensearch.common.xcontent.XContentType xContentType) throws java.io.IOExceptionCreates a parser for the bytes using the supplied content-type- Throws:
java.io.IOException
-
convertToMap
@Deprecated public static org.opensearch.common.collect.Tuple<org.opensearch.common.xcontent.XContentType,java.util.Map<java.lang.String,java.lang.Object>> convertToMap(BytesReference bytes, boolean ordered) throws OpenSearchParseExceptionDeprecated.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.- Throws:
OpenSearchParseException
-
convertToMap
public static org.opensearch.common.collect.Tuple<org.opensearch.common.xcontent.XContentType,java.util.Map<java.lang.String,java.lang.Object>> convertToMap(BytesReference bytes, boolean ordered, org.opensearch.common.xcontent.XContentType xContentType) throws OpenSearchParseExceptionConverts the given bytes into a map that is optionally ordered. The providedXContentType
must be non-null.- Throws:
OpenSearchParseException
-
convertToMap
public static java.util.Map<java.lang.String,java.lang.Object> convertToMap(org.opensearch.common.xcontent.XContent xContent, java.lang.String string, boolean ordered) throws OpenSearchParseExceptionConvert a string in someXContent
format to aMap
. Throws anOpenSearchParseException
if there is any error.- Throws:
OpenSearchParseException
-
convertToMap
public static java.util.Map<java.lang.String,java.lang.Object> convertToMap(org.opensearch.common.xcontent.XContent xContent, java.io.InputStream input, boolean ordered) throws OpenSearchParseExceptionConvert 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:
OpenSearchParseException
-
convertToMap
public static java.util.Map<java.lang.String,java.lang.Object> convertToMap(org.opensearch.common.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered) throws OpenSearchParseExceptionConvert 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:
OpenSearchParseException
-
convertToJson
@Deprecated public static java.lang.String convertToJson(BytesReference bytes, boolean reformatJson) throws java.io.IOExceptionDeprecated.- Throws:
java.io.IOException
-
convertToJson
@Deprecated public static java.lang.String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint) throws java.io.IOExceptionDeprecated.- Throws:
java.io.IOException
-
convertToJson
public static java.lang.String convertToJson(BytesReference bytes, boolean reformatJson, org.opensearch.common.xcontent.XContentType xContentType) throws java.io.IOException- Throws:
java.io.IOException
-
stripWhitespace
public static java.lang.String stripWhitespace(java.lang.String json) throws java.io.IOExceptionAccepts 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:
java.io.IOException
- if the reformatting fails, e.g. because the JSON is not well-formed
-
convertToJson
public static java.lang.String convertToJson(BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.common.xcontent.XContentType xContentType) throws java.io.IOException- Throws:
java.io.IOException
-
update
public static boolean update(java.util.Map<java.lang.String,java.lang.Object> source, java.util.Map<java.lang.String,java.lang.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
public static void mergeDefaults(java.util.Map<java.lang.String,java.lang.Object> content, java.util.Map<java.lang.String,java.lang.Object> defaults)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(java.lang.String field, BytesReference source, org.opensearch.common.xcontent.XContentBuilder builder, org.opensearch.common.xcontent.ToXContent.Params params) throws java.io.IOExceptionDeprecated.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:
java.io.IOException
-
writeRawField
public static void writeRawField(java.lang.String field, BytesReference source, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.common.xcontent.XContentBuilder builder, org.opensearch.common.xcontent.ToXContent.Params params) throws java.io.IOExceptionWrites a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing usingXContentBuilder.rawField(String, InputStream, XContentType)
.- Throws:
java.io.IOException
-
toXContent
public static BytesReference toXContent(org.opensearch.common.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, boolean humanReadable) throws java.io.IOExceptionReturns 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:
java.io.IOException
-
toXContent
public static BytesReference toXContent(org.opensearch.common.xcontent.ToXContent toXContent, org.opensearch.common.xcontent.XContentType xContentType, org.opensearch.common.xcontent.ToXContent.Params params, boolean humanReadable) throws java.io.IOExceptionReturns 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:
java.io.IOException
-
xContentType
@Deprecated public static org.opensearch.common.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.opensearch.common.xcontent.XContentParser parser) throws java.io.IOExceptionReturns 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:
java.io.IOException
-