Class JsonStringEncoder


  • public final class JsonStringEncoder
    extends java.lang.Object
    Helper class used for efficient encoding of JSON String values (including JSON field names) into Strings or UTF-8 byte arrays.

    Note that methods in here are somewhat optimized, but not ridiculously so. Reason is that conversion method results are expected to be cached so that these methods will not be hot spots during normal operation.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      byte[] encodeAsUTF8​(java.lang.CharSequence text)
      Overloaded variant of encodeAsUTF8(String).
      byte[] encodeAsUTF8​(java.lang.String text)
      Will encode given String as UTF-8 (without any escaping) and return the resulting byte array.
      static JsonStringEncoder getInstance()
      Factory method for getting an instance; this is either recycled per-thread instance, or a newly constructed one.
      char[] quoteAsString​(java.lang.CharSequence input)
      Overloaded variant of quoteAsString(String).
      void quoteAsString​(java.lang.CharSequence input, java.lang.StringBuilder output)
      Method that will quote text contents using JSON standard quoting, and append results to a supplied StringBuilder.
      char[] quoteAsString​(java.lang.String input)
      Method that will escape text contents using JSON standard escaping, and return results as a character array.
      byte[] quoteAsUTF8​(java.lang.String text)
      Method that will escape text contents using JSON standard escaping, encode resulting String as UTF-8 bytes and return results as a byte array.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JsonStringEncoder

        public JsonStringEncoder()
    • Method Detail

      • getInstance

        public static JsonStringEncoder getInstance()
        Factory method for getting an instance; this is either recycled per-thread instance, or a newly constructed one.
        Returns:
        Static stateless encoder instance
      • quoteAsString

        public char[] quoteAsString​(java.lang.String input)
        Method that will escape text contents using JSON standard escaping, and return results as a character array.
        Parameters:
        input - Value String to process
        Returns:
        JSON-escaped String matching input
      • quoteAsString

        public char[] quoteAsString​(java.lang.CharSequence input)
        Overloaded variant of quoteAsString(String).
        Parameters:
        input - Value CharSequence to process
        Returns:
        JSON-escaped String matching input
        Since:
        2.10
      • quoteAsString

        public void quoteAsString​(java.lang.CharSequence input,
                                  java.lang.StringBuilder output)
        Method that will quote text contents using JSON standard quoting, and append results to a supplied StringBuilder. Use this variant if you have e.g. a StringBuilder and want to avoid superfluous copying of it.
        Parameters:
        input - Value CharSequence to process
        output - StringBuilder to append escaped contents to
        Since:
        2.8
      • quoteAsUTF8

        public byte[] quoteAsUTF8​(java.lang.String text)
        Method that will escape text contents using JSON standard escaping, encode resulting String as UTF-8 bytes and return results as a byte array.
        Parameters:
        text - Value String to process
        Returns:
        UTF-8 encoded bytes of JSON-escaped text
      • encodeAsUTF8

        public byte[] encodeAsUTF8​(java.lang.String text)
        Will encode given String as UTF-8 (without any escaping) and return the resulting byte array.
        Parameters:
        text - Value String to process
        Returns:
        UTF-8 encoded bytes of text (without any escaping)
      • encodeAsUTF8

        public byte[] encodeAsUTF8​(java.lang.CharSequence text)
        Overloaded variant of encodeAsUTF8(String).
        Parameters:
        text - Value CharSequence to process
        Returns:
        UTF-8 encoded bytes of text (without any escaping)
        Since:
        2.11