Arrays - leocadiotine/Dumbledroid GitHub Wiki
Dumbledroid supports JSON and XML arrays. This page will guide you how to use this feature.
##How it works
When a field is declared as a List
, Dumbledroid will understand that it corresponds to an array. And since List<>
is a generic type, Dumbledroid will use the type between the <>
to find out the type of the array.
Dumbledroid supports JSON and XML arrays, of primitive types or complex objects.
##JSON arrays
###Primitive types
Suppose you have a JSON that contains an array of int
like this:
{
"name": "Success codes",
"codes": [200, 201, 202, 203, 204, 205, 206, 207, 208, 250, 226]
}
The equivalent Dumbledroid model will be like this:
public class HttpCodes extends AbstractModel {
private String name;
private List<Integer> codes;
public HttpCodes() {
super(YOUR_URL);
}
@Override
protected DataType getDataType() {
return DataType.JSON;
}
}
Important note: List
doesn't support primitives as its generic type. So instead of using List<int>
, you should use List<Integer>
. The same for the rest of primitive types: List<Boolean>
, List<Float>
, etc.
###Complex objects If you JSON contains an array of complex objects like this:
{
"name": "Success codes",
"codes": [
{
"number": 200,
"description": "OK"
},
{
"number": 201,
"description": "Created"
},
{
"number": 202,
"description": "Accepted"
}
]
}
…you should write two classes. One for HttpCodes
:
public class HttpCodes extends AbstractModel {
private String name;
private List<Code> codes;
public HttpCodes() {
super(YOUR_URL);
}
@Override
protected DataType getDataType() {
return DataType.JSON;
}
}
…and another for Code
:
public class Code implements Serializable {
private int number;
private String description;
}
##XML arrays XML arrays are a bit more complicated, because XML has no formal declaration for arrays. Basically, arrays can be represented by two ways in XML. This article shows both of them, which are supported by Dumbledroid.
###Primitive types Suppose you have a XML like this:
<HttpCodes>
<name>Success codes</name>
<codes>
<code>200</code>
<code>201</code>
<code>202</code>
</codes>
</HttpCodes>
…you should write a class like this:
public class HttpCodes extends AbstractModel {
private String name;
private List<Integer> codes;
public HttpCodes() {
super(YOUR_URL);
}
@Override
protected DataType getDataType() {
return DataType.XML;
}
}
But suppose your XML has an array declared like this:
<HttpCodes>
<name>Success codes</name>
<code>200</code>
<code>201</code>
<code>202</code>
</HttpCodes>
Dumbledroid handles it the same way it did with the first XML. The only difference here is that instead of List<Integer> codes;
(codes in plural), it will be List<Integer> code;
( code in singular).
###Complex objects The same thing is valid with arrays of complex objects in XML. A document like this:
<HttpCodes>
<name>Success codes</name>
<codes>
<code>
<number>200</number>
<description>OK</description>
</code>
<code>
<number>201</number>
<description>Created</description>
</code>
<code>
<number>202</number>
<description>Accepted</description>
</code>
</codes>
</HttpCodes>
…will require two classes. One for HttpCodes
:
public class HttpCodes extends AbstractModel {
private String name;
private List<Code> codes;
public HttpCodes() {
super(YOUR_URL);
}
@Override
protected DataType getDataType() {
return DataType.XML;
}
}
…and another for Code
:
public class Code implements Serializable {
private int number;
private String description;
}
Here, we can also have a different kind of array declaration:
<HttpCodes>
<name>Success codes</name>
<code>
<number>200</number>
<description>OK</description>
</code>
<code>
<number>201</number>
<description>Created</description>
</code>
<code>
<number>202</number>
<description>Accepted</description>
</code>
</HttpCodes>
And it works the same way as the primitive types ( code instead of codes ).