Skip to content

JSON Parsing strategy

QuickWrite edited this page Sep 2, 2024 · 2 revisions

To parse the JSON data from a string into a Kotlin object the string content is being tokenized by the tokenizer and then parsed using a slightly modified LL(1) parser.

---
title: Parser Flow
---

graph LR;
    S("Source (String)")-->L;
    O("Source (Other)")-.- Convert -.->S;
    O-.->LT;

    subgraph LI[Lexer Interface]
    L(String Lexer);
    LT("(Source) Lexer");
    end

    LI-->P(Parser);
    P-->D(Data in Memory)
    
    style Convert fill:#e1e1e1,stroke:none
    style LT stroke:#808080,stroke-dasharray: 5 5
    style O stroke:#808080,stroke-dasharray: 5 5

Loading

Note

Everything that is currently dashed is not being completely implemented and needs some extra work.

How the parser works

The parser can be in different states, depending on which element it is currently working on.

START State

At the beginning of parsing, the parser will always be in the START state. In this state if a primitive token that can directly be converted into an object (boolean, number, string, null) it will directly stop and emit this element as it cannot parse anything else. If there are still tokens left (except the EOF-token) it will throw an error as the JSON is malformed.

If it encounters a { it will switch into the OBJECT state and if it encounters a [ it will switch into the ARRAY state.

---
title: START state
---

stateDiagram-v2
    direction LR
    [*] --> Token

    state Start {
        direction LR

        state "Read Token" as Token
        state Choice <<choice>>
        Token --> Choice

        state "Switch to OBJECT state" as Curly
        Choice --> Curly : Open Curly <br> Brace ('{')

        state "Switch to ARRAY state" as Square
        Choice --> Square : Open Square <br >Bracket ('[')

        Curly --> Continue
        Square --> Continue

        state "Emit element directly" as Direct
        Choice --> Direct : boolean, number, <br> string, null

        state "Throw error" as Error
        Direct --> Error
        Choice --> Error : Anything else

        classDef next fill:white
        classDef error fill:#f00,color:white,font-weight:bold,stroke-width:2px,stroke:#ff5555
        
        class Error error
        class Continue next
    }

    Direct --> [*]
Loading

OBJECT state

Important

This section is still TODO.

ARRAY state

Important

This section is still TODO.

Clone this wiki locally