All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ipfs.api.JSONParser Maven / Gradle / Ivy

The newest version!
package io.ipfs.api;

import java.util.*;

import java.lang.reflect.*;

public class JSONParser
{
    private static char skipSpaces(String json, int[] pos)
    {
        while (true)
        {
            if (pos[0] >= json.length())
                return 0;
            char ch = json.charAt(pos[0]);
            if (Character.isWhitespace(ch))
                pos[0]++;
            else
                return ch;
        }
    }

    private static Boolean parseBoolean(String json, int[] pos)
    {
        if (json.regionMatches(pos[0], "true", 0, 4))
        {
            pos[0] += 4;
            return Boolean.TRUE;
        }

        if (json.regionMatches(pos[0], "false", 0, 5))
        {
            pos[0] += 5;
            return Boolean.FALSE;
        }

        return null;
    }

    private static Number parseNumber(String json, int[] pos)
    {
        int endPos = json.length();
        int startPos = pos[0];

        boolean foundExp = false;
        boolean foundDot = false;
        boolean allowPM = true;
        for (int i=startPos; i parseStream(String json)
    {
        if (json == null)
            return null;
        int[] pos = new int[1];
        List res = new ArrayList<>();
        json = json.trim();
        while (pos[0] < json.length())
            res.add(parse(json, pos));
        return res;
    }

    private static void escapeString(String s, StringBuffer buf)
    {
        buf.append('"');
        for (int i=0; i= 0)
            {
                if ((json != null) && (json instanceof List))
                    json = ((List) json).get(index);
                else
                    return null;
            }
        }

        return json;
    }
}