Class JSONValueNumber
- java.lang.Object
-
- com.github.tnakamot.json.value.JSONValue
-
- com.github.tnakamot.json.value.JSONValuePrimitive
-
- com.github.tnakamot.json.value.JSONValueNumber
-
public class JSONValueNumber extends JSONValuePrimitive
Represents one JSON 'number' value.According to RFC 8259 - 6. Numbers, JSON does not set limit for 'number' values. Very large or very precise numbers cannot be represented by Java primitive types. Therefore, this class internally holds a text representation of a JSON 'number' value.
Instances of this class are immutable.
-
-
Field Summary
Fields Modifier and Type Field Description static String
NUMBER_PATTERN
Regex pattern that exactly matches the "number" tokens defined in RFC 8259 - 6.
-
Constructor Summary
Constructors Constructor Description JSONValueNumber(double value)
Create an instance of a Java representation of a JSON number value from a Java double value.JSONValueNumber(long value)
Create an instance of a Java representation of a JSON number value from a Java long value.JSONValueNumber(JSONTokenNumber token)
Create an instance of a Java representation of a JSON number value from a JSON number token.JSONValueNumber(String text)
Create an instance of a Java representation of a JSON number value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canBeLong()
Returns if this token can be converted to a Java long value.boolean
equals(Object obj)
To best comply with JSON Schema this method tries to compare the value mathematically usingBigDecimal
.int
hashCode()
To best comply with JSON Schema this method tries to return the same hash for two JSONValueNumber which have mathematically have the same numeric value.String
text()
Return a JSON text representation of this JSON number value.double
toDouble()
This method returns a Java double value that this JSON number represents.long
toLong()
This method returns a Java long value that this token represents.String
toString()
@NotNull String
toTokenString()
Convert this JSON value toString
which can be saved as a JSON text to a file or transmitted to network.@NotNull String
toTokenString(String newline, String indent)
Convert this JSON value toString
which can be saved as a JSON text to a file or transmitted to network.-
Methods inherited from class com.github.tnakamot.json.value.JSONValuePrimitive
token
-
Methods inherited from class com.github.tnakamot.json.value.JSONValue
toTokenBytes, toTokenBytes, type
-
-
-
-
Field Detail
-
NUMBER_PATTERN
public static final String NUMBER_PATTERN
Regex pattern that exactly matches the "number" tokens defined in RFC 8259 - 6. Numbers.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JSONValueNumber
public JSONValueNumber(String text) throws NumberFormatException
Create an instance of a Java representation of a JSON number value.- Parameters:
text
- value represented by a JavaString
object of this JSON string value. Cannot be null.- Throws:
NumberFormatException
- when the given text does not fulfill the number format defined by RFC 8259 - 6. Numbers.
-
JSONValueNumber
public JSONValueNumber(long value)
Create an instance of a Java representation of a JSON number value from a Java long value.- Parameters:
value
- Java long value which represents this JSON number value
-
JSONValueNumber
public JSONValueNumber(double value) throws IllegalArgumentException
Create an instance of a Java representation of a JSON number value from a Java double value.- Parameters:
value
- Java long value which represents this JSON number value- Throws:
IllegalArgumentException
- if the given value is NaN, +Inf or -Inf
-
JSONValueNumber
public JSONValueNumber(JSONTokenNumber token)
Create an instance of a Java representation of a JSON number value from a JSON number token.- Parameters:
token
- source token of this JSON string value.
-
-
Method Detail
-
text
public String text()
Return a JSON text representation of this JSON number value.The returned text is not normalized. For example, there are many JSON texts that represent one thousand; 1000, 1.0e3, 1000.00. This method returns one of valid JSON text representations. There is no guarantee that this method returns the same text representation for the same mathematical value.
Returned text always complies with RFC 8259 - 6. Numbers.
- Returns:
- text which represents this JSON number value.
-
toDouble
public double toDouble()
This method returns a Java double value that this JSON number represents.Note that not all valid values of JSON "number" primitives can be precisely represented by a Java double value. For example, JSON "number" primitive allows very large number like 1E400, but Java double treats such large value as
Double.POSITIVE_INFINITY
. Or, some fractions may be simply ignored. Therefore, the application programmer who calls this method must not assume that the returned double value precisely represents the original number in the JSON text.This method internally calls
Double.parseDouble(String)
. Too big values like "1e309" is converted toDouble.POSITIVE_INFINITY
, for example. Too small values like "-2.4E-324" is converted to -0.0.- Returns:
- a Java double value that this token represents
-
toLong
public long toLong() throws NumberFormatException
This method returns a Java long value that this token represents.Note that not all valid values of JSON "number" primitives can be represented by a Java long value. For example, JSON "number" primitive allows a fraction, but Java long value does not accept a fraction. If this JSON "number" primitive value cannot be converted to a Java long value, this method raises
NumberFormatException
.- Returns:
- a Java long value that this token represents.
- Throws:
NumberFormatException
- if the value cannot be converted to a Java long value
-
canBeLong
public boolean canBeLong()
Returns if this token can be converted to a Java long value. If this method returns true,toLong()
can be executed without an exception.- Returns:
- whether this token can be converted to a Java long value.
-
hashCode
public int hashCode()
To best comply with JSON Schema this method tries to return the same hash for two JSONValueNumber which have mathematically have the same numeric value. For example, 10e9 and 1e10 returns the same hash.However, big values which
BigDecimal
cannot handle may or may not return the same hash.
-
equals
public boolean equals(Object obj)
To best comply with JSON Schema this method tries to compare the value mathematically usingBigDecimal
. However, big values whichBigDecimal
cannot handle will not be mathematically evaluated. For example, 10e10000000000 and 1e10000000001 are mathematically equal, but this method considers that they are different.
-
toTokenString
@NotNull public @NotNull String toTokenString()
Description copied from class:JSONValue
Convert this JSON value toString
which can be saved as a JSON text to a file or transmitted to network. The output text is optimized for machine, so no indent or new lines will be inserted.Note that, according to RFC 8259 - 8.1 Character Encoding, your application program must encode the returned String using UTF-8 without BOM before writing the returned
String
to a file or a network stream, or network datagram(s). It is caller's responsibility to correctly encode it.- Specified by:
toTokenString
in classJSONValue
- Returns:
- a string representation of this JSON value
-
toTokenString
@NotNull public @NotNull String toTokenString(String newline, String indent)
Description copied from class:JSONValue
Convert this JSON value toString
which can be saved as a JSON text to a file or transmitted to network. The output text is optimized for human. Indents and new line characters are inserted accordingly.Note that, according to RFC 8259 - 8.1 Character Encoding, your application program must encode the returned String using UTF-8 without BOM before writing the returned
String
to a file or a network stream, or network datagram(s). It is caller's responsibility to correctly encode it.- Specified by:
toTokenString
in classJSONValue
- Parameters:
newline
- line separator; "\r", "\n" or "\r\n"indent
- indent string. Must consist of " " and "\t".- Returns:
- a string representation of this JSON value
-
-