1.4.0
More abilities in Index Notation!
To extend on dot notations in paths, you can use this notation to perform operations on array children.
Take this JSON:
{
"group_id": 1,
"title": "Hello, world!",
"participants": [
{
"name": "Aidan",
"id": 2
},
{
"name": "Waverly",
"id": 1
}
]
}
You could create this using index notation as such:
Ason ason = new Ason()
.put("group_id", 1)
.put("title", "Hello, world!")
.put("participants.$0.name", "Aidan")
.put("participants.$0.id", 2)
.put("participants.$1.name", "Waverly")
.put("participants.$1.id", 1);
The dollar sign followed by the number 0 indicates that you want the item at index 0 (position 1)
within an array called "participants".
You can retrieve the value of "name" in the second participant like this:
Ason object = // ...
String name = object.get("participants.$1.name");
If you wanted to remove the first item from the inner array, you can do that with index notation. This avoids the
need to first retrieve the "participants" object:
Ason object = // ...
object.remove("participants.$0");
Dot Notation has also been improved, here's the latest docs!
Lets create an object using a few dot notation keys:
Ason ason = new Ason()
.put("id", 1)
.put("name", "Aidan")
.put("birthday.month", "July")
.put("birthday.day", 28)
.put("birthday.year", 1995);
The above would construct this:
{
"id": 1,
"name": "Aidan",
"birthday": {
"month": "July",
"day": 28,
"year": 1995
}
}
As you can see, a child object is automatically created for you. We only use two levels, but you could create many more
just by using more periods to separate child names.
You can retrieve values from objects using dot notation:
Ason ason = // ...
String name = ason.get("name");
String month = ason.get("birthday.month");
int day = ason.get("birthday.day");
int year = ason.get("birthday.year");
If you wanted to remove the inner "year" value:
Ason ason = // ...
ason.remove("birthday.year");
You can use dot notation with arrays too, but you need to specify the index of the object to pull from:
AsonArray ason = // ...
String name = ason.get(1, "birthday.month");
As a bonus, you can check equality without doing a manual value comparison:
Ason ason = // ...
boolean birthYearCheck = ason.equal("birthday.year", 1995);
AsonArray ason2 = // ...
boolean birthYearCheck2 = ason2.equal(2, "birthday.year", 1995);