Class JsonPrinter

java.lang.Object
de.monticore.symboltable.serialization.JsonPrinter

public class JsonPrinter extends Object
Facade for the IndentPrinter that is capable of printing JSON syntax only. It hides details on the concrete syntax of Json and performs basic well-formedness checks on the produced Json.
  • Field Details

    • DEFAULT_BOOLEAN

      public static boolean DEFAULT_BOOLEAN
    • DEFAULT_NUMBER

      public static int DEFAULT_NUMBER
    • DEFAULT_STRING

      public static String DEFAULT_STRING
    • enableIndentation

      protected static boolean enableIndentation
    • serializeDefaults

      protected static boolean serializeDefaults
    • currElements

      protected Deque<JsonElement> currElements
    • root

      protected Optional<JsonElement> root
  • Constructor Details

    • JsonPrinter

      public JsonPrinter(boolean serializeDefaults)
      Constructor for de.monticore.symboltable.serialization.JsonPrinter
      Parameters:
      serializeDefaults -
    • JsonPrinter

      public JsonPrinter()
      Constructor for de.monticore.symboltable.serialization.JsonPrinter that does not print empty lists by default. This property can be enabled trough { @link #enableIndentation }
  • Method Details

    • isIndentationEnabled

      public static boolean isIndentationEnabled()
      Returns:
      enableIndentation
    • enableIndentation

      public static void enableIndentation()
      Enables the indentation
    • disableIndentation

      public static void disableIndentation()
      Disables the indentation
    • isSerializingDefaults

      public static boolean isSerializingDefaults()
      Returns:
      serializeDefaults
    • setSerializeDefaults

      public static void setSerializeDefaults(boolean serializeDefaults)
      Parameters:
      serializeDefaults -
    • beginObject

      public void beginObject()
      Prints the begin of an object in Json notation.
    • beginObject

      public void beginObject(String kind)
      Prints the begin of an object in Json notation as member or the current object.
    • endObject

      public void endObject()
      Prints the end of an object in Json notation.
    • beginArray

      public void beginArray()
      Prints the beginning of a collection in Json notation.
    • beginArray

      public void beginArray(String kind)
      Prints the beginning of a collection in Json notation as member or the current object.
    • endArray

      public void endArray()
      Prints the end of a collection in Json notation.
    • array

      public <T> void array(String name, Collection<T> values, Function<T,String> printValue)
      Prints a member with the passed name that is a collection of values as JSON array. The serialization of each value of the collection has to be passed as Function
      Type Parameters:
      T -
      Parameters:
      name -
      values -
      printValue -
    • array

      public <T> void array(Collection<T> values, Function<T,String> printValue)
      Prints a collection of values as JSON array. The serialization of each value of the collection has to be passed as Function
      Type Parameters:
      T -
      Parameters:
      values -
      printValue -
    • member

      public void member(String kind, Collection<String> values)
      Prints a Json collection with the given kind as key and the given collection of object values. Empty lists are serialized only, if serializeEmptyLists() is activated via the constructor. To serialize the passed objects, their toString() method is invoked. Complex objects should be serialized separately, before they are passed as parameter to this method!
      Parameters:
      kind - The key of the Json attribute
      values - The values of the Json attribute
    • member

      public void member(String kind, Optional<String> value)
      Prints a Json attribute with the given kind as key and the given optional object value. To serialize the passed object if it is present, its toString() method is invoked. Absent optionals are serialized only, if serializeEmptyLists() is activated via the constructor. Complex objects should be serialized separately, before they are passed as parameter to this method!
      Parameters:
      kind - The key of the Json attribute
      value - The value of the Json attribute
    • member

      public void member(String kind, double value)
      Prints a Json member with the given kind as key and the given double value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.
      Parameters:
      kind - The key of the Json attribute
      value - The double value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, double value)
    • member

      public void member(String kind, long value)
      Prints a Json member with the given kind as key and the given long value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.
      Parameters:
      kind - The key of the Json attribute
      value - The long value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, long value)
    • member

      public void member(String kind, float value)
      Prints a Json member with the given kind as key and the given float value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.
      Parameters:
      kind - The key of the Json attribute
      value - The float value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, float value)
    • member

      public void member(String kind, int value)
      Prints a Json member with the given kind as key and the given int value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.
      Parameters:
      kind - The key of the Json attribute
      value - The int value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, int value)
    • member

      public void member(String kind, boolean value)
      Prints a Json member with the given kind as key and the given boolean value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.
      Parameters:
      kind - The key of the Json attribute
      value - The boolean value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, boolean value)
    • member

      public void member(String kind, String value)
      Prints a Json member with the given kind as key and the given String value, which is a basic data type in Json. If the member value equals the default value and the serialization of default values is turned off, the member is not printed. NOTE: if the parameter value is a serialized String, use the member(String kind, JsonPrinter value) method or the memberJson(String kind, String value) method instead! Otherwise the content is escaped twice!
      Parameters:
      kind - The key of the Json attribute
      value - The boolean value of the Json attribute
    • memberNoDef

      public void memberNoDef(String kind, String value)
    • memberJson

      public void memberJson(String kind, String value)
      Prints a Json member with the given kind as key and the given String value that is encoded in JSON. If the member value equals the default value and the serialization of default values is turned off, the member is not printed.NOTE: if the parameter value is NOT a serialized String, use the member(String kind, String value) method instead! Otherwise the content is not escaped!
      Parameters:
      kind -
      value -
    • memberJsonNoDef

      public void memberJsonNoDef(String kind, String value)
    • member

      public void member(String kind, JsonPrinter value)
      Prints a Json member with the given kind as key and a Json value that is of an object type and therefore needs separat serialization.
      Parameters:
      kind - The key of the Json attribute
      value - The boolean value of the Json attribute
    • value

      public void value(double value)
      Prints a double as Json value.
      Parameters:
      value - The double value of the Json attribute
    • value

      public void value(long value)
      Prints a long as Json value.
      Parameters:
      value - The long value of the Json attribute
    • value

      public void value(float value)
      Prints a float as Json value.
      Parameters:
      value - The float value of the Json attribute
    • value

      public void value(int value)
      Prints an int as Json value, if it deviates from the default boolean value or if * default values should be printed.
      Parameters:
      value - The int value of the Json attribute
    • value

      public void value(boolean value)
      Prints a boolean value as Json value.
      Parameters:
      value - The boolean value of the Json attribute
    • value

      public void value(String value)
      Prints a String as Json value. NOTE: if the parameter value is a serialized String, use the value(JsonPrinter) method instead! Otherwise the content is escaped twice!
      Parameters:
      value - The String value of the Json attribute
    • valueJson

      public void valueJson(String value)
      Prints a String that contains encoded Json. NOTE: if the parameter value is NOT a serialized String, use the value(String) method instead! Otherwise the content is not escaped properly!
      Parameters:
      value - The String encoded in JSON
    • value

      public void value(JsonPrinter value)
      Prints a Json attribute that is of an object type and therefore needs separat serialization.
      Parameters:
      value - The JsonPrinter of the value object
    • escapeSpecialChars

      protected String escapeSpecialChars(String input)
      Adds escape sequences for all characters that are escaped in Json
    • internalNumberValue

      protected void internalNumberValue(String value)
    • internalStringValue

      protected void internalStringValue(String value)
    • getParentObject

      protected JsonObject getParentObject()
    • addToArray

      public void addToArray(JsonElement e)
    • clearBuffer

      public void clearBuffer()
      Resets everything printed so far
    • getContent

      public String getContent()
      Returns the current value of the Json code produced so far and performs basic checking for correct nestings
    • toString

      public String toString()
      Returns the current value of the Json code produced so far.
      Overrides:
      toString in class Object
      See Also: