1 | // actually it's now almost the same as jsonDecode :) |
2 | static Object unstructure(String text) {
|
3 | final L<S> tok = javaTok(text); |
4 | |
5 | class X {
|
6 | int i = 1; |
7 | |
8 | Object parse() {
|
9 | String t = tok.get(i); |
10 | if (t.startsWith("\"")) {
|
11 | String s = unquote(tok.get(i)); |
12 | i += 2; |
13 | return s; |
14 | } |
15 | if (t.equals("{"))
|
16 | return parseMap(); |
17 | if (t.equals("["))
|
18 | return parseList(); |
19 | if (t.equals("null")) {
|
20 | i += 2; return null; |
21 | } |
22 | if (t.equals("false")) {
|
23 | i += 2; return false; |
24 | } |
25 | if (t.equals("true")) {
|
26 | i += 2; return true; |
27 | } |
28 | if (isInteger(t)) {
|
29 | i += 2; return Long.parseLong(t); |
30 | } |
31 | if (isJavaIdentifier(t)) {
|
32 | Class c = findClass(t); |
33 | O o = nuObject(c); |
34 | i += 2; |
35 | if (i < tok.size() && tok.get(i).equals("(")) {
|
36 | consume("(");
|
37 | while (!tok.get(i).equals(")")) {
|
38 | // It's like parsing a map. |
39 | //Object key = parse(); |
40 | //if (tok.get(i).equals(")"))
|
41 | // key = onlyField(); |
42 | String key = unquote(tok.get(i)); |
43 | i += 2; |
44 | consume("=");
|
45 | Object value = parse(); |
46 | set(o, key, value); |
47 | if (tok.get(i).equals(",")) i += 2;
|
48 | } |
49 | consume(")");
|
50 | } |
51 | return o; |
52 | } |
53 | throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
|
54 | } |
55 | |
56 | Object parseList() {
|
57 | consume("[");
|
58 | List list = new ArrayList; |
59 | while (!tok.get(i).equals("]")) {
|
60 | list.add(parse()); |
61 | if (tok.get(i).equals(",")) i += 2;
|
62 | } |
63 | consume("]");
|
64 | return list; |
65 | } |
66 | |
67 | Object parseMap() {
|
68 | consume("{");
|
69 | Map map = new TreeMap; |
70 | while (!tok.get(i).equals("}")) {
|
71 | String key = unquote(tok.get(i)); |
72 | i += 2; |
73 | consume("=");
|
74 | Object value = parse(); |
75 | map.put(key, value); |
76 | if (tok.get(i).equals(",")) i += 2;
|
77 | } |
78 | consume("}");
|
79 | return map; |
80 | } |
81 | |
82 | void consume(String s) {
|
83 | if (!tok.get(i).equals(s)) {
|
84 | S prevToken = i-2 >= 0 ? tok.get(i-2) : ""; |
85 | S nextTokens = join(tok.subList(i, Math.min(i+4, tok.size()))); |
86 | fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
|
87 | } |
88 | i += 2; |
89 | } |
90 | } |
91 | |
92 | return new X().parse(); |
93 | } |
Began life as a copy of #1000620
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1001061 |
| Snippet name: | OLD unstructure (opposite of "structure") - restores data structures from a string |
| Eternal ID of this version: | #1001061/1 |
| Text MD5: | 51d37397a4d175a0854d1b93c59cd3b7 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2015-10-28 18:03:18 |
| Source code size: | 2506 bytes / 93 lines |
| Pitched / IR pitched: | No / Yes |
| Views / Downloads: | 904 / 1020 |
| Referenced in: | [show references] |