-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathArrayEncoder.java
More file actions
222 lines (200 loc) · 8.77 KB
/
Copy pathArrayEncoder.java
File metadata and controls
222 lines (200 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package dev.toonformat.jtoon.encoder;
import dev.toonformat.jtoon.EncodeOptions;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.node.ArrayNode;
import tools.jackson.databind.node.ObjectNode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.StreamSupport;
import static dev.toonformat.jtoon.util.Constants.LIST_ITEM_PREFIX;
import static dev.toonformat.jtoon.util.Constants.SPACE;
/**
* Handles encoding of JSON arrays to TOON format.
* Orchestrates array encoding by detecting array types and delegating to specialized encoders.
*/
public final class ArrayEncoder {
private ArrayEncoder() {
throw new UnsupportedOperationException("Utility class cannot be instantiated");
}
/**
* Main entry point for array encoding.
* Detects array type and delegates to appropriate encoding method.
*
* @param key Optional key prefix
* @param value ArrayNode to encode
* @param writer LineWriter for output
* @param depth Indentation depth
* @param options Encoding options
*/
public static void encodeArray(final String key, final ArrayNode value,
final LineWriter writer, final int depth, final EncodeOptions options) {
if (value.isEmpty()) {
final String header = PrimitiveEncoder.formatHeader(0, key, null, options.delimiter().toString(),
options.lengthMarker());
writer.push(depth, header);
return;
}
// Primitive array
if (isArrayOfPrimitives(value)) {
encodeInlinePrimitiveArray(key, value, writer, depth, options);
return;
}
// Array of arrays (all primitives)
if (isArrayOfArrays(value)) {
final boolean allPrimitiveArrays = StreamSupport.stream(value.spliterator(), false)
.filter(JsonNode::isArray)
.allMatch(ArrayEncoder::isArrayOfPrimitives);
if (allPrimitiveArrays) {
encodeArrayOfArraysAsListItems(key, value, writer, depth, options);
return;
}
}
// Array of objects
if (isArrayOfObjects(value)) {
final List<String> header = TabularArrayEncoder.detectTabularHeader(value);
if (!header.isEmpty()) {
TabularArrayEncoder.encodeArrayOfObjectsAsTabular(key, value, header, writer, depth, options);
} else {
encodeMixedArrayAsListItems(key, value, writer, depth, options);
}
return;
}
// Mixed array: fallback to expanded format
encodeMixedArrayAsListItems(key, value, writer, depth, options);
}
/**
* Checks if an array contains only primitive values.
*
* @param array for testing that all items are primitives
* @return true if all items in the array are primitive values, false otherwise
*/
public static boolean isArrayOfPrimitives(final JsonNode array) {
if (!array.isArray()) {
return false;
}
for (JsonNode item : array) {
if (!item.isValueNode()) {
return false;
}
}
return true;
}
/**
* Checks if an array contains only arrays.
*
* @param array the array to check
* @return true if all items in the array are arrays, false otherwise
*/
static boolean isArrayOfArrays(final JsonNode array) {
if (!array.isArray()) {
return false;
}
for (JsonNode item : array) {
if (!item.isArray()) {
return false;
}
}
return true;
}
/**
* Checks if an array contains only objects.
*
* @param array the array to check
* @return true if all items in the array are objects, false otherwise
*/
public static boolean isArrayOfObjects(final JsonNode array) {
if (!array.isArray()) {
return false;
}
for (JsonNode item : array) {
if (!item.isObject()) {
return false;
}
}
return true;
}
/**
* Encodes a primitive array inline: key[N]: v1,v2,v3.
*/
private static void encodeInlinePrimitiveArray(final String prefix, final ArrayNode values,
final LineWriter writer, final int depth, final EncodeOptions options) {
final String formatted = formatInlineArray(values, options.delimiter().toString(), prefix,
options.lengthMarker());
writer.push(depth, formatted);
}
/**
* Formats an inline primitive array with header and values.
*
* @param values the array of primitive values to format
* @param delimiter the delimiter to use between values
* @param prefix optional key prefix for the array
* @param lengthMarker whether to include the # marker before the length
* @return the formatted inline array string
*/
public static String formatInlineArray(final ArrayNode values, final String delimiter,
final String prefix, final boolean lengthMarker) {
final List<JsonNode> valueList = new ArrayList<>();
values.forEach(valueList::add);
final String header = PrimitiveEncoder.formatHeader(values.size(), prefix, null, delimiter, lengthMarker);
final String joinedValue = PrimitiveEncoder.joinEncodedValues(valueList, delimiter);
// Only add space if there are values
if (values.isEmpty()) {
return header;
}
return header + SPACE + joinedValue;
}
/**
* Encodes an array of primitive arrays as list items.
*/
private static void encodeArrayOfArraysAsListItems(final String prefix, final ArrayNode values,
final LineWriter writer, final int depth, final EncodeOptions options) {
final String header = PrimitiveEncoder.formatHeader(values.size(), prefix, null,
options.delimiter().toString(), options.lengthMarker());
writer.push(depth, header);
for (JsonNode arr : values) {
if (arr.isArray() && isArrayOfPrimitives(arr)) {
final String inline = formatInlineArray((ArrayNode) arr, options.delimiter().toString(), null,
options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + inline);
}
}
}
/**
* Encodes a mixed array (non-uniform) as list items.
*/
private static void encodeMixedArrayAsListItems(final String prefix,
final ArrayNode items,
final LineWriter writer,
final int depth,
final EncodeOptions options) {
final String header = PrimitiveEncoder.formatHeader(items.size(), prefix, null,
options.delimiter().toString(), options.lengthMarker());
writer.push(depth, header);
for (JsonNode item : items) {
if (item.isValueNode()) {
// Direct primitive as list item
writer.push(depth + 1,
LIST_ITEM_PREFIX + PrimitiveEncoder.encodePrimitive(item, options.delimiter().toString()));
} else if (item.isArray()) {
// Direct array as list item
if (isArrayOfPrimitives(item)) {
final String inline = formatInlineArray((ArrayNode) item, options.delimiter().toString(), null,
options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + inline);
}
if (isArrayOfObjects(item)) {
final ArrayNode arrayItems = (ArrayNode) item;
final String nestedHeader = PrimitiveEncoder.formatHeader(arrayItems.size(), null, null,
options.delimiter().toString(),
options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + nestedHeader);
arrayItems.elements().forEach(e -> ListItemEncoder.encodeObjectAsListItem((ObjectNode) e, writer,
depth + 2, options));
}
} else if (item.isObject()) {
// Object as list item - delegate to ListItemEncoder
ListItemEncoder.encodeObjectAsListItem((ObjectNode) item, writer, depth + 1, options);
}
}
}
}