-
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 18838a3
Showing
4 changed files
with
69 additions
and
1 deletion.
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
49 changes: 49 additions & 0 deletions
49
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,49 @@ | ||
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 -> { | ||
addIndent(sb, indent); | ||
sb.append("{\n"); | ||
stream(node.fields()).sorted(comparing(KeyValue::getKey)).forEach(keyValue -> { | ||
addIndent(sb, indent + depth); | ||
sb.append('"').append(keyValue.getKey()).append("\": "); | ||
normalize(keyValue.getValue(), sb, indent + depth); | ||
sb.append(",\n"); | ||
}); | ||
addIndent(sb, indent); | ||
sb.append("}"); | ||
} | ||
case STRING -> sb.append('\"').append(node).append('"'); | ||
default -> sb.append(node); | ||
} | ||
} | ||
|
||
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