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

40
LINES

< > BotCompany Repo | #1012984 // jsonEncode_breakAtLevel1 - jsonEncode with line breaks

JavaX fragment (include)

1  
static S jsonEncode_breakAtLevel1(O o) {
2  
  new StringBuilder buf;
3  
  jsonEncode_breakAtLevel1(o, buf, 1);
4  
  ret str(buf);
5  
}
6  
7  
static void jsonEncode_breakAtLevel1(O o, StringBuilder buf, int level) {
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  
      if (level == 1) buf.append("\n  ");
21  
      buf.append(quote((S) key));
22  
      buf.append(":");
23  
      jsonEncode_breakAtLevel1(map.get(key), buf, level+1);
24  
    }
25  
    if (level == 1) buf.append("\n");
26  
    buf.append("}");
27  
  } else if (o instanceof Collection) {
28  
    Collection l = cast o;
29  
    buf.append("[");
30  
    bool first = true;
31  
    for (O element : l) {
32  
      if (first) first = false; else buf.append(",");
33  
      if (level == 1) buf.append("\n  ");
34  
      jsonEncode_breakAtLevel1(element, buf, level+1);
35  
    }
36  
    if (level == 1) buf.append("\n");
37  
    buf.append("]");
38  
  } else
39  
    fail("Unknown object for JSON encoding: " + className(o));
40  
}

Author comment

Began life as a copy of #1012410

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: #1012984
Snippet name: jsonEncode_breakAtLevel1 - jsonEncode with line breaks
Eternal ID of this version: #1012984/2
Text MD5: 203b8ff44ec043d1f91e08204d044031
Author: stefan
Category: javax / json
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-12-19 03:21:03
Source code size: 1238 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 417 / 450
Version history: 1 change(s)
Referenced in: [show references]