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.

    NOTE: starting with 2.9.3, access to most functionality should go through BufferRecyclers and NOT directly through this class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected ByteArrayBuilder _bytes
      Lazily-constructed builder used for UTF-8 encoding of text values (quoted and unquoted)
      protected char[] _qbuf
      Temporary buffer used for composing quote/escape sequences
      protected TextBuffer _text
      Lazily constructed text buffer used to produce JSON encoded Strings as characters (without UTF-8 encoding)
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      byte[] encodeAsUTF8​(java.lang.String text)
      Will encode given String as UTF-8 (without any quoting), return resulting byte array.
      static JsonStringEncoder getInstance()
      Deprecated.
      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 quote text contents using JSON standard quoting, and return results as a character array
      byte[] quoteAsUTF8​(java.lang.String text)
      Will quote given JSON String value using standard quoting, encode results as UTF-8, and return result as a byte array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • _text

        protected TextBuffer _text
        Lazily constructed text buffer used to produce JSON encoded Strings as characters (without UTF-8 encoding)
      • _bytes

        protected ByteArrayBuilder _bytes
        Lazily-constructed builder used for UTF-8 encoding of text values (quoted and unquoted)
      • _qbuf

        protected final char[] _qbuf
        Temporary buffer used for composing quote/escape sequences
    • Constructor Detail

      • JsonStringEncoder

        public JsonStringEncoder()
    • Method Detail

      • quoteAsString

        public char[] quoteAsString​(java.lang.String input)
        Method that will quote text contents using JSON standard quoting, and return results as a character array
      • 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.
        Since:
        2.8
      • quoteAsUTF8

        public byte[] quoteAsUTF8​(java.lang.String text)
        Will quote given JSON String value using standard quoting, encode results as UTF-8, and return result as a byte array.
      • encodeAsUTF8

        public byte[] encodeAsUTF8​(java.lang.String text)
        Will encode given String as UTF-8 (without any quoting), return resulting byte array.