-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
946832f
commit baeb5b6
Showing
4 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Normalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package net.javacrumbs.jsonunit.core.internal; | ||
|
||
import static java.util.Comparator.comparing; | ||
|
||
import java.util.Iterator; | ||
import java.util.Spliterators; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
import net.javacrumbs.jsonunit.core.internal.Node.KeyValue; | ||
|
||
class Normalizer { | ||
|
||
private static int depth = 2; | ||
|
||
static String toNormalizedString(Node node) { | ||
StringBuilder sb = new StringBuilder(); | ||
normalize(node, sb, 0); | ||
return sb.toString(); | ||
} | ||
|
||
private static void normalize(Node node, StringBuilder sb, int indent) { | ||
switch (node.getNodeType()) { | ||
case OBJECT -> normalizeObject(node, sb, indent); | ||
case ARRAY -> normalizeArray(node, sb, indent); | ||
case STRING -> sb.append('\"').append(node).append('"'); | ||
default -> sb.append(node); | ||
} | ||
} | ||
|
||
private static void normalizeArray(Node node, StringBuilder sb, int indent) { | ||
sb.append("[\n"); | ||
Iterator<Node> elements = node.arrayElements(); | ||
while (elements.hasNext()) { | ||
var element = elements.next(); | ||
addIndent(sb, indent + depth); | ||
normalize(element, sb, indent + depth); | ||
if (elements.hasNext()) sb.append(","); | ||
sb.append('\n'); | ||
} | ||
addIndent(sb, indent); | ||
sb.append("]"); | ||
} | ||
|
||
private static void normalizeObject(Node node, StringBuilder sb, int indent) { | ||
sb.append("{\n"); | ||
Iterator<KeyValue> sortedValues = | ||
stream(node.fields()).sorted(comparing(KeyValue::getKey)).iterator(); | ||
while (sortedValues.hasNext()) { | ||
var keyValue = sortedValues.next(); | ||
addIndent(sb, indent + depth); | ||
sb.append('"').append(keyValue.getKey()).append("\": "); | ||
normalize(keyValue.getValue(), sb, indent + depth); | ||
if (sortedValues.hasNext()) sb.append(","); | ||
sb.append('\n'); | ||
} | ||
addIndent(sb, indent); | ||
sb.append("}"); | ||
} | ||
|
||
private static void addIndent(StringBuilder sb, int indent) { | ||
for (int i = 0; i < indent; i++) { | ||
sb.append(' '); | ||
} | ||
} | ||
|
||
private static <T> Stream<T> stream(Iterator<T> iterator) { | ||
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters