Learning JSON parsing

aj-json: a JSON parser made for learning

aj-json is a small project created to learn how JSON parsing works and to explore a small piece of compiler theory. It is not a production-ready JSON library. Treat it as readable study material: run it, change it, and follow the code from characters to Java objects.

For production systems, choose a maintained JSON library such as Jackson, Gson, or JSON-B. Do not use aj-json for untrusted input, compatibility-sensitive APIs, or data that must be handled without surprises.

What you can learn here

When a program receives JSON text, it needs to answer two simple questions:

  1. What does each piece of text mean? For example, { starts an object and true is a boolean.
  2. In what order may these pieces appear? For example, an object key must be followed by : and then a value.

aj-json separates those jobs. Lexer reads the characters and produces small labels called tokens. FMS (the state machine) reads those tokens in order and builds ordinary Java values.

Important limits

Start here

Add the dependency only if you are following the example or reading the source:

<dependency>
    <groupId>com.ajaxjs</groupId>
    <artifactId>aj-json</artifactId>
    <version>1.6</version>
</dependency>

Then continue to parse your first JSON. When you are ready to see the moving parts, read how the parser works.

Source and references