- Notifications
You must be signed in to change notification settings - Fork0
A naive parser for json string
License
fanofxiaofeng/naive-json-parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A naive parser forJSON
string
Before diving into this project, let's think over the following questions
- Is
"true"
a validJSON
string? What about" \r\n\t true"
,"3.14"
,"\"\u0000\""
? (and more) - With
node.js
,JSON.parse("123456789123456789123456789")
returns1.2345678912345679e+26
, so doesJSON
formatsupport arbitrary length integers? - With
node.js
, bothJSON.parse('"\\/"')
andJSON.parse('"/"')
run successfully, doesJSON
format support both"\\/"
and"/"
?
For question 1, all of"true"
," \r\n\t true"
,"3.14"
,"\"\u0000\""
are parsed successfully byJSON.parse(...)
For question 2 and 3, what I tried can be seen as follows
To get a good understanding ofJSON
format, I referred toIntroducing JSONand implemented a simpleJSON
parser in this project.It should be able to handle arbitrary lengthJSON
string (the max length ofjava
string is2147483647, pleasedon't let the input string exceed this length limit).If you find any bug, welcome to create anissue
, thanks.
Step 1: Build ajar
file with the following command
mvn clean package
Step 2: Use thisjar
file to parse and pretty printJSON
string from standard input or specified input file.Two examples are shown below.
echo' [ {"Message": "Hello, world", "Some special numbers": [4.2E1, 23E0, 3.14159265358979], "Today is Saturday" : true, "Needs to work": false, "Test for null": null}]'| \ java -jar target/naive-json-parser-1.0-SNAPSHOT-jar-with-dependencies.jar
The output is like this
[ {"Message":"Hello, world","Some special numbers": [4.2E1,23E0,3.14159265358979 ],"Today is Saturday":true,"Needs to work":false,"Test for null":null }]
echo'[ "Hello", 3.14, true, {"key1": ["value1", "value2"]} ]'> input.jsonjava -jar target/naive-json-parser-1.0-SNAPSHOT-jar-with-dependencies.jar -f input.json
The output is like this
["Hello",3.14,true, {"key1": ["value1","value2" ] }]
Let's take a look at themain
method incom.study.Main
class
publicstaticvoidmain(String[]args)throwsIOException {SimpleOptionHandleroptionHandler =newSimpleOptionHandler(args);try (InputStreaminputStream =optionHandler.isFileSpecified() ?newFileInputStream(optionHandler.getFileName()) :System.in) {byte[]bytes =inputStream.readAllBytes();Stringraw =newString(bytes,StandardCharsets.UTF_8);JsonParserjsonParser =newJsonParser();Jsonjson =jsonParser.parse(newPeekingIterator<>(raw.codePoints().iterator()));PresenterFacadepresenterFacade =newPresenterFacade();Stringresult =presenterFacade.convertToString(json);System.out.println(result); }}
There are 4 steps.
- Command line arguments handling (
-f/--file
option is supported) - Read from
stdin
or the specified file - Parse as a
Json
instance - Present this
Json
instance
AJSON
value can be any of the below items
- object
- array
- string
- number
- "true" (i.e. literal
true
) - "false" (i.e. literal
false
) - "null" (i.e. literal
null
)
The key idea for parsing the above seven items can be classified into five cases.Let me show these five cases from easier ones to complex ones.
Fornull
,false
,true
, they are special literal items.They are parsed by the below parsers respectively
Anumber
has below three parts
integer
fraction
exponent
Number
items are parsed byNumberParser.As there are three parts (fraction
andexponent
can be empty) within anumber
item,IntegerParser,FractionParser,ExponentParser are used to parse these three partsrespectively.
Astring
is composed by below three parts.
- A leading
"
characters
- A tailing
"
String
items are parsed byStringParser.AStringParser implements the logic for matchingleading/tailing"
by itself,while it dispatches the logic of matchingcharacters
toaCharactersParser.
Anarray
item comes in one of below two formats
'[' ws ']'
(case one)'[' elements ']'
(case two)
Array
items are parsed byArrayParser.To handlecase one,WhitespaceParser is used.To handlecase two,ElementsParser is used.
Anobject
item comes in one of below two formats
'{' ws '}'
(case one)'{' members '}'
(case two)
Object
items are parsed byObjectParser.To handlecase one,WhitespaceParser is used.To handlecase two,MembersParser is used.
- Use some library for command line arguments handling (e.g. arguments for output-file, indent-level-control)
- Use slf4j for logging
About
A naive parser for json string
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.