Skip to content

Commit

Permalink
Clear list pointers of child before adding it to a JsonValue (libgdx#…
Browse files Browse the repository at this point in the history
…7044)

* Add test for removing and adding a json value

* Clear list pointers of child before adding it to a JsonValue

* Update CHANGES
  • Loading branch information
dasisdormax authored Oct 5, 2023
1 parent 02202a3 commit f341273
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions gdx/src/com/badlogic/gdx/utils/JsonValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 32 additions & 0 deletions gdx/test/com/badlogic/gdx/utils/JsonValueTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit f341273

Please sign in to comment.