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).

1  
sS jsonEncode_extended(O o) {
2  
  new StringBuilder buf;
3  
  jsonEncode_extended(o, buf);
4  
  ret str(buf);
5  
}
6  
7  
svoid jsonEncode_extended(O o, StringBuilder buf) {
8  
  if (o == null)
9  
    buf.append("null");
10  
  else if (o cast JS)
11  
    buf.append(o!);
12  
  else if (o cast S)
13  
    buf.append(quote(o));
14  
  else if (o instanceof Number || o instanceof Bool)
15  
    buf.append(o);
16  
  else if (o instanceof Map) {
17  
    Map map = cast o;
18  
    buf.append("{");
19  
    bool first = true;
20  
    for (O key : keys(map)) {
21  
      if (first) first = false; else buf.append(",");
22  
      buf.append(quote((S) key));
23  
      buf.append(":");
24  
      jsonEncode_extended(map.get(key), buf);
25  
    }
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  
      jsonEncode_extended(element, buf);
34  
    }
35  
    buf.append("]");
36  
  } else
37  
    fail("Unknown object for JSON encoding: " + className(o));
38  
}

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: 148 / 214
Referenced in: [show references]