Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ build/
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/misc.xml
.idea/libraries/
*.iws
*.iml
Expand Down Expand Up @@ -40,4 +41,4 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store
4 changes: 0 additions & 4 deletions src/main/java/dev/toonformat/jtoon/Delimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ public enum Delimiter {
* Returns the string representation of this delimiter.
* @return the string value of this delimiter
*/
public String getValue() {
return value;
}

@Override
public String toString() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private static class Parser {
Parser(String toon, DecodeOptions options) {
this.lines = toon.split("\n", -1);
this.options = options;
this.delimiter = options.delimiter().getValue();
this.delimiter = options.delimiter().toString();
}

/**
Expand Down Expand Up @@ -1467,4 +1467,4 @@ private void validateIndentation(String line) {
}
}
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/dev/toonformat/jtoon/encoder/ArrayEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private ArrayEncoder() {
*/
public static void encodeArray(String key, ArrayNode value, LineWriter writer, int depth, EncodeOptions options) {
if (value.isEmpty()) {
String header = PrimitiveEncoder.formatHeader(0, key, null, options.delimiter().getValue(),
String header = PrimitiveEncoder.formatHeader(0, key, null, options.delimiter().toString(),
options.lengthMarker());
writer.push(depth, header);
return;
Expand Down Expand Up @@ -133,7 +133,7 @@ public static boolean isArrayOfObjects(JsonNode array) {
*/
private static void encodeInlinePrimitiveArray(String prefix, ArrayNode values, LineWriter writer, int depth,
EncodeOptions options) {
String formatted = formatInlineArray(values, options.delimiter().getValue(), prefix, options.lengthMarker());
String formatted = formatInlineArray(values, options.delimiter().toString(), prefix, options.lengthMarker());
writer.push(depth, formatted);
}

Expand Down Expand Up @@ -165,13 +165,13 @@ public static String formatInlineArray(ArrayNode values, String delimiter, Strin
*/
private static void encodeArrayOfArraysAsListItems(String prefix, ArrayNode values, LineWriter writer, int depth,
EncodeOptions options) {
String header = PrimitiveEncoder.formatHeader(values.size(), prefix, null, options.delimiter().getValue(),
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)) {
String inline = formatInlineArray((ArrayNode) arr, options.delimiter().getValue(), null,
String inline = formatInlineArray((ArrayNode) arr, options.delimiter().toString(), null,
options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + inline);
}
Expand All @@ -186,26 +186,26 @@ private static void encodeMixedArrayAsListItems(String prefix,
LineWriter writer,
int depth,
EncodeOptions options) {
String header = PrimitiveEncoder.formatHeader(items.size(), prefix, null, options.delimiter().getValue(),
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().getValue()));
LIST_ITEM_PREFIX + PrimitiveEncoder.encodePrimitive(item, options.delimiter().toString()));
} else if (item.isArray()) {
// Direct array as list item
if (isArrayOfPrimitives(item)) {
String inline = formatInlineArray((ArrayNode) item, options.delimiter().getValue(), null,
String inline = formatInlineArray((ArrayNode) item, options.delimiter().toString(), null,
options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + inline);
}
if (isArrayOfObjects(item)) {
ArrayNode arrayItems = (ArrayNode) item;
String nestedHeader = PrimitiveEncoder.formatHeader(arrayItems.size(), null, null,
options.delimiter().getValue(), options.lengthMarker());
options.delimiter().toString(), options.lengthMarker());
writer.push(depth + 1, LIST_ITEM_PREFIX + nestedHeader);

arrayItems.elements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private static void encodeFirstKeyValue(String key, JsonNode value, LineWriter w
private static void encodeFirstValueAsPrimitive(String encodedKey, JsonNode value, LineWriter writer, int depth,
EncodeOptions options) {
writer.push(depth, LIST_ITEM_PREFIX + encodedKey + COLON + SPACE
+ PrimitiveEncoder.encodePrimitive(value, options.delimiter().getValue()));
+ PrimitiveEncoder.encodePrimitive(value, options.delimiter().toString()));
}

private static void encodeFirstValueAsArray(String key, String encodedKey, ArrayNode arrayValue, LineWriter writer,
Expand All @@ -88,7 +88,7 @@ private static void encodeFirstValueAsArray(String key, String encodedKey, Array

private static void encodeFirstArrayAsPrimitives(String key, ArrayNode arrayValue, LineWriter writer, int depth,
EncodeOptions options) {
String formatted = ArrayEncoder.formatInlineArray(arrayValue, options.delimiter().getValue(), key,
String formatted = ArrayEncoder.formatInlineArray(arrayValue, options.delimiter().toString(), key,
options.lengthMarker());
writer.push(depth, LIST_ITEM_PREFIX + formatted);
}
Expand All @@ -98,7 +98,7 @@ private static void encodeFirstArrayAsObjects(String key, String encodedKey, Arr
List<String> header = TabularArrayEncoder.detectTabularHeader(arrayValue);
if (!header.isEmpty()) {
String headerStr = PrimitiveEncoder.formatHeader(arrayValue.size(), key, header,
options.delimiter().getValue(), options.lengthMarker());
options.delimiter().toString(), options.lengthMarker());
writer.push(depth, LIST_ITEM_PREFIX + headerStr);
// Write just the rows, header was already written above
TabularArrayEncoder.writeTabularRows(arrayValue, header, writer, depth + 2, options);
Expand All @@ -120,9 +120,9 @@ private static void encodeFirstArrayAsComplex(String encodedKey, ArrayNode array
for (JsonNode item : arrayValue) {
if (item.isValueNode()) {
writer.push(depth + 2, LIST_ITEM_PREFIX
+ PrimitiveEncoder.encodePrimitive(item, options.delimiter().getValue()));
+ PrimitiveEncoder.encodePrimitive(item, options.delimiter().toString()));
} else if (item.isArray() && ArrayEncoder.isArrayOfPrimitives(item)) {
String inline = ArrayEncoder.formatInlineArray((ArrayNode) item, options.delimiter().getValue(), null,
String inline = ArrayEncoder.formatInlineArray((ArrayNode) item, options.delimiter().toString(), null,
options.lengthMarker());
writer.push(depth + 2, LIST_ITEM_PREFIX + inline);
} else if (item.isObject()) {
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/dev/toonformat/jtoon/encoder/ObjectEncoder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.toonformat.jtoon.encoder;


import dev.toonformat.jtoon.EncodeOptions;
import tools.jackson.databind.JsonNode;
import tools.jackson.databind.node.ArrayNode;
Expand Down Expand Up @@ -44,16 +43,16 @@ public static void encodeObject(ObjectNode value, LineWriter writer, int depth,
if (depth == 0 && rootLiteralKeys != null) {
rootLiteralKeys.clear();
fields.stream()
.filter(e -> e.getKey().contains("."))
.map(Map.Entry::getKey)
.forEach(rootLiteralKeys::add);
.filter(e -> e.getKey().contains("."))
.map(Map.Entry::getKey)
.forEach(rootLiteralKeys::add);
}
int effectiveFlattenDepth = remainingDepth != null ? remainingDepth : options.flattenDepth();

//the siblings collision do not need the absolute path
Set<String> siblings = fields.stream()
.map(Map.Entry::getKey)
.collect(Collectors.toCollection(LinkedHashSet::new));
.map(Map.Entry::getKey)
.collect(Collectors.toCollection(LinkedHashSet::new));

for (Map.Entry<String, JsonNode> entry : fields) {
encodeKeyValuePair(entry.getKey(), entry.getValue(), writer, depth, options, siblings, rootLiteralKeys, pathPrefix, effectiveFlattenDepth, blockedKeys);
Expand Down Expand Up @@ -85,17 +84,20 @@ public static void encodeKeyValuePair(String key,
Integer flattenDepth,
Set<String> blockedKeys
) {
if (key == null) {
return;
}
String encodedKey = PrimitiveEncoder.encodeKey(key);
String currentPath = pathPrefix != null ? pathPrefix + "." + key : key;
int effectiveFlattenDepth = flattenDepth != null && flattenDepth > 0 ? flattenDepth : options.flattenDepth();
int remainingDepth = effectiveFlattenDepth - depth;

// Attempt key folding when enabled
if (options.flatten()
&& !siblings.isEmpty()
&& remainingDepth > 0
&& blockedKeys != null
&& !blockedKeys.contains(key)) {
&& !siblings.isEmpty()
&& remainingDepth > 0
&& blockedKeys != null
&& !blockedKeys.contains(key)) {
Flatten.FoldResult foldResult = Flatten.tryFoldKeyChain(key, value, siblings, rootLiteralKeys, pathPrefix, remainingDepth);
if (foldResult != null) {
options = flatten(key, foldResult, writer, depth, options, rootLiteralKeys, pathPrefix, blockedKeys, remainingDepth);
Expand All @@ -106,7 +108,7 @@ public static void encodeKeyValuePair(String key,
}

if (value.isValueNode()) {
writer.push(depth, encodedKey + COLON + SPACE + PrimitiveEncoder.encodePrimitive(value, options.delimiter().getValue()));
writer.push(depth, encodedKey + COLON + SPACE + PrimitiveEncoder.encodePrimitive(value, options.delimiter().toString()));
} else if (value.isArray()) {
ArrayEncoder.encodeArray(key, (ArrayNode) value, writer, depth, options);
} else if (value.isObject()) {
Expand All @@ -132,7 +134,8 @@ public static void encodeKeyValuePair(String key,
* @param remainingDepth the depth that remind to the limit
* @return EncodeOptions changes for Case 2
*/
private static EncodeOptions flatten(String key, Flatten.FoldResult foldResult, LineWriter writer, int depth, EncodeOptions options, Set<String> rootLiteralKeys, String pathPrefix, Set<String> blockedKeys, int remainingDepth) {
private static EncodeOptions flatten(String key, Flatten.FoldResult foldResult, LineWriter writer, int depth, EncodeOptions options, Set<String> rootLiteralKeys, String pathPrefix, Set<String> blockedKeys,
int remainingDepth) {
String foldedKey = foldResult.foldedKey();

// prevent second folding pass
Expand Down Expand Up @@ -175,10 +178,10 @@ private static void handleFullyFoldedLeaf(Flatten.FoldResult foldResult, LineWri
// Primitive
if (leaf.isValueNode()) {
writer.push(depth,
indentedLine(depth,
encodedFoldedKey + ": " +
PrimitiveEncoder.encodePrimitive(leaf, options.delimiter().getValue()),
options.indent()));
indentedLine(depth,
encodedFoldedKey + ": " +
PrimitiveEncoder.encodePrimitive(leaf, options.delimiter().toString()),
options.indent()));
return;
}

Expand All @@ -193,7 +196,7 @@ private static void handleFullyFoldedLeaf(Flatten.FoldResult foldResult, LineWri
writer.push(depth, indentedLine(depth, encodedFoldedKey + ":", options.indent()));
if (!leaf.isEmpty()) {
encodeObject((ObjectNode) leaf, writer, depth + 1, options,
null, null, null, null);
null, null, null, null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import static dev.toonformat.jtoon.util.Constants.*;

Expand Down Expand Up @@ -108,6 +109,7 @@ public static String encodeKey(String key) {
*/
public static String joinEncodedValues(List<JsonNode> values, String delimiter) {
return values.stream()
.filter(Objects::nonNull)
.map(v -> encodePrimitive(v, delimiter))
.reduce((a, b) -> a + delimiter + b)
.orElse("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tools.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
Expand All @@ -27,26 +28,26 @@ private TabularArrayEncoder() {
*/
public static List<String> detectTabularHeader(ArrayNode rows) {
if (rows.isEmpty()) {
return new ArrayList<>();
return Collections.emptyList();
}

JsonNode firstRow = rows.get(0);
if (!firstRow.isObject()) {
return new ArrayList<>();
return Collections.emptyList();
}

ObjectNode firstObj = (ObjectNode) firstRow;
List<String> firstKeys = new ArrayList<>(firstObj.propertyNames());

if (firstKeys.isEmpty()) {
return new ArrayList<>();
return Collections.emptyList();
}

if (isTabularArray(rows, firstKeys)) {
return firstKeys;
}

return new ArrayList<>();
return Collections.emptyList();
}

/**
Expand Down Expand Up @@ -92,7 +93,7 @@ private static boolean isTabularArray(ArrayNode rows, List<String> header) {
*/
public static void encodeArrayOfObjectsAsTabular(String prefix, ArrayNode rows, List<String> header,
LineWriter writer, int depth, EncodeOptions options) {
String headerStr = PrimitiveEncoder.formatHeader(rows.size(), prefix, header, options.delimiter().getValue(),
String headerStr = PrimitiveEncoder.formatHeader(rows.size(), prefix, header, options.delimiter().toString(),
options.lengthMarker());
writer.push(depth, headerStr);

Expand All @@ -112,12 +113,16 @@ public static void encodeArrayOfObjectsAsTabular(String prefix, ArrayNode rows,
public static void writeTabularRows(ArrayNode rows, List<String> header, LineWriter writer, int depth,
EncodeOptions options) {
for (JsonNode row : rows) {
//skip non-object rows
if (!row.isObject()) {
continue;
}
ObjectNode obj = (ObjectNode) row;
List<JsonNode> values = new ArrayList<>();
for (String key : header) {
values.add(obj.get(key));
}
String joinedValue = PrimitiveEncoder.joinEncodedValues(values, options.delimiter().getValue());
String joinedValue = PrimitiveEncoder.joinEncodedValues(values, options.delimiter().toString());
writer.push(depth, joinedValue);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private ValueEncoder() {
public static String encodeValue(JsonNode value, EncodeOptions options) {
// Handle primitive values directly
if (value.isValueNode()) {
return PrimitiveEncoder.encodePrimitive(value, options.delimiter().getValue());
return PrimitiveEncoder.encodePrimitive(value, options.delimiter().toString());
}

// Complex values need a LineWriter for indentation
Expand Down
Loading
Loading