DEV Community

Will BL
Will BL

Posted on

Let's write a tiny JSON parser in Kotlin! Part 1: Characters

In the previous post, we looked at the elements that make up JSON. Now, lets start building something that can recognise them!

To start off with, we'll need to define three important building blocks of JSON: whitespace, digits, and hex digits.

Whitespace

A diagram showing the grammar for whitespace

You'll remember that whitespace is a string of zero or more spaces, line feeds (\n), carriage returns (\r), and horizontal tabs (\t).

To check if a character is a valid part of whitespace, we can define the following extension function:

fun Char.isWhitespace(): Boolean {
    return this == ' ' || this == '\r' || this == '\n' || this == '\t'
}
Enter fullscreen mode Exit fullscreen mode

which checks if the character is one of the characters allowed in whitespace. Simple!

Digits

Digits are even simpler: the Arabic numerals 0..9. We can check if a character is a valid digit with this:

fun Char.isDigit(): Boolean {
    return this in '0'..'9'
}
Enter fullscreen mode Exit fullscreen mode

This works because in ASCII (and therefore also in Unicode) the characters for the ten digits are all assigned consecutive numbers - so
this in '0'..'9' becomes '0' <= this && this <= '9', and '0' and '9' become their respective ASCII codepoints.

Hex Digits

fun Char.isHexDigit(): Boolean {
    return this in '0'..'9' || this in 'a'..'f' || this in 'A'..'F'
}
Enter fullscreen mode Exit fullscreen mode

This is pretty much the same as isDigit, but with extra checking for digits A through F. Remember to check for both upper and lower case!

Conclusion

All of these functions could also be written as expression functions - give that a go!

In the next post, we'll start work on our parser.

Top comments (0)