Class Snippet


  • public class Snippet
    extends Object
    Constructs short snippets of serialized JSON text representations of JsonValue instances in a way that is ideal for error messages. Instances of Snippet are thread-safe, reusable and memory-safe. Snippet serializes only enough of the json to fill the desired snippet size and is therefore safe to use regardless of the size of the JsonValue.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private class  Snippet.Buffer
      There are several buffers involved in the creation of a json string.
      private static class  Snippet.PassthroughWriter  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private jakarta.json.stream.JsonGeneratorFactory generatorFactory  
      private int max  
    • Constructor Summary

      Constructors 
      Constructor Description
      Snippet​(int max, jakarta.json.stream.JsonGeneratorFactory generatorFactory)
      This is the preferred approach to using Snippet in any context where there is an existing JsonGeneratorFactory in scope.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static Snippet of​(int max)
      This factory should be used only in static or other scenarios were there is no JsonGeneratorFactory instance in scope - ie external code.
      static String of​(int max, jakarta.json.JsonValue value)
      Create a serialized json representation of the supplied JsonValue, truncating the value to the specified max length.
      String of​(jakarta.json.JsonValue value)
      Create a serialized json representation of the supplied JsonValue, truncating the value to the specified max length.
    • Field Detail

      • max

        private final int max
      • generatorFactory

        private final jakarta.json.stream.JsonGeneratorFactory generatorFactory
    • Constructor Detail

      • Snippet

        public Snippet​(int max,
                       jakarta.json.stream.JsonGeneratorFactory generatorFactory)
        This is the preferred approach to using Snippet in any context where there is an existing JsonGeneratorFactory in scope.
        Parameters:
        max - the maximum length of the serialized json produced via of()
        generatorFactory - the JsonGeneratorFactory created by the user
    • Method Detail

      • of

        public String of​(jakarta.json.JsonValue value)
        Create a serialized json representation of the supplied JsonValue, truncating the value to the specified max length. Truncated text appears with a suffix of "..." This method is thread safe.
        Parameters:
        value - the JsonValue to be serialized as json text
        Returns:
        a potentially truncated json text
      • of

        public static Snippet of​(int max)
        This factory should be used only in static or other scenarios were there is no JsonGeneratorFactory instance in scope - ie external code.
        Parameters:
        max - the maximum length of the serialized json produced via of()
      • of

        public static String of​(int max,
                                jakarta.json.JsonValue value)
        Create a serialized json representation of the supplied JsonValue, truncating the value to the specified max length. Truncated text appears with a suffix of "..." This method is thread safe. Avoid using this method in any context where there already is a JsonGeneratorFactory instance in scope. For those scenarios use the constructor that accepts a JsonGeneratorFactory instead.
        Parameters:
        max - the maximum length of the serialized json text
        value - the JsonValue to be serialized as json text
        Returns:
        a potentially truncated json text