Class JsonParser
java.lang.Object
de.linusdev.data.parser.JsonParser
This class is used to parse
json-string to
when parsing json-string to
AbstractData to a json-string and json-string to SOData.
AbstractData to json-string can parse:
-
Boolean,Byte,Short,Integer,Long,Float,Double,String, -
Datable,Simplifiable - any primitive type array
-
arrays and
collectionsof all mentioned Classes -
mapswith values of all mentioned Classes. The keys will be converted to a string usingObjects.toString(Object).
json-string to
SOData can parse:
-
false/true to
Boolean(ignores case) -
null to
null(ignores case) -
Integer Numbers (1, 4, 5, ...) to
Long
-
Decimal Numbers (5.6, ...) to
Double
-
"strings" to
String -
Arrays ([...]) to
ArrayList<Object><Object> -
any other values are not supported and will most likely cause a
ParseException
when parsing json-string to
SOData:
-
an empty json-string, for example "" or " ", will be parsed to an empty
SOData -
a json-string that starts with a "[" (a json-array) will be parsed to an
SOData. The array will be accessible with thearrayWrapperKey. seesetArrayWrapperKey(String)andDEFAULT_ARRAY_WRAPPER_KEY
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final charstatic final intstatic final intstatic final intstatic final intstatic final Stringstatic final charstatic final Stringstatic final charstatic final charstatic final charstatic final intstatic final Stringstatic final intstatic final charstatic final intstatic final intstatic final intstatic final intstatic final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription@NotNull SODataparseReader(@NotNull Reader reader) parses the content of given reader to aSOData.
The reader should not be wrapped in aBufferedReader, as this method does this.
The reader will beclosedafter parsing finished.
If only an empty string is being read (for example "" or " "), an emptySODatawill be returned.@NotNull SODataparseStream(@NotNull InputStream stream) @NotNull SODataparseString(@NotNull String json) @NotNull JsonParsersetAllowComments(boolean allowComments, @Nullable de.linusdev.lutils.interfaces.TBiConsumer<@NotNull JsonParser, @NotNull String, ?> commentConsumer) If enabled it will allow comments in the json.@NotNull JsonParsersetAllowNewLineInStrings(boolean allowNewLineInStrings) Default:true@NotNull JsonParsersetArrayWrapperKey(@NotNull String arrayWrapperKey) @NotNull JsonParsersetDataSupplier(@NotNull Supplier<SOData> dataSupplier) @NotNull JsonParsersetIdentifyNumberValues(boolean identifyNumberValues) If enabled it puts a single character (token) after a number, to identify which type of number it is.@NotNull JsonParserWhat to use as indent.
Default:"\t"voidwriteData(@NotNull Appendable writer, @Nullable AbstractData<?, ?> data) @NotNull StringwriteDataToString(@Nullable AbstractData<?, ?> data) @NotNull StringBuilderwriteDataToStringBuilder(@Nullable AbstractData<?, ?> data)
-
Field Details
-
CURLY_BRACKET_OPEN_CHAR
public static final int CURLY_BRACKET_OPEN_CHAR- See Also:
-
CURLY_BRACKET_CLOSE_CHAR
public static final int CURLY_BRACKET_CLOSE_CHAR- See Also:
-
SQUARE_BRACKET_OPEN_CHAR
public static final int SQUARE_BRACKET_OPEN_CHAR- See Also:
-
SQUARE_BRACKET_CLOSE_CHAR
public static final int SQUARE_BRACKET_CLOSE_CHAR- See Also:
-
QUOTE_CHAR
public static final int QUOTE_CHAR- See Also:
-
COLON_CHAR
public static final int COLON_CHAR- See Also:
-
NEW_LINE_CHAR
public static final int NEW_LINE_CHAR- See Also:
-
COMMA_CHAR
public static final int COMMA_CHAR- See Also:
-
SLASH_CHAR
public static final int SLASH_CHAR- See Also:
-
ASTERISK_CHAR
public static final int ASTERISK_CHAR- See Also:
-
SPACE_CHAR
public static final int SPACE_CHAR- See Also:
-
TRUE
- See Also:
-
FALSE
- See Also:
-
NULL
- See Also:
-
DEFAULT_ARRAY_WRAPPER_KEY
- See Also:
-
BYTE_TOKEN
public static final char BYTE_TOKEN- See Also:
-
SHORT_TOKEN
public static final char SHORT_TOKEN- See Also:
-
INTEGER_TOKEN
public static final char INTEGER_TOKEN- See Also:
-
LONG_TOKEN
public static final char LONG_TOKEN- See Also:
-
FLOAT_TOKEN
public static final char FLOAT_TOKEN- See Also:
-
DOUBLE_TOKEN
public static final char DOUBLE_TOKEN- See Also:
-
-
Constructor Details
-
JsonParser
public JsonParser()
-
-
Method Details
-
setIndent
@Contract("_ -> this") @NotNull public @NotNull JsonParser setIndent(@NotNull @NotNull String indent) What to use as indent.
Default:"\t"- Parameters:
indent-String
-
setDataSupplier
@Contract("_ -> this") @NotNull public @NotNull JsonParser setDataSupplier(@NotNull @NotNull Supplier<SOData> dataSupplier) -
setArrayWrapperKey
@Contract("_ -> this") @NotNull public @NotNull JsonParser setArrayWrapperKey(@NotNull @NotNull String arrayWrapperKey) If the json to read, does not start with a json-object, but instead with a json-array, the array will be available with this key in the returnedSOData.
Default: "array"- Parameters:
arrayWrapperKey- key to use when wrapping the array in aSOData
-
setAllowNewLineInStrings
@Contract("_ -> this") @NotNull public @NotNull JsonParser setAllowNewLineInStrings(boolean allowNewLineInStrings) Default:true- Parameters:
allowNewLineInStrings- whether to allow new lines in keys and string-values while reading
-
setIdentifyNumberValues
@Contract("_ -> this") @NotNull public @NotNull JsonParser setIdentifyNumberValues(boolean identifyNumberValues) If enabled it puts a single character (token) after a number, to identify which type of number it is. The tokens are the following:
- Byte: B
BYTE_TOKEN - Short: S
SHORT_TOKEN - Integer: I
INTEGER_TOKEN - Long: L
LONG_TOKEN - Float: F
FLOAT_TOKEN - Double: D
DOUBLE_TOKEN
false- Parameters:
identifyNumberValues-trueto enable the feature described above
- Byte: B
-
setAllowComments
@Contract("_, _ -> this") @NotNull public @NotNull JsonParser setAllowComments(boolean allowComments, @Nullable @Nullable de.linusdev.lutils.interfaces.TBiConsumer<@NotNull JsonParser, @NotNull String, ?> commentConsumer) If enabled it will allow comments in the json. E.g.:// This is a single line comment /* This is a multi line comment */- Parameters:
allowComments-trueto allow comments as desribed above
-
parseStream
@NotNull public @NotNull SOData parseStream(@NotNull @NotNull InputStream stream) throws IOException, de.linusdev.lutils.other.parser.ParseException parses the content of given stream to aSOData.
The stream will beclosedafter parsing finished.
If only an empty string is being read (for example "" or " "), an emptySODatawill be returned.- Parameters:
stream- the stream to read the json from- Returns:
- parsed
SOData - Throws:
IOException- while parsingde.linusdev.lutils.other.parser.ParseException- while parsing
-
parseReader
@NotNull public @NotNull SOData parseReader(@NotNull @NotNull Reader reader) throws IOException, de.linusdev.lutils.other.parser.ParseException parses the content of given reader to aSOData.
The reader should not be wrapped in aBufferedReader, as this method does this.
The reader will beclosedafter parsing finished.
If only an empty string is being read (for example "" or " "), an emptySODatawill be returned.- Parameters:
reader- the reader to read the json from- Returns:
- parsed
SOData - Throws:
IOException- while parsingde.linusdev.lutils.other.parser.ParseException- while parsing
-
parseString
@NotNull public @NotNull SOData parseString(@NotNull @NotNull String json) throws IOException, de.linusdev.lutils.other.parser.ParseException - Parameters:
json- the json to parse- Returns:
- parsed
SOData - Throws:
IOException- while parsingde.linusdev.lutils.other.parser.ParseException- while parsing
-
writeDataToString
- Parameters:
data-AbstractDatato write to aStringBuffer- Returns:
StringBuffer.toString()
-
writeDataToStringBuilder
@NotNull public @NotNull StringBuilder writeDataToStringBuilder(@Nullable @Nullable AbstractData<?, ?> data) - Parameters:
data-AbstractDatato write to aStringBuffer- Returns:
StringBuffer
-
writeData
public void writeData(@NotNull @NotNull Appendable writer, @Nullable @Nullable AbstractData<?, ?> data) throws IOException- Parameters:
writer-Writerto write todata-AbstractDatato write.nullwill write an empty Data: "{}"- Throws:
IOException-IOExceptionwhile writing
-