-
Notifications
You must be signed in to change notification settings - Fork 0
JSON Parsing strategy
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
Note
Everything that is currently dashed is not being completely implemented and needs some extra work.
The parser can be in different states, depending on which element it is currently working on.
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 --> [*]
Important
This section is still TODO.
Important
This section is still TODO.
KotlinJsonParser is a toy project created for educational purposes. It is not intended for production use, and while efforts have been made to ensure the code is functional, it may not cover all edge cases or be optimized for performance. Use at your own risk.
But most importantly: Have fun! :D
The content of this wiki is licensed under the CC-BY-NC-SA 4.0 License.
