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

105
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 (4010L/23K).

scope jsonDecode

sO jsonDecode(S text) {
  ret new Y(text).parse();
}

// the miraculous class Y
sclass #Y {
  S text;
  LS tok;
  bool useOrderedMaps;
  int i = 1;

  *(S *text) {
    tok = jsonTok(text);
  }
  
  swappable RuntimeException fail(S msg) { ret main fail(msg); }
  
  O parse() {
    if (l(tok) == 1) null;
    ret parseExpr();
  }
  
  O parseExpr() {
    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);
        ret boxedIntOrLong(minus ? -l : l);
      }
    }
    
    fail("Unknown token " + (i+1) + ": " + t + ": " + text);
  }
  
  Object parseList() {
    consume("[");
    List list = new ArrayList;
    while (!tok.get(i).equals("]")) {
      list.add(parseExpr());
      if (tok.get(i).equals(",")) i += 2;
    }
    consume("]");
    return list;
  }
  
  Object parseMap() {
    consume("{");
    Map map = useOrderedMaps ? new LinkedHashMap : new TreeMap;
    while (!tok.get(i).equals("}")) {
      String key = unquote(tok.get(i));
      i += 2;
      consume(":");
      Object value = parseExpr();
      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;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

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/19
Text MD5: 4990d6435ff1be87a0aa7f5129349783
Transpilation MD5: 0b9fabbd728483bd6d6837ca600e9941
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-01 23:27:14
Source code size: 2503 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 867 / 2531
Version history: 18 change(s)
Referenced in: [show references]