Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser

So now we have an understanding of JSON and the components that make it up, but we have to create something that will read it – this will be our parser.

Our parser will step through our JSON file character by character to read it, so it’ll need to:


This content originally appeared on DEV Community and was authored by Will BL

So now we have an understanding of JSON and the components that make it up, but we have to create something that will read it - this will be our parser.

Our parser will step through our JSON file character by character to read it, so it'll need to:

  • remember our JSON
  • remember where in the JSON it's currently looking
  • know if it's at the end
  • read the current character
  • step to the next character

So let's do it!
First, we'll create a class which will take in our JSON as a CharSequence (we could use a String, too - the functionality is the same, but this lets you do something weird like throw a StringBuilder in there), and will remember where it's currently reading:

class Parser(private val input: CharSequence) {
    var cursor: Int = 0
}

Now let's give it the ability to read and step through the input.

class Parser(private val input: CharSequence) {
    var cursor: Int = 0

    private fun step(): Char {
        return input[cursor++]
    }

    private fun peek(): Char {
        return input[cursor]
    }

    private fun skip() {
        cursor++
    }
}

peek() will read the current character, skip(), will move the cursor forwards by one, and step() and read and then move on.

However, trying to read past the end will throw an exception, so let's add a method to check if we're at the end:

    private fun hasNext(): Boolean {
        return cursor < input.length
    }

This is everything we'll need for moving along our JSON text. Next, let's add some functions to actually read it:

fun read(predicate: (Char) -> Boolean): String {
    val result = StringBuilder()
    while (predicate(peek())) {
        result.append(step())
    }
    return result.toString()
}

fun read(pattern: CharSequence): String {
    val result = StringBuilder()
    var patternCursor = 0
    while (peek() == pattern[patternCursor++]) {
        result.append(skip())
    }
    return result.toString()
}

These two functions are vital to how the parser will do its job:

They read the JSON one character at a time, and store what they've read in a buffer, until the character they read reaches a certain condition. For the first function, that condition is given via a predicate, and in the second, the condition is that the buffer's contents match a given string. Once the condition is met, the buffer's contents are returned.

These five methods, simple as they seem, are extremely powerful, and all we need as a foundation for our parser!


This content originally appeared on DEV Community and was authored by Will BL


Print Share Comment Cite Upload Translate Updates
APA

Will BL | Sciencx (2021-05-06T21:01:19+00:00) Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser. Retrieved from https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/

MLA
" » Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser." Will BL | Sciencx - Thursday May 6, 2021, https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/
HARVARD
Will BL | Sciencx Thursday May 6, 2021 » Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser., viewed ,<https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/>
VANCOUVER
Will BL | Sciencx - » Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/
CHICAGO
" » Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser." Will BL | Sciencx - Accessed . https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/
IEEE
" » Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser." Will BL | Sciencx [Online]. Available: https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/. [Accessed: ]
rf:citation
» Let’s write a tiny JSON parser in Kotlin! Part 2: The Parser | Will BL | Sciencx | https://www.scien.cx/2021/05/06/lets-write-a-tiny-json-parser-in-kotlin-part-2-the-parser/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.