diff --git a/CHANGES b/CHANGES index f0a85f82c55..54aff94cda1 100644 --- a/CHANGES +++ b/CHANGES @@ -71,6 +71,7 @@ - API Fix: MathUtils.lerpAngle() fixed for extreme inputs - MathUtils trigonometry improvements - Various Scene2D fixes and improvements +- Fix: JsonValue#addChild now clears leftover list pointers, preventing inconsistent or looping JSON objects. [1.11.0] - [BREAKING CHANGE] iOS: Increased min supported iOS version to 9.0. Update your Info.plist file if necessary. diff --git a/gdx/src/com/badlogic/gdx/utils/JsonValue.java b/gdx/src/com/badlogic/gdx/utils/JsonValue.java index 2a78404cffe..3acef9e1279 100644 --- a/gdx/src/com/badlogic/gdx/utils/JsonValue.java +++ b/gdx/src/com/badlogic/gdx/utils/JsonValue.java @@ -912,11 +912,13 @@ public void addChild (JsonValue value) { if (type == ValueType.object && value.name == null) throw new IllegalStateException("An object child requires a name: " + value); value.parent = this; + value.next = null; size++; JsonValue current = child; - if (current == null) + if (current == null) { + value.prev = null; child = value; - else { + } else { while (true) { if (current.next == null) { current.next = value; diff --git a/gdx/test/com/badlogic/gdx/utils/JsonValueTest.java b/gdx/test/com/badlogic/gdx/utils/JsonValueTest.java new file mode 100644 index 00000000000..021a91f91ea --- /dev/null +++ b/gdx/test/com/badlogic/gdx/utils/JsonValueTest.java @@ -0,0 +1,32 @@ + +package com.badlogic.gdx.utils; + +import org.junit.Assert; +import org.junit.Test; + +public class JsonValueTest { + + @Test + public void testAddingRemovedValue () { + // Prepare two JSON objects + JsonValue firstObject = new JsonValue(JsonValue.ValueType.object); + JsonValue secondObject = new JsonValue(JsonValue.ValueType.object); + + firstObject.addChild("a", new JsonValue("A")); + secondObject.addChild("b", new JsonValue("B")); + secondObject.addChild("c", new JsonValue("C")); + + // Remove an item from one object and add it to the other + JsonValue b = secondObject.remove("b"); + firstObject.addChild(b); + + // Check if both objects have the expected children + Assert.assertNotNull(firstObject.get("a")); + Assert.assertNotNull(firstObject.get("b")); + Assert.assertNull(firstObject.get("c")); + + Assert.assertNull(secondObject.get("a")); + Assert.assertNull(secondObject.get("b")); + Assert.assertNotNull(secondObject.get("c")); + } +}