A naive parser for json
string
I referred to Introducing JSON
and implemented a very simple json
parser.
Step 1: Build a jar
file with the following command
mvn clean package
Step 2: Use this jar
file to parse and pretty print json string from standard input.
An example is 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
}
]
Let's take a look at the main
method in com.study.Main
class
public static void main(String[] args) throws IOException {
try (InputStream inputStream = System.in) {
byte[] bytes = inputStream.readAllBytes();
String raw = new String(bytes, StandardCharsets.UTF_8);
JsonParser jsonParser = new JsonParser();
Json json = jsonParser.parse(new PeekingIterator<>(raw.codePoints().iterator()));
PresenterFacade presenterFacade = new PresenterFacade();
String result = presenterFacade.convertToString(json);
System.out.println(result);
}
There are 3 main steps.
- Read from stdin
- Parse to a
Json
instance - Present this
Json
instance
todo