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

36
LINES

< > BotCompany Repo | #1012410 // jsonEncode

JavaX fragment (include)

1  
static S jsonEncode(O o) {
2  
  new StringBuilder buf;
3  
  jsonEncode(o, buf);
4  
  ret str(buf);
5  
}
6  
7  
static void jsonEncode(O o, StringBuilder buf) {
8  
  if (o == null)
9  
    buf.append("null");
10  
  else if (o instanceof S)
11  
    buf.append(quote((S) o));
12  
  else if (o instanceof Number || o instanceof Bool)
13  
    buf.append(o);
14  
  else if (o instanceof Map) {
15  
    Map map = cast o;
16  
    buf.append("{");
17  
    bool first = true;
18  
    for (O key : keys(map)) {
19  
      if (first) first = false; else buf.append(",");
20  
      buf.append(quote((S) key));
21  
      buf.append(":");
22  
      jsonEncode(map.get(key), buf);
23  
    }
24  
    buf.append("}");
25  
  } else if (o instanceof Collection) {
26  
    Collection l = cast o;
27  
    buf.append("[");
28  
    bool first = true;
29  
    for (O element : l) {
30  
      if (first) first = false; else buf.append(",");
31  
      jsonEncode(element, buf);
32  
    }
33  
    buf.append("]");
34  
  } else
35  
    fail("Unknown object for JSON encoding: " + className(o));
36  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lnbujpyubztb, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1012410
Snippet name: jsonEncode
Eternal ID of this version: #1012410/3
Text MD5: 315cc94319221cc9cf5e0228780fe38a
Author: stefan
Category: javax / json
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-02 12:21:08
Source code size: 972 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 432 / 484
Version history: 2 change(s)
Referenced in: [show references]