Libraryless. Click here for Pure Java version (3897L/22K).
sS jsonEncode_breakAtLevels(int levels default 5, O o) { new StringBuilder buf; jsonEncode_breakAtLevels(levels, o, buf, 1); ret str(buf); } static void jsonEncode_breakAtLevels(int levels, 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" + spaces(level*2)); buf.append(quote((S) key)); buf.append(":"); jsonEncode_breakAtLevels(levels, map.get(key), buf, level+1); } if (level <= levels) buf.append("\n" + spaces((level-1)*2)); 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 <= levels) buf.append("\n" + spaces(level*2)); jsonEncode_breakAtLevels(levels, element, buf, level+1); } if (level <= levels) buf.append("\n" + spaces((level-1)*2)); buf.append("]"); } else fail("Unknown object for JSON encoding: " + className(o)); }
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: | 231 / 343 |
Version history: | 4 change(s) |
Referenced in: | #1006654 - Standard functions list 2 (LIVE, continuation of #761) |