Libraryless. Click here for Pure Java version (3897L/22K).
1 | sS jsonEncode_breakAtLevels(int levels default 5, O o) { |
2 | new StringBuilder buf; |
3 | jsonEncode_breakAtLevels(levels, o, buf, 1); |
4 | ret str(buf); |
5 | } |
6 | |
7 | static void jsonEncode_breakAtLevels(int levels, 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" + spaces(level*2)); |
21 | buf.append(quote((S) key)); |
22 | buf.append(":"); |
23 | jsonEncode_breakAtLevels(levels, map.get(key), buf, level+1); |
24 | } |
25 | if (level <= levels) buf.append("\n" + spaces((level-1)*2)); |
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 <= levels) buf.append("\n" + spaces(level*2)); |
34 | jsonEncode_breakAtLevels(levels, element, buf, level+1); |
35 | } |
36 | if (level <= levels) buf.append("\n" + spaces((level-1)*2)); |
37 | buf.append("]"); |
38 | } else |
39 | fail("Unknown object for JSON encoding: " + className(o)); |
40 | } |
Began life as a copy of #1012984
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1029847 |
Snippet name: | jsonEncode_breakAtLevels - jsonEncode with line breaks in multiple top levels |
Eternal ID of this version: | #1029847/5 |
Text MD5: | 65a4a643530320d0cd2a229c063a1563 |
Transpilation MD5: | a61e145b02e79c61115a92d5a734c539 |
Author: | stefan |
Category: | javax / json |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-09-23 00:30:47 |
Source code size: | 1381 bytes / 40 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 230 / 343 |
Version history: | 4 change(s) |
Referenced in: | [show references] |