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

36
LINES

< > BotCompany Repo | #1012410 // jsonEncode

JavaX fragment (include)

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

static void jsonEncode(O o, StringBuilder buf) {
  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(",");
      buf.append(quote((S) key));
      buf.append(":");
      jsonEncode(map.get(key), buf);
    }
    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(",");
      jsonEncode(element, buf);
    }
    buf.append("]");
  } else
    fail("Unknown object for JSON encoding: " + className(o));
}

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: 431 / 483
Version history: 2 change(s)
Referenced in: [show references]