|
| 1 | +/* |
| 2 | + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package jdk.incubator.internal.util.json; |
| 27 | + |
| 28 | +import java.util.ArrayDeque; |
| 29 | +import java.util.Deque; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import jdk.incubator.java.util.json.JsonArray; |
| 34 | +import jdk.incubator.java.util.json.JsonObject; |
| 35 | +import jdk.incubator.java.util.json.JsonValue; |
| 36 | + |
| 37 | +/** |
| 38 | + * Generates JSON text for JsonValue, either for toString() or toDisplayString(). |
| 39 | + */ |
| 40 | +public final class JsonGenerator { |
| 41 | + |
| 42 | + private sealed interface StructureFrame permits ArrayFrame, ObjectFrame {} |
| 43 | + |
| 44 | + private static final class ArrayFrame implements StructureFrame { |
| 45 | + private final Iterator<JsonValue> elements; |
| 46 | + private final int depth; // For indentation |
| 47 | + private boolean first; // Whether iterator points to first value |
| 48 | + |
| 49 | + private ArrayFrame(Iterator<JsonValue> elements, int depth) { |
| 50 | + this.elements = elements; |
| 51 | + this.depth = depth; |
| 52 | + first = true; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static final class ObjectFrame implements StructureFrame { |
| 57 | + private final Iterator<Map.Entry<String, JsonValue>> members; |
| 58 | + private final int depth; // For indentation |
| 59 | + private boolean first; // Whether iterator points to first entry |
| 60 | + |
| 61 | + private ObjectFrame(Iterator<Map.Entry<String, JsonValue>> members, int depth) { |
| 62 | + this.members = members; |
| 63 | + this.depth = depth; |
| 64 | + first = true; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Generates JSON text for Json[Object|Array].toString() |
| 69 | + public static String toCompactString(JsonValue jv) { |
| 70 | + return generate(jv, "", false); |
| 71 | + } |
| 72 | + |
| 73 | + // Generates JSON text for Json.toDisplayString() |
| 74 | + public static String toDisplayString(JsonValue jv, String indent) { |
| 75 | + return generate(jv, indent, true); |
| 76 | + } |
| 77 | + |
| 78 | + private static String generate(JsonValue root, String indent, boolean isDisplay) { |
| 79 | + var sb = new StringBuilder(); |
| 80 | + Deque<StructureFrame> stack = new ArrayDeque<>(); |
| 81 | + enterValue(root, sb, stack, 0, isDisplay); |
| 82 | + |
| 83 | + while (!stack.isEmpty()) { |
| 84 | + switch (stack.peek()) { |
| 85 | + case ArrayFrame af -> { |
| 86 | + var elements = af.elements; |
| 87 | + if (elements.hasNext()) { |
| 88 | + if (af.first) { |
| 89 | + af.first = false; |
| 90 | + } else { |
| 91 | + sb.append(isDisplay ? ",\n" : ","); |
| 92 | + } |
| 93 | + if (isDisplay) { |
| 94 | + sb.repeat(indent, af.depth + 1); |
| 95 | + } |
| 96 | + enterValue(elements.next(), sb, stack, af.depth + 1, isDisplay); |
| 97 | + } else { |
| 98 | + if (isDisplay) { |
| 99 | + sb.append("\n"); |
| 100 | + sb.repeat(indent, af.depth); |
| 101 | + } |
| 102 | + sb.append("]"); |
| 103 | + stack.pop(); |
| 104 | + } |
| 105 | + } |
| 106 | + case ObjectFrame of -> { |
| 107 | + var members = of.members; |
| 108 | + if (members.hasNext()) { |
| 109 | + if (of.first) { |
| 110 | + of.first = false; |
| 111 | + } else { |
| 112 | + sb.append(isDisplay ? ",\n" : ","); |
| 113 | + } |
| 114 | + var entry = members.next(); |
| 115 | + if (isDisplay) { |
| 116 | + sb.repeat(indent, of.depth + 1); |
| 117 | + } |
| 118 | + sb.append('"') |
| 119 | + .append(Utils.escape(entry.getKey())) |
| 120 | + .append("\":") |
| 121 | + .append(isDisplay ? " " : ""); |
| 122 | + enterValue(entry.getValue(), sb, stack, of.depth + 1, isDisplay); |
| 123 | + } else { |
| 124 | + if (isDisplay) { |
| 125 | + sb.append("\n"); |
| 126 | + sb.repeat(indent, of.depth); |
| 127 | + } |
| 128 | + sb.append("}"); |
| 129 | + stack.pop(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return sb.toString(); |
| 135 | + } |
| 136 | + |
| 137 | + private static void enterValue(JsonValue jv, StringBuilder sb, Deque<StructureFrame> stack, |
| 138 | + int depth, boolean isDisplay) { |
| 139 | + switch (jv) { |
| 140 | + case JsonArray ja -> { |
| 141 | + var elements = ja.asList().iterator(); |
| 142 | + if (!elements.hasNext()) { |
| 143 | + sb.append("[]"); |
| 144 | + } else { |
| 145 | + sb.append(isDisplay ? "[\n" : "["); |
| 146 | + stack.push(new ArrayFrame(elements, depth)); |
| 147 | + } |
| 148 | + } |
| 149 | + case JsonObject jo -> { |
| 150 | + var members = jo.asMap().entrySet().iterator(); |
| 151 | + if (!members.hasNext()) { |
| 152 | + sb.append("{}"); |
| 153 | + } else { |
| 154 | + sb.append(isDisplay ? "{\n" : "{"); |
| 155 | + stack.push(new ObjectFrame(members, depth)); |
| 156 | + } |
| 157 | + } |
| 158 | + default -> sb.append(jv); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Instantiation is not allowed |
| 163 | + private JsonGenerator() {} |
| 164 | +} |
0 commit comments