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)

static S jsonEncode_breakAtLevel1(O o) {
  new StringBuilder buf;
  jsonEncode_breakAtLevel1(o, buf, 1);
  ret str(buf);
}

static void jsonEncode_breakAtLevel1(O o, StringBuilder buf, int level) {
  if (o == null)
    buf.append("null");
  else if (o instanceof S)
    buf.append(quote((S) o));
  else if (o instanceof Number || o instanceof Bool)
    buf.append(o);
  else if (o instanceof Map) {
    Map map = cast o;
    buf.append("{");
    bool first = true;
    for (O key : keys(map)) {
      if (first) first = false; else buf.append(",");
      if (level == 1) buf.append("\n  ");
      buf.append(quote((S) key));
      buf.append(":");
      jsonEncode_breakAtLevel1(map.get(key), buf, level+1);
    }
    if (level == 1) buf.append("\n");
    buf.append("}");
  } else if (o instanceof Collection) {
    Collection l = cast o;
    buf.append("[");
    bool first = true;
    for (O element : l) {
      if (first) first = false; else buf.append(",");
      if (level == 1) buf.append("\n  ");
      jsonEncode_breakAtLevel1(element, buf, level+1);
    }
    if (level == 1) buf.append("\n");
    buf.append("]");
  } else
    fail("Unknown object for JSON encoding: " + className(o));
}

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: 415 / 448
Version history: 1 change(s)
Referenced in: [show references]