Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

94
LINES

< > BotCompany Repo | #1000620 - jsonDecode function (super-simple JSON decoder in JavaX!)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2491L/16K).

static bool jsonDecode_useOrderedMaps = true;

static O jsonDecode(final String text) {
  final List<String> tok = jsonTok(text);
  if (l(tok) == 1) null;
  
  class Y {
    int i = 1;

    Object parse() {
      String t = tok.get(i);
      if (t.startsWith("\"") || t.startsWith("'")) {
        String s = unquote(tok.get(i));
        i += 2;
        return s;
      }
      if (t.equals("{"))
        return parseMap();
      if (t.equals("["))
        return this.parseList(); // avoid loading standard function "parseList"
      if (t.equals("null")) {
        i += 2; return null;
      }
      if (t.equals("false")) {
        i += 2; return false;
      }
      if (t.equals("true")) {
        i += 2; return true;
      }
      bool minus = false;
      if (t.equals("-")) {
        minus = true;
        i += 2;
        t = get(tok, i);
      }
      if (isInteger(t)) {
        int j = i;
        i += 2;
        if (eqOneOf(get(tok, i), ".", "e", "E")) {
          // rough parsing for doubles
          while (isInteger(get(tok, i))
            || eqOneOf(get(tok, i), ".", "e", "E", "-"))
            i += 2;
          double d = parseDouble(joinSubList(tok, j, i-1));
          if (minus) d = -d;
          ret d;
        } else {
          long l = parseLong(t);
          if (minus) l = -l;
          ret l != (int) l ? (O) new Long(l) : new Integer((int) l);
        }
      }
      
      throw new RuntimeException("Unknown token " + (i+1) + ": " + t + ": " + text);
    }
    
    Object parseList() {
      consume("[");
      List list = new ArrayList;
      while (!tok.get(i).equals("]")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("]");
      return list;
    }
    
    Object parseMap() {
      consume("{");
      Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap : new TreeMap;
      while (!tok.get(i).equals("}")) {
        String key = unquote(tok.get(i));
        i += 2;
        consume(":");
        Object value = parse();
        map.put(key, value);
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("}");
      return map;
    }
    
    void consume(String s) {
      if (!tok.get(i).equals(s)) {
        S prevToken = i-2 >= 0 ? tok.get(i-2) : "";
        S nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
        fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
      }
      i += 2;
    }
  }
  
  return new Y().parse();
}

download  show line numbers  debug dex   

Travelled to 11 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, tslmcundralx, tvejysmllsmz

Comments [hide]

ID Author/Program Comment Date
509 #1000604 (pitcher) 2015-08-18 19:07:10

add comment

Snippet ID: #1000620
Snippet name: jsonDecode function (super-simple JSON decoder in JavaX!)
Eternal ID of this version: #1000620/7
Text MD5: aef4e841635e20ebbc0604e5ea0a4f33
Transpilation MD5: 6b6a50305b0de1b1335068820985ec3a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-11 22:34:08
Source code size: 2598 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 643 / 2065
Version history: 6 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1000620 & http://1000620.tinybrain.de