Class XContentHelper

java.lang.Object
org.opensearch.common.xcontent.XContentHelper

public class XContentHelper extends Object
Helper for xcontent utilities.
Opensearch.internal:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.opensearch.core.common.bytes.BytesReference
    childBytes(org.opensearch.core.xcontent.XContentParser parser)
    Returns the contents of an object as an unparsed BytesReference
    static 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)
    static String
    convertToJson(org.opensearch.core.common.bytes.BytesReference bytes, boolean reformatJson, boolean prettyPrint, org.opensearch.core.xcontent.MediaType mediaType)
    Converts the given MediaType to a json string
    static 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)
    static 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.
    static Map<String,Object>
    convertToMap(org.opensearch.core.xcontent.XContent xContent, byte[] bytes, int offset, int length, boolean ordered)
    Convert a byte array in some XContent format to a Map.
    static Map<String,Object>
    convertToMap(org.opensearch.core.xcontent.XContent xContent, InputStream input, boolean ordered)
    Convert a string in some XContent format to a Map.
    static Map<String,Object>
    convertToMap(org.opensearch.core.xcontent.XContent xContent, String string, boolean ordered)
    Convert a string in some XContent format to a Map.
    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)
    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)
    Creates a parser for the bytes using the supplied content-type
    static void
    mergeDefaults(Map<String,Object> content, Map<String,Object> defaults)
    Merges the defaults provided as the second parameter into the content of the first.
    static String
    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
    update(Map<String,Object> source, Map<String,Object> changes, boolean checkUpdatesAreUnequal)
    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 using XContentBuilder.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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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
      Creates 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,Object>> convertToMap(org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered) throws org.opensearch.OpenSearchParseException
      Deprecated.
      this method relies on auto-detection of content type. Use convertToMap(BytesReference, boolean, MediaType) instead with the proper XContentType
      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,Object>> convertToMap(org.opensearch.core.common.bytes.BytesReference bytes, boolean ordered, org.opensearch.common.xcontent.XContentType xContentType)
      Converts the given bytes into a map that is optionally ordered. The provided XContentType must be non-null.
    • convertToMap

      public static 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) throws org.opensearch.OpenSearchParseException
      Converts the given bytes into a map that is optionally ordered. The provided XContentType 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 some XContent format to a Map. Throws an OpenSearchParseException 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 some XContent format to a Map. Throws an OpenSearchParseException if there is any error. Note that unlike convertToMap(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 some XContent format to a Map. Throws an OpenSearchParseException if there is any error. Note that unlike convertToMap(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

      public static String stripWhitespace(String json) throws IOException
      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
      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 given MediaType 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 updated
      changes - the changes to update into updated
      checkUpdatesAreUnequal - 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(Map<String,Object> content, Map<String,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(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.
      Writes a "raw" (bytes) field, handling cases where the bytes are compressed, and tries to optimize writing using XContentBuilder.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 using XContentBuilder.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 provided ToXContent object, using the provided XContentType. Wraps the output into a new anonymous object according to the value returned by the ToXContent.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 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