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

38
LINES

< > BotCompany Repo | #1032542 // jsonEncode_extended - allows JS instances to insert raw JavaScript

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3877L/22K).

sS jsonEncode_extended(O o) {
  new StringBuilder buf;
  jsonEncode_extended(o, buf);
  ret str(buf);
}

svoid jsonEncode_extended(O o, StringBuilder buf) {
  if (o == null)
    buf.append("null");
  else if (o cast JS)
    buf.append(o!);
  else if (o cast S)
    buf.append(quote(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_extended(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_extended(element, buf);
    }
    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 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1032542
Snippet name: jsonEncode_extended - allows JS instances to insert raw JavaScript
Eternal ID of this version: #1032542/1
Text MD5: 96746d64043f5ea439d42cc28e29ad9b
Transpilation MD5: 58a8cfb81270dd92d8db945c637301a6
Author: stefan
Category: javax / json
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-22 14:44:59
Source code size: 1039 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 146 / 211
Referenced in: [show references]