Package de.marhali.json5
Class Json5Primitive
- java.lang.Object
-
- de.marhali.json5.Json5Element
-
- de.marhali.json5.Json5Primitive
-
public final class Json5Primitive extends Json5Element
A class representing a Json5 primitive value. A primitive value is either a String, a Java primitive, or a Java primitive wrapper type.See the
Json5Elementdocumentation for details on how to convertJson5Primitiveand generally anyJson5Elementfrom and to Json5.
-
-
Field Summary
-
Fields inherited from class de.marhali.json5.Json5Element
comment
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Json5PrimitivedeepCopy()Returns the same value as primitives are immutable.booleanequals(java.lang.Object o)static Json5PrimitivefromBinaryString(java.lang.String binaryString)Create a primitive containing a binary number (radix base2).static Json5PrimitivefromBoolean(java.lang.Boolean bool)Create a primitive containing a boolean value.static Json5PrimitivefromCharacter(java.lang.Character c)Create a primitive containing a character.static Json5PrimitivefromHexString(java.lang.String hexString)Create a primitive containing a binary number (radix base16).static Json5PrimitivefromInstant(java.time.Instant instant)Create a primitive containing aInstantvalue.static Json5NullfromNull()Create a primitive containing anullvalue.static Json5PrimitivefromNumber(java.lang.Number number)Create a primitive containing a decimalNumber(radix base10).static Json5PrimitivefromNumber(java.lang.Number number, int radix)Creates a primitive containing aNumberwith specified radix base.static Json5PrimitivefromOctalString(java.lang.String octalString)Create a primitive containing an octal number (radix base8).static Json5PrimitivefromString(java.lang.String string)Create a primitive containing a String value.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.longgetAsLong()Convenience method to get this element as a primitive long.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.intgetNumberRadix()inthashCode()booleanisBinaryNumber()booleanisBoolean()Check whether this primitive contains a boolean value.booleanisHexNumber()booleanisInstant()Check whether this primitive contains aInstantvalue.booleanisNumber()Check whether this primitive contains a Number.booleanisOctalNumber()booleanisString()Check whether this primitive contains a String value.-
Methods inherited from class de.marhali.json5.Json5Element
getAsJson5Array, getAsJson5Null, getAsJson5Object, getAsJson5Primitive, getComment, hasComment, isJson5Array, isJson5Null, isJson5Object, isJson5Primitive, setComment, toString, toString
-
-
-
-
Method Detail
-
fromNull
public static Json5Null fromNull()
Create a primitive containing anullvalue.- Returns:
- New
Json5Nullvalue
-
fromBoolean
public static Json5Primitive fromBoolean(java.lang.Boolean bool)
Create a primitive containing a boolean value.- Parameters:
bool- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided
Booleanvalue
-
fromInstant
public static Json5Primitive fromInstant(java.time.Instant instant)
Create a primitive containing aInstantvalue.- Parameters:
instant- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided
InstantvalueThis is an extension that is not compliant to the official Json5 spec.
-
fromNumber
public static Json5Primitive fromNumber(java.lang.Number number, int radix)
Creates a primitive containing aNumberwith specified radix base. If a radix base of2,8or16is set, this method will ensure that the underlying number implementation is aBigInteger.- Parameters:
number- The numberradix- Radix base- Returns:
- Json5Primitive containing the provided
Numberwith specified radix base
-
fromNumber
public static Json5Primitive fromNumber(java.lang.Number number)
Create a primitive containing a decimalNumber(radix base10).- Parameters:
number- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided
Numberwith radix base10
-
fromBinaryString
public static Json5Primitive fromBinaryString(java.lang.String binaryString)
Create a primitive containing a binary number (radix base2). For example+0b1010...,0b1010...or-0b1010....This is an extension that is not compliant to the official Json5 spec.
- Parameters:
binaryString- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided binary number with radix base
2
-
fromOctalString
public static Json5Primitive fromOctalString(java.lang.String octalString)
Create a primitive containing an octal number (radix base8). For example+0o107...,0o107...or-0o107....This is an extension that is not compliant to the official Json5 spec.
- Parameters:
octalString- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided octal number with radix base
8
-
fromHexString
public static Json5Primitive fromHexString(java.lang.String hexString)
Create a primitive containing a binary number (radix base16). For example+0x09af...,0x09af...or-0x09af....- Parameters:
hexString- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided hex number with radix base
16
-
fromString
public static Json5Primitive fromString(java.lang.String string)
Create a primitive containing a String value.- Parameters:
string- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided
String
-
fromCharacter
public static Json5Primitive fromCharacter(java.lang.Character c)
Create a primitive containing a character. The character is turned into a one character String since Json5 only supports String.- Parameters:
c- the value to create the primitive with.- Returns:
- Json5Primitive containing the provided
Character
-
deepCopy
public Json5Primitive deepCopy()
Returns the same value as primitives are immutable.- Specified by:
deepCopyin classJson5Element- Returns:
- Deep copy
-
isBoolean
public boolean isBoolean()
Check whether this primitive contains a boolean value.- Returns:
- true if this primitive contains a boolean value, false otherwise.
-
getAsBoolean
public boolean getAsBoolean()
Convenience method to get this element as a boolean value. If this primitive is not a boolean, the string value is parsed usingBoolean.parseBoolean(String). This means"true"(ignoring case) is consideredtrueand any other value is consideredfalse.- Overrides:
getAsBooleanin classJson5Element- Returns:
- this element as a primitive boolean value.
-
isInstant
public boolean isInstant()
Check whether this primitive contains aInstantvalue.- Returns:
- true if this primitive contains a
Instantvalue, false otherwise.
-
getAsInstant
public java.time.Instant getAsInstant()
Description copied from class:Json5ElementConvenience method to get this element as aInstantvalue.- Overrides:
getAsInstantin classJson5Element- Returns:
- this element as a primitive
Instantvalue.
-
isNumber
public boolean isNumber()
Check whether this primitive contains a Number.- Returns:
- true if this primitive contains a Number, false otherwise.
-
getAsRadixNumber
public RadixNumber getAsRadixNumber()
Description copied from class:Json5ElementConvenience method to get this element as aRadixNumber.- Overrides:
getAsRadixNumberin classJson5Element- Returns:
- this element as a
RadixNumber.
-
getNumberRadix
public int getNumberRadix()
-
isBinaryNumber
public boolean isBinaryNumber()
-
isOctalNumber
public boolean isOctalNumber()
-
isHexNumber
public boolean isHexNumber()
-
getAsNumber
public java.lang.Number getAsNumber()
Convenience method to get this element as aNumber. If this primitive is a string, a lazily parsedNumberis constructed which parses the string when any of its methods are called (which can lead to aNumberFormatException).- Overrides:
getAsNumberin classJson5Element- Returns:
- this element as a
Number. - Throws:
java.lang.UnsupportedOperationException- if this primitive is neither a number nor a string.
-
getAsBinaryString
public java.lang.String getAsBinaryString()
Description copied from class:Json5ElementConvenience 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.
- Overrides:
getAsBinaryStringin classJson5Element- Returns:
- this element as a primitive binary number value string.
-
getAsOctalString
public java.lang.String getAsOctalString()
Description copied from class:Json5ElementConvenience 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.
- Overrides:
getAsOctalStringin classJson5Element- Returns:
- this element as a primitive octal number value string.
-
getAsHexString
public java.lang.String getAsHexString()
Description copied from class:Json5ElementConvenience method to get this element as a primitive hex number (radix base16) value.- Overrides:
getAsHexStringin classJson5Element- Returns:
- this element as a primitive hex number value string.
-
isString
public boolean isString()
Check whether this primitive contains a String value.- Returns:
- true if this primitive contains a String value, false otherwise.
-
getAsString
public java.lang.String getAsString()
Description copied from class:Json5ElementConvenience method to get this element as a string value.- Overrides:
getAsStringin classJson5Element- Returns:
- this element as a string value.
-
getAsDouble
public double getAsDouble()
Description copied from class:Json5ElementConvenience method to get this element as a primitive double value.- Overrides:
getAsDoublein classJson5Element- Returns:
- this element as a primitive double value.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid double.
-
getAsBigDecimal
public java.math.BigDecimal getAsBigDecimal()
Description copied from class:Json5ElementConvenience method to get this element as aBigDecimal.- Overrides:
getAsBigDecimalin classJson5Element- Returns:
- this element as a
BigDecimal. - Throws:
java.lang.NumberFormatException- if this element is not a validBigDecimal.
-
getAsBigInteger
public java.math.BigInteger getAsBigInteger()
Description copied from class:Json5ElementConvenience method to get this element as aBigInteger.- Overrides:
getAsBigIntegerin classJson5Element- Returns:
- this element as a
BigInteger. - Throws:
java.lang.NumberFormatException- if this element is not a validBigInteger.
-
getAsFloat
public float getAsFloat()
Description copied from class:Json5ElementConvenience method to get this element as a primitive float value.- Overrides:
getAsFloatin classJson5Element- Returns:
- this element as a primitive float value.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid float.
-
getAsLong
public long getAsLong()
Convenience method to get this element as a primitive long.- Overrides:
getAsLongin classJson5Element- Returns:
- this element as a primitive long.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid long.
-
getAsShort
public short getAsShort()
Description copied from class:Json5ElementConvenience method to get this element as a primitive short value.- Overrides:
getAsShortin classJson5Element- Returns:
- this element as a primitive short value.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid short.
-
getAsInt
public int getAsInt()
Description copied from class:Json5ElementConvenience method to get this element as a primitive integer value.- Overrides:
getAsIntin classJson5Element- Returns:
- this element as a primitive integer value.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid integer.
-
getAsByte
public byte getAsByte()
Description copied from class:Json5ElementConvenience method to get this element as a primitive byte value.- Overrides:
getAsBytein classJson5Element- Returns:
- this element as a primitive byte value.
- Throws:
java.lang.NumberFormatException- if the value contained is not a valid byte.
-
hashCode
public int hashCode()
- Overrides:
hashCodein classJson5Element
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classJson5Element
-
-