Class JsonPrinter
java.lang.Object
de.monticore.symboltable.serialization.JsonPrinter
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 Summary
FieldsModifier and TypeFieldDescriptionprotected Deque<JsonElement> static booleanstatic intstatic Stringprotected static booleanprotected Optional<JsonElement> protected static boolean -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for de.monticore.symboltable.serialization.JsonPrinter that does not print empty lists by default.JsonPrinter(boolean serializeDefaults) Constructor for de.monticore.symboltable.serialization.JsonPrinter -
Method Summary
Modifier and TypeMethodDescriptionvoid<T> voidarray(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.<T> voidarray(Collection<T> values, Function<T, String> printValue) Prints a collection of values as JSON array.voidPrints the beginning of a collection in Json notation.voidbeginArray(String kind) Prints the beginning of a collection in Json notation as member or the current object.voidPrints the begin of an object in Json notation.voidbeginObject(String kind) Prints the begin of an object in Json notation as member or the current object.voidResets everything printed so farstatic voidDisables the indentationstatic voidEnables the indentationvoidendArray()Prints the end of a collection in Json notation.voidPrints the end of an object in Json notation.protected StringescapeSpecialChars(String input) Adds escape sequences for all characters that are escaped in JsonReturns the current value of the Json code produced so far and performs basic checking for correct nestingsprotected JsonObjectprotected voidinternalNumberValue(String value) protected voidinternalStringValue(String value) static booleanstatic booleanvoidPrints a Json member with the given kind as key and the given boolean value, which is a basic data type in Json.voidPrints a Json member with the given kind as key and the given double value, which is a basic data type in Json.voidPrints a Json member with the given kind as key and the given float value, which is a basic data type in Json.voidPrints a Json member with the given kind as key and the given int value, which is a basic data type in Json.voidPrints a Json member with the given kind as key and the given long value, which is a basic data type in Json.voidmember(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.voidPrints a Json member with the given kind as key and the given String value, which is a basic data type in Json.voidmember(String kind, Collection<String> values) Prints a Json collection with the given kind as key and the given collection of object values.voidPrints a Json attribute with the given kind as key and the given optional object value.voidmemberJson(String kind, String value) Prints a Json member with the given kind as key and the given String value that is encoded in JSON.voidmemberJsonNoDef(String kind, String value) voidmemberNoDef(String kind, boolean value) voidmemberNoDef(String kind, double value) voidmemberNoDef(String kind, float value) voidmemberNoDef(String kind, int value) voidmemberNoDef(String kind, long value) voidmemberNoDef(String kind, String value) static voidsetSerializeDefaults(boolean serializeDefaults) toString()Returns the current value of the Json code produced so far.voidvalue(boolean value) Prints a boolean value as Json value.voidvalue(double value) Prints a double as Json value.voidvalue(float value) Prints a float as Json value.voidvalue(int value) Prints an int as Json value, if it deviates from the default boolean value or if * default values should be printed.voidvalue(long value) Prints a long as Json value.voidvalue(JsonPrinter value) Prints a Json attribute that is of an object type and therefore needs separat serialization.voidPrints a String as Json value.voidPrints a String that contains encoded Json.
-
Field Details
-
DEFAULT_BOOLEAN
public static boolean DEFAULT_BOOLEAN -
DEFAULT_NUMBER
public static int DEFAULT_NUMBER -
DEFAULT_STRING
-
enableIndentation
protected static boolean enableIndentation -
serializeDefaults
protected static boolean serializeDefaults -
currElements
-
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
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
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
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
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
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 attributevalues- The values of the Json attribute
-
member
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 attributevalue- The value of the Json attribute
-
member
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 attributevalue- The double value of the Json attribute
-
memberNoDef
-
member
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 attributevalue- The long value of the Json attribute
-
memberNoDef
-
member
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 attributevalue- The float value of the Json attribute
-
memberNoDef
-
member
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 attributevalue- The int value of the Json attribute
-
memberNoDef
-
member
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 attributevalue- The boolean value of the Json attribute
-
memberNoDef
-
member
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 attributevalue- The boolean value of the Json attribute
-
memberNoDef
-
memberJson
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
-
member
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 attributevalue- 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
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
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
Prints a Json attribute that is of an object type and therefore needs separat serialization.- Parameters:
value- The JsonPrinter of the value object
-
escapeSpecialChars
Adds escape sequences for all characters that are escaped in Json -
internalNumberValue
-
internalStringValue
-
getParentObject
-
addToArray
-
clearBuffer
public void clearBuffer()Resets everything printed so far -
getContent
Returns the current value of the Json code produced so far and performs basic checking for correct nestings -
toString
Returns the current value of the Json code produced so far.
-