Skip to content

Commit f864a39

Browse files
glhezlukas-krecan
authored andcommitted
fix #796 where order of object nodes attributes is not kept
1 parent 483249e commit f864a39

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

json-unit-core/src/main/java/net/javacrumbs/jsonunit/core/internal/Node.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
package net.javacrumbs.jsonunit.core.internal;
1717

1818
import static java.util.stream.Collectors.toUnmodifiableSet;
19+
import static java.util.stream.Collectors.collectingAndThen;
20+
import static java.util.stream.Collectors.toCollection;
1921
import static net.javacrumbs.jsonunit.core.internal.JsonUtils.prettyPrint;
2022

2123
import java.math.BigDecimal;
2224
import java.util.AbstractMap;
2325
import java.util.Collections;
2426
import java.util.Iterator;
27+
import java.util.LinkedHashSet;
2528
import java.util.LinkedList;
2629
import java.util.Set;
2730
import java.util.Spliterators;
@@ -233,10 +236,9 @@ class JsonMap extends AbstractMap<String, Object> implements NodeWrapper {
233236
public Set<Entry<String, Object>> entrySet() {
234237
if (entrySet == null) {
235238
Iterator<KeyValue> fields = wrappedNode.fields();
236-
entrySet = StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, 0), false)
237-
.map(keyValue -> new SimpleEntry<>(
238-
keyValue.getKey(), keyValue.getValue().getValue()))
239-
.collect(toUnmodifiableSet());
239+
entrySet = StreamSupport.stream(Spliterators.spliteratorUnknownSize(fields, java.util.Spliterator.ORDERED), false)
240+
.map(keyValue -> new SimpleEntry<>(keyValue.getKey(), keyValue.getValue().getValue()))
241+
.collect(collectingAndThen(toCollection(LinkedHashSet::new), Collections::unmodifiableSet));
240242
}
241243
return entrySet;
242244
}

tests/test-base/src/main/java/net/javacrumbs/jsonunit/test/base/AbstractAssertJTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ void objectShouldContainComplexValueError() {
270270
{"c":5}""");
271271
}
272272

273+
@Test
274+
void objectFieldsShouldBeKeptInOrder() {
275+
assertThatJson("{\"root\":{\"key3\": 3, \"key2\": 2, \"key1\": 1 }}").node("root")
276+
.isObject()
277+
.containsExactly(entry("key3", 3),
278+
entry("key2", 2),
279+
entry("key1", 1));
280+
}
281+
273282
@Test
274283
void objectDoesContainComplexValue() {
275284
assertThatJson("{\"a\":1, \"b\": {\"c\" :3}}")

0 commit comments

Comments
 (0)