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

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

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, 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/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: 684 / 2140
Version history: 6 change(s)
Referenced in: [show references]

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