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).

1  
scope jsonDecode
2  
3  
sO jsonDecode(S text) {
4  
  ret new Y(text).parse();
5  
}
6  
7  
// the miraculous class Y
8  
sclass #Y {
9  
  S text;
10  
  LS tok;
11  
  bool useOrderedMaps;
12  
  int i = 1;
13  
14  
  *(S *text) {
15  
    tok = jsonTok(text);
16  
  }
17  
  
18  
  swappable RuntimeException fail(S msg) { ret main fail(msg); }
19  
  
20  
  O parse() {
21  
    if (l(tok) == 1) null;
22  
    ret parseExpr();
23  
  }
24  
  
25  
  O parseExpr() {
26  
    String t = tok.get(i);
27  
    if (t.startsWith("\"") || t.startsWith("'")) {
28  
      String s = unquote(tok.get(i));
29  
      i += 2;
30  
      return s;
31  
    }
32  
    if (t.equals("{"))
33  
      return parseMap();
34  
    if (t.equals("["))
35  
      return this.parseList(); // avoid loading standard function "parseList"
36  
    if (t.equals("null")) {
37  
      i += 2; return null;
38  
    }
39  
    if (t.equals("false")) {
40  
      i += 2; return false;
41  
    }
42  
    if (t.equals("true")) {
43  
      i += 2; return true;
44  
    }
45  
    bool minus = false;
46  
    if (t.equals("-")) {
47  
      minus = true;
48  
      i += 2;
49  
      t = get(tok, i);
50  
    }
51  
    if (isInteger(t)) {
52  
      int j = i;
53  
      i += 2;
54  
      if (eqOneOf(get(tok, i), ".", "e", "E")) {
55  
        // rough parsing for doubles
56  
        while (isInteger(get(tok, i))
57  
          || eqOneOf(get(tok, i), ".", "e", "E", "-"))
58  
          i += 2;
59  
        double d = parseDouble(joinSubList(tok, j, i-1));
60  
        if (minus) d = -d;
61  
        ret d;
62  
      } else {
63  
        long l = parseLong(t);
64  
        ret boxedIntOrLong(minus ? -l : l);
65  
      }
66  
    }
67  
    
68  
    fail("Unknown token " + (i+1) + ": " + t + ": " + text);
69  
  }
70  
  
71  
  Object parseList() {
72  
    consume("[");
73  
    List list = new ArrayList;
74  
    while (!tok.get(i).equals("]")) {
75  
      list.add(parseExpr());
76  
      if (tok.get(i).equals(",")) i += 2;
77  
    }
78  
    consume("]");
79  
    return list;
80  
  }
81  
  
82  
  Object parseMap() {
83  
    consume("{");
84  
    Map map = useOrderedMaps ? new LinkedHashMap : new TreeMap;
85  
    while (!tok.get(i).equals("}")) {
86  
      String key = unquote(tok.get(i));
87  
      i += 2;
88  
      consume(":");
89  
      Object value = parseExpr();
90  
      map.put(key, value);
91  
      if (tok.get(i).equals(",")) i += 2;
92  
    }
93  
    consume("}");
94  
    return map;
95  
  }
96  
  
97  
  void consume(String s) {
98  
    if (!tok.get(i).equals(s)) {
99  
      S prevToken = i-2 >= 0 ? tok.get(i-2) : "";
100  
      S nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
101  
      fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
102  
    }
103  
    i += 2;
104  
  }
105  
}

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: 1211 / 2981
Version history: 18 change(s)
Referenced in: [show references]