Skip to content

Instantly share code, notes, and snippets.

@TheKodeToad
Last active May 3, 2025 22:45
Show Gist options
  • Select an option

  • Save TheKodeToad/f01e41ab16abd5f06aad0d4ac6802ab1 to your computer and use it in GitHub Desktop.

Select an option

Save TheKodeToad/f01e41ab16abd5f06aad0d4ac6802ab1 to your computer and use it in GitHub Desktop.

Revisions

  1. TheKodeToad revised this gist Nov 4, 2022. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Single-file Java JSON Parser
    This is a version of [jfgjds](https://codeberg.org/TheKodeToad/jfgjds) modified to fit inside a single file. Its syntax is not as nice, but it is more portable.

    ```java
    JsonParser.parse("{\"a\":\"b\"}"); // returns Map{a=b}
    JsonParser.parse("[1, 2, 3]"); // returns List[Double(1), Double(2), Double(3)]
    JsonParser.parse("\"Hi, world!\""); // returns String "Hi, world!"
    ```
  2. TheKodeToad revised this gist Nov 4, 2022. No changes.
  3. TheKodeToad revised this gist Nov 4, 2022. 1 changed file with 115 additions and 119 deletions.
    234 changes: 115 additions & 119 deletions JsonParser.java
    Original file line number Diff line number Diff line change
    @@ -31,19 +31,19 @@ private JsonParser(Reader in) throws IOException {
    }

    private int character() {
    if(length == -1) {
    if (length == -1) {
    return -1;
    }

    return buffer[pos];
    }

    private int read() throws IOException {
    if(length == -1) {
    if (length == -1) {
    return -1;
    }

    if(buffer == null || pos++ == length - 1) {
    if (buffer == null || pos++ == length - 1) {
    pos = 0;
    buffer = new char[8192];
    length = in.read(buffer);
    @@ -53,19 +53,20 @@ private int read() throws IOException {
    }

    private void assertCharacter(char character) throws JsonParseException {
    if(character() != character) {
    throw new JsonParseException("Expected '" + character + "' but got " + (character() != -1 ? ("'" + (char) character() + "'") : "EOF"));
    if (character() != character) {
    throw new JsonParseException("Expected '" + character + "' but got "
    + (character() != -1 ? ("'" + (char) character() + "'") : "EOF"));
    }
    }

    private void assertNoEOF(String expected) throws JsonParseException {
    if(character() == -1) {
    if (character() == -1) {
    throw new JsonParseException("Expected " + expected + " but got EOF");
    }
    }

    private void skipWhitespace() throws IOException {
    while(isWhitespace()) {
    while (isWhitespace()) {
    read();
    }
    }
    @@ -77,11 +78,11 @@ private boolean isWhitespace() {
    private Object readSingleValue() throws IOException {
    skipWhitespace();
    Object result = readValue();
    if(!(result instanceof Double)) {
    if (!(result instanceof Double)) {
    read();
    }
    skipWhitespace();
    if(character() != -1) {
    if (character() != -1) {
    throw new JsonParseException("Found trailing non-whitespace characters");
    }
    return result;
    @@ -92,30 +93,30 @@ private Object readValue() throws IOException {

    int character = character();

    switch(character) {
    case '{':
    return readObject();
    case '[':
    return readArray();
    case '"':
    return readString();
    case 't':
    case 'f':
    // probably boolean
    Boolean bool = readBoolean();
    if(bool != null) {
    return bool;
    }
    break;
    case 'n':
    // probably null
    if(readNull()) {
    return null;
    }
    break;
    switch (character) {
    case '{':
    return readObject();
    case '[':
    return readArray();
    case '"':
    return readString();
    case 't':
    case 'f':
    // probably boolean
    Boolean bool = readBoolean();
    if (bool != null) {
    return bool;
    }
    break;
    case 'n':
    // probably null
    if (readNull()) {
    return null;
    }
    break;
    }

    if(character == '-' || isDigit()) {
    if (character == '-' || isDigit()) {
    // probably a number
    return readNumber();
    }
    @@ -131,8 +132,8 @@ private Map<String, Object> readObject() throws IOException {
    read();
    skipWhitespace();

    while(character() != '}') {
    if(comma) {
    while (character() != '}') {
    if (comma) {
    assertCharacter(',');
    read();
    skipWhitespace();
    @@ -148,7 +149,7 @@ private Map<String, Object> readObject() throws IOException {
    Object value = readValue();
    obj.put(key, value);

    if(!(value instanceof Double)) {
    if (!(value instanceof Double)) {
    read();
    }

    @@ -167,8 +168,8 @@ private List<Object> readArray() throws IOException {
    read();
    skipWhitespace();

    while(character() != ']') {
    if(comma) {
    while (character() != ']') {
    if (comma) {
    assertCharacter(',');
    read();
    skipWhitespace();
    @@ -177,7 +178,7 @@ private List<Object> readArray() throws IOException {
    Object value = readValue();
    array.add(value);

    if(!(value instanceof Double)) {
    if (!(value instanceof Double)) {
    read();
    }

    @@ -193,77 +194,75 @@ private String readString() throws IOException {

    StringBuilder result = new StringBuilder();

    while(read() != '"') {
    while (read() != '"') {
    int character = character();

    if(character >= '\u0000' && character <= '\u001F') {
    if (character >= '\u0000' && character <= '\u001F') {
    throw new JsonParseException("Found unescaped control character within string");
    }

    switch(character) {
    switch (character) {
    case -1:
    throw new JsonParseException("Expected '\"' but got EOF");
    case 0x7F:
    if (read() == '"') {
    return result.toString();
    }
    continue;
    case '\\':
    int seq = read();

    switch (seq) {
    case -1:
    throw new JsonParseException("Expected '\"' but got EOF");
    case 0x7F:
    if(read() == '"') {
    return result.toString();
    }
    continue;
    throw new JsonParseException("Expected an escape sequence but got EOF");
    case '\\':
    int seq = read();

    switch(seq) {
    case -1:
    throw new JsonParseException("Expected an escape sequence but got EOF");
    case '\\':
    break;
    case '/':
    case '\"':
    character = seq;
    break;
    case 'b':
    character = '\b';
    break;
    case 'f':
    character = '\f';
    break;
    case 'n':
    character = '\n';
    break;
    case 'r':
    character = '\r';
    break;
    case 't':
    character = '\t';
    break;
    case 'u':
    // char array to allow allocation in advance.
    char[] digits = new char[4];

    for(int index = 0; index < digits.length; index++) {
    character = read();
    if(index == 0 && character() == '-') {
    throw new JsonParseException("Hex sequence may not be negative");
    }
    else if(character() == -1) {
    throw new JsonParseException("Expected a hex sequence but got EOF");
    }
    digits[index] = (char) character;
    }

    String digitsString = new String(digits);

    try {
    character = Integer.parseInt(digitsString, 16);
    }
    catch(NumberFormatException error) {
    throw new JsonParseException("Could not parse hex sequence \"" + digitsString + "\"");
    }

    break;
    default:
    throw new JsonParseException("Invalid escape sequence: \\" + (char) seq);
    break;
    case '/':
    case '\"':
    character = seq;
    break;
    case 'b':
    character = '\b';
    break;
    case 'f':
    character = '\f';
    break;
    case 'n':
    character = '\n';
    break;
    case 'r':
    character = '\r';
    break;
    case 't':
    character = '\t';
    break;
    case 'u':
    // char array to allow allocation in advance.
    char[] digits = new char[4];

    for (int index = 0; index < digits.length; index++) {
    character = read();
    if (index == 0 && character() == '-') {
    throw new JsonParseException("Hex sequence may not be negative");
    } else if (character() == -1) {
    throw new JsonParseException("Expected a hex sequence but got EOF");
    }
    digits[index] = (char) character;
    }

    String digitsString = new String(digits);

    try {
    character = Integer.parseInt(digitsString, 16);
    } catch (NumberFormatException error) {
    throw new JsonParseException("Could not parse hex sequence \"" + digitsString + "\"");
    }

    break;
    default:
    throw new JsonParseException("Invalid escape sequence: \\" + (char) seq);
    }
    break;
    }

    result.append((char) character);
    @@ -279,59 +278,58 @@ private boolean isDigit() {
    private Double readNumber() throws IOException {
    StringBuilder result = new StringBuilder();

    if(character() == '-') {
    if (character() == '-') {
    result.append((char) character());
    read();
    }

    if(character() == '0') {
    if (character() == '0') {
    result.append((char) character());
    read();
    if(isDigit()) {
    if (isDigit()) {
    throw new JsonParseException("Found superfluous leading zero");
    }
    }
    else if(!isDigit()) {
    } else if (!isDigit()) {
    throw new JsonParseException("Expected digits");
    }

    while(character() != -1 && isDigit()) {
    while (character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }

    if(character() == '.') {
    if (character() == '.') {
    result.append('.');

    read();
    assertNoEOF("digits");

    if(!isDigit()) {
    if (!isDigit()) {
    throw new JsonParseException("Expected digits after decimal point");
    }

    while(character() != -1 && isDigit()) {
    while (character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }
    }

    if(character() == 'e' || character() == 'E') {
    if (character() == 'e' || character() == 'E') {
    result.append('E');

    read();
    assertNoEOF("digits");

    if(character() == '+' || character() == '-') {
    if (character() == '+' || character() == '-') {
    result.append((char) character());
    read();
    }

    if(!(character() == '+' || character() == '-' || isDigit())) {
    if (!(character() == '+' || character() == '-' || isDigit())) {
    throw new JsonParseException("Expected exponent digits");
    }

    while(character() != -1 && isDigit()) {
    while (character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }
    @@ -341,20 +339,18 @@ else if(!isDigit()) {

    try {
    return Double.parseDouble(resultStr);
    }
    catch(NumberFormatException error) {
    } catch (NumberFormatException error) {
    throw new JsonParseException("Failed to parse number '" + resultStr + "'");
    }
    }

    private Boolean readBoolean() throws IOException {
    if(character() == 't') {
    if(read() == 'r' && read() == 'u' && read() == 'e') {
    if (character() == 't') {
    if (read() == 'r' && read() == 'u' && read() == 'e') {
    return true;
    }
    }
    else if(character() == 'f') {
    if(read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') {
    } else if (character() == 'f') {
    if (read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') {
    return false;
    }
    }
  4. TheKodeToad revised this gist Nov 4, 2022. No changes.
  5. TheKodeToad revised this gist Nov 4, 2022. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions JsonParser.java
    Original file line number Diff line number Diff line change
    @@ -10,10 +10,7 @@
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    package io.toadlabs;

    import java.io.IOException;
    import java.io.Reader;
    import java.io.*;
    import java.util.*;

    public final class JsonParser {
    @@ -379,4 +376,3 @@ public JsonParseException(String message) {
    }

    }

  6. TheKodeToad created this gist Nov 4, 2022.
    382 changes: 382 additions & 0 deletions JsonParser.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,382 @@
    /*
    * MIT License
    *
    * Copyright (c) 2022 TheKodeToad
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

    package io.toadlabs;

    import java.io.IOException;
    import java.io.Reader;
    import java.util.*;

    public final class JsonParser {

    private final Reader in;
    private int pos;
    private int length;
    private char[] buffer;

    public static Object parse(Reader in) throws JsonParseException, IOException {
    return new JsonParser(in).readSingleValue();
    }

    private JsonParser(Reader in) throws IOException {
    this.in = in;
    pos = length = 0;
    read();
    }

    private int character() {
    if(length == -1) {
    return -1;
    }

    return buffer[pos];
    }

    private int read() throws IOException {
    if(length == -1) {
    return -1;
    }

    if(buffer == null || pos++ == length - 1) {
    pos = 0;
    buffer = new char[8192];
    length = in.read(buffer);
    }

    return character();
    }

    private void assertCharacter(char character) throws JsonParseException {
    if(character() != character) {
    throw new JsonParseException("Expected '" + character + "' but got " + (character() != -1 ? ("'" + (char) character() + "'") : "EOF"));
    }
    }

    private void assertNoEOF(String expected) throws JsonParseException {
    if(character() == -1) {
    throw new JsonParseException("Expected " + expected + " but got EOF");
    }
    }

    private void skipWhitespace() throws IOException {
    while(isWhitespace()) {
    read();
    }
    }

    private boolean isWhitespace() {
    return character() == ' ' || character() == '\n' || character() == '\r' || character() == '\t';
    }

    private Object readSingleValue() throws IOException {
    skipWhitespace();
    Object result = readValue();
    if(!(result instanceof Double)) {
    read();
    }
    skipWhitespace();
    if(character() != -1) {
    throw new JsonParseException("Found trailing non-whitespace characters");
    }
    return result;
    }

    private Object readValue() throws IOException {
    assertNoEOF("a value");

    int character = character();

    switch(character) {
    case '{':
    return readObject();
    case '[':
    return readArray();
    case '"':
    return readString();
    case 't':
    case 'f':
    // probably boolean
    Boolean bool = readBoolean();
    if(bool != null) {
    return bool;
    }
    break;
    case 'n':
    // probably null
    if(readNull()) {
    return null;
    }
    break;
    }

    if(character == '-' || isDigit()) {
    // probably a number
    return readNumber();
    }

    throw new JsonParseException("Expected a JSON value but got '" + (char) character + "'");
    }

    private Map<String, Object> readObject() throws IOException {
    assertCharacter('{');
    Map<String, Object> obj = new HashMap<>();
    boolean comma = false;

    read();
    skipWhitespace();

    while(character() != '}') {
    if(comma) {
    assertCharacter(',');
    read();
    skipWhitespace();
    }

    String key = readString();
    read();
    skipWhitespace();
    assertCharacter(':');
    read();
    skipWhitespace();

    Object value = readValue();
    obj.put(key, value);

    if(!(value instanceof Double)) {
    read();
    }

    skipWhitespace();
    comma = true;
    }

    return obj;
    }

    private List<Object> readArray() throws IOException {
    assertCharacter('[');
    List<Object> array = new ArrayList<>();
    boolean comma = false;

    read();
    skipWhitespace();

    while(character() != ']') {
    if(comma) {
    assertCharacter(',');
    read();
    skipWhitespace();
    }

    Object value = readValue();
    array.add(value);

    if(!(value instanceof Double)) {
    read();
    }

    skipWhitespace();
    comma = true;
    }

    return array;
    }

    private String readString() throws IOException {
    assertCharacter('"');

    StringBuilder result = new StringBuilder();

    while(read() != '"') {
    int character = character();

    if(character >= '\u0000' && character <= '\u001F') {
    throw new JsonParseException("Found unescaped control character within string");
    }

    switch(character) {
    case -1:
    throw new JsonParseException("Expected '\"' but got EOF");
    case 0x7F:
    if(read() == '"') {
    return result.toString();
    }
    continue;
    case '\\':
    int seq = read();

    switch(seq) {
    case -1:
    throw new JsonParseException("Expected an escape sequence but got EOF");
    case '\\':
    break;
    case '/':
    case '\"':
    character = seq;
    break;
    case 'b':
    character = '\b';
    break;
    case 'f':
    character = '\f';
    break;
    case 'n':
    character = '\n';
    break;
    case 'r':
    character = '\r';
    break;
    case 't':
    character = '\t';
    break;
    case 'u':
    // char array to allow allocation in advance.
    char[] digits = new char[4];

    for(int index = 0; index < digits.length; index++) {
    character = read();
    if(index == 0 && character() == '-') {
    throw new JsonParseException("Hex sequence may not be negative");
    }
    else if(character() == -1) {
    throw new JsonParseException("Expected a hex sequence but got EOF");
    }
    digits[index] = (char) character;
    }

    String digitsString = new String(digits);

    try {
    character = Integer.parseInt(digitsString, 16);
    }
    catch(NumberFormatException error) {
    throw new JsonParseException("Could not parse hex sequence \"" + digitsString + "\"");
    }

    break;
    default:
    throw new JsonParseException("Invalid escape sequence: \\" + (char) seq);
    }
    break;
    }

    result.append((char) character);
    }

    return result.toString();
    }

    private boolean isDigit() {
    return character() >= '0' && character() <= '9';
    }

    private Double readNumber() throws IOException {
    StringBuilder result = new StringBuilder();

    if(character() == '-') {
    result.append((char) character());
    read();
    }

    if(character() == '0') {
    result.append((char) character());
    read();
    if(isDigit()) {
    throw new JsonParseException("Found superfluous leading zero");
    }
    }
    else if(!isDigit()) {
    throw new JsonParseException("Expected digits");
    }

    while(character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }

    if(character() == '.') {
    result.append('.');

    read();
    assertNoEOF("digits");

    if(!isDigit()) {
    throw new JsonParseException("Expected digits after decimal point");
    }

    while(character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }
    }

    if(character() == 'e' || character() == 'E') {
    result.append('E');

    read();
    assertNoEOF("digits");

    if(character() == '+' || character() == '-') {
    result.append((char) character());
    read();
    }

    if(!(character() == '+' || character() == '-' || isDigit())) {
    throw new JsonParseException("Expected exponent digits");
    }

    while(character() != -1 && isDigit()) {
    result.append((char) character());
    read();
    }
    }

    String resultStr = result.toString();

    try {
    return Double.parseDouble(resultStr);
    }
    catch(NumberFormatException error) {
    throw new JsonParseException("Failed to parse number '" + resultStr + "'");
    }
    }

    private Boolean readBoolean() throws IOException {
    if(character() == 't') {
    if(read() == 'r' && read() == 'u' && read() == 'e') {
    return true;
    }
    }
    else if(character() == 'f') {
    if(read() == 'a' && read() == 'l' && read() == 's' && read() == 'e') {
    return false;
    }
    }
    return null;
    }

    private boolean readNull() throws IOException {
    return character() == 'n' && read() == 'u' && read() == 'l' && read() == 'l';
    }

    public class JsonParseException extends IOException {

    private static final long serialVersionUID = 1L;

    public JsonParseException(String message) {
    super(message);
    }

    }

    }