Class Snippet

java.lang.Object
org.apache.johnzon.core.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 
    There are several buffers involved in the creation of a json string.
    private static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final jakarta.json.stream.JsonGeneratorFactory
     
    private final int
     
  • 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

    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.
    of(jakarta.json.JsonValue value)
    Create a serialized json representation of the supplied JsonValue, truncating the value to the specified max length.

    Methods inherited from class java.lang.Object

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

    • max

      private final int max
    • generatorFactory

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

    • 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 Details

    • 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