Updating based on further exploring and thanks to @pinaka30 for the comment-
I wanted to verify if I deserialize JSON array into List, will that preserve the sequence.
I took the below JSON
{
"MyJSON": {
"name": "Any Name",
"values": [
"1111",
"2222",
"3333",
"4444",
"5555",
"6666"
]
}
}
The DTO that I created is as follows:
package com.jsonrelated.jsonrelated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"MyJSONMapping"
})
public class MyJSONMapping {
@JsonProperty("MyJSON")
@Valid
@NotNull
private MyJSON myJSON;
@JsonProperty("MyJSON")
public MyJSON getMyJSON() {
return myJSON;
}
@JsonProperty("MyJSON")
public void setMyJSON(MyJSON myJSON) {
this.myJSON = myJSON;
}
}
package com.jsonrelated.jsonrelated;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.util.List;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"name",
"values"
})
public class MyJSON {
@JsonProperty("name")
String name;
@JsonProperty("values")
List<String> values;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getValues() {
return values;
}
public void setValues(List<String> values) {
this.values = values;
}
}
Then I have created a REST API and sending that JSON as request Body
@RequestMapping(path="generateMyJSONChart",method=RequestMethod.POST,produces="application/json",consumes="application/json")
public String generateMyJSONChart(@Valid @RequestBody MyJSONMapping myJSONMapping, @RequestParam("type") @NotBlank String type) throws IOException {
List comparedProducts = myJSONMapping.getMyJSON().getValues();
Iterator i = comparedProducts.iterator();
while (i.hasNext())
{
String name = (String) i.next();
System.out.println(name);
}
return "nothing";
}
I was checking if the sequence of the values in "Values" as in
"values": [
"1111",
"2222",
"3333",
"4444",
"5555",
"6666"
]
are preserved if we deserialize them into List as in
@JsonProperty("values")
List<String> values;
I have run it multiple times with 100 elements in the values and I see the order is preserved.
Adding a good post already there Is the order of elements in a JSON list preserved? so mine seems to be duplicate.