Package de.marhali.json5
Class Json5Element
- java.lang.Object
-
- de.marhali.json5.Json5Element
-
- Direct Known Subclasses:
Json5Array,Json5Null,Json5Object,Json5Primitive
public abstract class Json5Element extends java.lang.ObjectA class representing an element of Json5. It could either be aJson5Object, aJson5Array, aJson5Primitiveor aJson5Null.This class provides multiple
getAsmethods which allow- obtaining the represented primitive value, for example
getAsString() - casting to the
Json5Elementsubclasses in a convenient way, for examplegetAsJson5Object()
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.StringcommentAssociated comment on this element.
-
Constructor Summary
Constructors Constructor Description Json5Element()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Json5ElementdeepCopy()Returns a deep copy of this element.booleanequals(java.lang.Object o)java.math.BigDecimalgetAsBigDecimal()Convenience method to get this element as aBigDecimal.java.math.BigIntegergetAsBigInteger()Convenience method to get this element as aBigInteger.java.lang.StringgetAsBinaryString()Convenience method to get this element as a primitive binary number (radix base2) value.booleangetAsBoolean()Convenience method to get this element as a boolean value.bytegetAsByte()Convenience method to get this element as a primitive byte value.doublegetAsDouble()Convenience method to get this element as a primitive double value.floatgetAsFloat()Convenience method to get this element as a primitive float value.java.lang.StringgetAsHexString()Convenience method to get this element as a primitive hex number (radix base16) value.java.time.InstantgetAsInstant()Convenience method to get this element as aInstantvalue.intgetAsInt()Convenience method to get this element as a primitive integer value.Json5ArraygetAsJson5Array()Convenience method to get this element as aJson5Array.Json5NullgetAsJson5Null()Convenience method to get this element as aJson5Null.Json5ObjectgetAsJson5Object()Convenience method to get this element as aJson5Object.Json5PrimitivegetAsJson5Primitive()Convenience method to get this element as aJson5Primitive.longgetAsLong()Convenience method to get this element as a primitive long value.java.lang.NumbergetAsNumber()Convenience method to get this element as aNumber.java.lang.StringgetAsOctalString()Convenience method to get this element as a primitive octal number (radix base8) value.RadixNumbergetAsRadixNumber()Convenience method to get this element as aRadixNumber.shortgetAsShort()Convenience method to get this element as a primitive short value.java.lang.StringgetAsString()Convenience method to get this element as a string value.java.lang.StringgetComment()Returns the associated comment on this element.booleanhasComment()Provides a check for verifying if this element has an associated comment.inthashCode()booleanisJson5Array()Provides a check for verifying if this element is a Json5 array or not.booleanisJson5Null()Provides a check for verifying if this element represents a null value or not.booleanisJson5Object()Provides a check for verifying if this element is a Json5 object or not.booleanisJson5Primitive()Provides a check for verifying if this element is a primitive or not.voidsetComment(java.lang.String comment)Updates the associated comment on this element.java.lang.StringtoString()Converts this element to a Json5 string.java.lang.StringtoString(Json5Options options)Converts this element to a Json5 string using the provided configuration options for formatting.
-
-
-
Method Detail
-
hasComment
public boolean hasComment()
Provides a check for verifying if this element has an associated comment.- Returns:
- true if this element has an associated comment, false otherwise.
-
getComment
public java.lang.String getComment()
Returns the associated comment on this element. Can benullif not set.- Returns:
- optional comment string
-
setComment
public void setComment(java.lang.String comment)
Updates the associated comment on this element. Supports multi-line comments with break-line control character.- Parameters:
comment- Comment to set. Can benullto omit.
-
deepCopy
public abstract Json5Element deepCopy()
Returns a deep copy of this element.- Returns:
- Deep copy
-
isJson5Array
public boolean isJson5Array()
Provides a check for verifying if this element is a Json5 array or not.- Returns:
- true if this element is of type
Json5Array, false otherwise.
-
isJson5Object
public boolean isJson5Object()
Provides a check for verifying if this element is a Json5 object or not.- Returns:
- true if this element is of type
Json5Object, false otherwise.
-
isJson5Primitive
public boolean isJson5Primitive()
Provides a check for verifying if this element is a primitive or not.- Returns:
- true if this element is of type
Json5Primitive, false otherwise.
-
isJson5Null
public boolean isJson5Null()
Provides a check for verifying if this element represents a null value or not.- Returns:
- true if this element is of type
Json5Null, false otherwise.
-
getAsJson5Object
public Json5Object getAsJson5Object()
Convenience method to get this element as aJson5Object. If this element is of some other type, anIllegalStateExceptionwill result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJson5Object()first.- Returns:
- this element as a
Json5Object. - Throws:
java.lang.IllegalStateException- if this element is of another type.
-
getAsJson5Array
public Json5Array getAsJson5Array()
Convenience method to get this element as aJson5Array. If this element is of some other type, anIllegalStateExceptionwill result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJson5Array()first.- Returns:
- this element as a
Json5Array. - Throws:
java.lang.IllegalStateException- if this element is of another type.
-
getAsJson5Primitive
public Json5Primitive getAsJson5Primitive()
Convenience method to get this element as aJson5Primitive. If this element is of some other type, anIllegalStateExceptionwill result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJson5Primitive()first.- Returns:
- this element as a
Json5Primitive. - Throws:
java.lang.IllegalStateException- if this element is of another type.
-
getAsJson5Null
public Json5Null getAsJson5Null()
Convenience method to get this element as aJson5Null. If this element is of some other type, anIllegalStateExceptionwill result. Hence it is best to use this method after ensuring that this element is of the desired type by callingisJson5Null()first.- Returns:
- this element as a
Json5Null. - Throws:
java.lang.IllegalStateException- if this element is of another type.
-
getAsBoolean
public boolean getAsBoolean()
Convenience method to get this element as a boolean value.- Returns:
- this element as a primitive boolean value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsInstant
public java.time.Instant getAsInstant()
Convenience method to get this element as aInstantvalue.- Returns:
- this element as a primitive
Instantvalue. - Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsNumber
public java.lang.Number getAsNumber()
Convenience method to get this element as aNumber.- Returns:
- this element as a
Number. - Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array, or cannot be converted to a number.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsRadixNumber
public RadixNumber getAsRadixNumber()
Convenience method to get this element as aRadixNumber.- Returns:
- this element as a
RadixNumber. - Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array, or cannot be converted to a radix number.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsString
public java.lang.String getAsString()
Convenience method to get this element as a string value.- Returns:
- this element as a string value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsDouble
public double getAsDouble()
Convenience method to get this element as a primitive double value.- Returns:
- this element as a primitive double value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid double.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsFloat
public float getAsFloat()
Convenience method to get this element as a primitive float value.- Returns:
- this element as a primitive float value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid float.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsLong
public long getAsLong()
Convenience method to get this element as a primitive long value.- Returns:
- this element as a primitive long value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid long.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsInt
public int getAsInt()
Convenience method to get this element as a primitive integer value.- Returns:
- this element as a primitive integer value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid integer.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsByte
public byte getAsByte()
Convenience method to get this element as a primitive byte value.- Returns:
- this element as a primitive byte value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid byte.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsBigDecimal
public java.math.BigDecimal getAsBigDecimal()
Convenience method to get this element as aBigDecimal.- Returns:
- this element as a
BigDecimal. - Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if this element is not a validBigDecimal.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsBigInteger
public java.math.BigInteger getAsBigInteger()
Convenience method to get this element as aBigInteger.- Returns:
- this element as a
BigInteger. - Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if this element is not a validBigInteger.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsShort
public short getAsShort()
Convenience method to get this element as a primitive short value.- Returns:
- this element as a primitive short value.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid short.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsBinaryString
public java.lang.String getAsBinaryString()
Convenience method to get this element as a primitive binary number (radix base2) value.This is an extension that is not compliant to the official Json5 spec.
- Returns:
- this element as a primitive binary number value string.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid short.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsOctalString
public java.lang.String getAsOctalString()
Convenience method to get this element as a primitive octal number (radix base8) value.This is an extension that is not compliant to the official Json5 spec.
- Returns:
- this element as a primitive octal number value string.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid short.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
getAsHexString
public java.lang.String getAsHexString()
Convenience method to get this element as a primitive hex number (radix base16) value.- Returns:
- this element as a primitive hex number value string.
- Throws:
java.lang.UnsupportedOperationException- if this element is not aJson5PrimitiveorJson5Array.java.lang.NumberFormatException- if the value contained is not a valid short.java.lang.IllegalStateException- if this element is of the typeJson5Arraybut contains more than a single element.
-
toString
public java.lang.String toString(Json5Options options)
Converts this element to a Json5 string using the provided configuration options for formatting.- Parameters:
options- Configuration options.- Returns:
- Json5 string representation of this element.
-
toString
public java.lang.String toString()
Converts this element to a Json5 string.For example:
Json5Object object = new Json5Object(); object.add("a", new Json5Null()); Json5Array array = new Json5Array(); array.add(1); object.add("b", array); String json = object.toString(); // json: {"a":null,"b":[1]}To get the contained String value (without enclosing
"and without escaping), usegetAsString()instead:Json5Primitive Json5Primitive = new Json5Primitive("with \" quote"); String json = Json5Primitive.toString(); // json: "with \" quote" String value = Json5Primitive.getAsString(); // value: with " quote- Overrides:
toStringin classjava.lang.Object- See Also:
toString(Json5Options)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-