I have a use case where I must parse JSON into primitive values in Java. I have instructive fields embedded in the JSON that informs my parsing which primitive to deserialize to. These primitive types must be added to an array of some length.
So I may have some JSON like this:
"primitives" : [
{
"valueType" : "int",
"value" : 3
},
{
"valueType" : "double",
"value" : 4
},
]
I have written the code to properly parse this JSON into two primitives, one int and double with values 3 and 4. However, because I am adding them to an ArrayList which expects Objects they are autoboxed into Java's Integer and Double types. I thought of using a regular Java Array but there is still the problem of specifying the element type like Object[] arr where I have the same problem, or int[] where I am being too specific.
Is there some functionality within Java that can allow me to parse this JSON to the correct Array of primitives.
One solution I have considered is an object that has all the different primitives as properties but this seems like too much complexity if a language level path is available.
int value1 = SomeArray[0], anddouble value2 = SomeArray[1]seems like you would need to know the type of the value.Object[] arr?