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 - any primitive type array
-
Object[] andCollectionof the before mentioned Classes
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 -
an 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
Fields -
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) voidsetAllowNewLineInStrings(boolean allowNewLineInStrings) Default:truevoidsetArrayWrapperKey(@NotNull String arrayWrapperKey) voidsetDataSupplier(@NotNull Supplier<SOData> dataSupplier) voidWhat 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:
-
TRUE
- See Also:
-
FALSE
- See Also:
-
NULL
- See Also:
-
DEFAULT_ARRAY_WRAPPER_KEY
- See Also:
-
-
Constructor Details
-
JsonParser
public JsonParser()
-
-
Method Details
-
setIndent
What to use as indent.
Default:"\t"- Parameters:
indent-String
-
setDataSupplier
-
setArrayWrapperKey
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
public void setAllowNewLineInStrings(boolean allowNewLineInStrings) Default:true- Parameters:
allowNewLineInStrings- whether to allow new lines in keys and string-values while reading
-
parseStream
@NotNull public @NotNull SOData parseStream(@NotNull @NotNull InputStream stream) throws IOException, 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 parsingParseException- while parsing
-
parseReader
@NotNull public @NotNull SOData parseReader(@NotNull @NotNull Reader reader) throws IOException, 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 parsingParseException- while parsing
-
writeDataToString
@NotNull public @NotNull String writeDataToString(@Nullable @Nullable AbstractData<?, ?> data) throws IOException- Parameters:
data-AbstractDatato write to aStringBuffer- Returns:
StringBuffer.toString()- Throws:
IOException-IOExceptionwhile writing
-
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:
data-AbstractDatato write.nullwill write an empty Data: "{}"writer-Writerto write to- Throws:
IOException-IOExceptionwhile writing
-