SpinJsonNode add/remove elements to SpinList

Hi!

sorry if using your Spin implementation wrong, but I encounter with this problem.

When using SpinJsonNode for storing process and task variables I can’t set elements in existing array. After modifing or adding elements into existing array, nothing is added/removed, but return value from method “add” is true and from “remove” method is correct element.

Can you check my simple code pls ?

import org.camunda.spin.json.SpinJsonNode;
import org.junit.jupiter.api.Test;

public class SpinTest {

@Test
public void spinArrayTest() {
	String obj = "{\r\n\t\"procVersion\": 1.0,\r\n\t\"uzivatel\": {\r\n\t\t\"id_uzivatele\": 11111,\r\n\t\t\"jmeno\": \"Pan\",\r\n                \"prijmeni\": \"Au\",\r\n                \"nickname\": \"mkasl\"\r\n\t},\r\n\t\"accounting\": [{\r\n\t\t\t\"firstName\": \"John\",\r\n\t\t\t\"lastName\": \"Doe\",\r\n\t\t\t\"address\": {\r\n\t\t\t\t\"street\": \"volsanska\",\r\n\t\t\t\t\"zip\": 2332\r\n\t\t\t}\r\n\t\t},\r\n\t\t{\r\n\t\t\t\"firstName\": \"Mary\",\r\n\t\t\t\"lastName\": \"Smith\",\r\n\t\t\t\"address\": {\r\n\t\t\t\t\"street\": \"dutohlavska\",\r\n\t\t\t\t\"zip\": 2332\r\n\t\t\t}\r\n\t\t}\r\n\t]\r\n}";

	String newAccount = "{\"firstName\":\"Tom\",\"lastName\":\"Bok\",\"address\":{\"street\":\"Tralala\",\"zip\":111}}";

	//Convert obj to SpinJsonNode
	SpinJsonNode spinJsonNode = SpinJsonNode.JSON(obj);

	System.out.println("Accounting elements before remove > " + spinJsonNode.prop("accounting").elements().toString());

	//Remove element
	//DOESN'T WORK nothig removed from object spinJsonNode
	SpinJsonNode removedElement = spinJsonNode.prop("accounting").elements().remove(0); //DOESN'T WORK

	System.out.println("Accounting elements affter remove > " + spinJsonNode.prop("accounting").elements().toString());
	System.out.println("removed element > " + removedElement);

	SpinJsonNode newAccountSpinJsonNode = SpinJsonNode.JSON(newAccount);
	//DOESN'T WORK nothing added to object spinJsonNode
	boolean added = spinJsonNode.prop("accounting").elements().add(newAccountSpinJsonNode); //DOESN'T WORK

	System.out.println("Accounting elements affter add > " + spinJsonNode.prop("accounting").elements().toString());
	System.out.println("added > " + added);

}

}

Using mvn dependency:

<dependency>
   <groupId>org.camunda.spin</groupId>
   <artifactId>camunda-spin-dataformat-json-jackson</artifactId>
   <version>1.5.3</version>
<dependency>

Thank you very much for your time!

BR
Maria

Hi Maria,

whenever Spin gives you a java.util.List, this is a copy of a collection of elements, not a view on that element collection in the original datastructure. So modifying the list will not do anything more than that.

See the following link for how to add an element to a JSON array and other examples:

Cheers,
Thorben

1 Like