Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A naive parser for json string

License

NotificationsYou must be signed in to change notification settings

fanofxiaofeng/naive-json-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A naive parser forJSON string

Some questions

Before diving into this project, let's think over the following questions

  1. Is"true" a validJSON string? What about" \r\n\t true","3.14","\"\u0000\""? (and more)
  2. Withnode.js,JSON.parse("123456789123456789123456789") returns1.2345678912345679e+26, so doesJSON formatsupport arbitrary length integers?
  3. Withnode.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(...)q1.png

For question 2 and 3, what I tried can be seen as followsq2.png

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.

Usage

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.

Example 1: with standard input

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  }]

Example 2: with specified input file

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"    ]  }]

Code structure

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.

  1. Command line arguments handling (-f/--file option is supported)
  2. Read fromstdin or the specified file
  3. Parse as aJson instance
  4. Present thisJson instance

AJSON value can be any of the below items

  • object
  • array
  • string
  • number
  • "true" (i.e. literaltrue)
  • "false" (i.e. literalfalse)
  • "null" (i.e. literalnull)

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.

1. Fornull/false/true case

Fornull,false,true, they are special literal items.They are parsed by the below parsers respectively

  1. CaseNullParser
  2. CaseFalseParser
  3. CaseTrueParser

2. Fornumber case

Anumber has below three parts

  1. integer
  2. fraction
  3. 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.

3. Forstring case

Astring is composed by below three parts.

  1. A leading"
  2. characters
  3. A tailing"

String items are parsed byStringParser.AStringParser implements the logic for matchingleading/tailing" by itself,while it dispatches the logic of matchingcharacters toaCharactersParser.

4. Forarray case

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.

5. Forobject case

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.

TODO

  • 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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp