-
Notifications
You must be signed in to change notification settings - Fork 0
Paths and models
Efra Espada edited this page Mar 31, 2018
·
4 revisions
Objects are located in database by paths. Let's imagine we want to generate or work with this database model:
{
"chats": {
"chatA": {
"title": "My chat",
"members": {
"774562384": {
"rol": "admin"
},
"775635684": {
"rol": "basic"
}
},
"messages": {
"djwersdveg": {
"message": "Hi John!",
"author": "775635684"
},
"djhsfdgssg": {
"message": "Hi!",
"author": "774562384"
}
}
}
},
"users": {
"774562384": {
"name": "John",
"birth": 1993
},
"775635684": {
"name": "Mark",
"birth": 1993
}
}
}/chats/chatA/members
{
"774562384": {
"rol": "admin"
},
"775635684": {
"rol": "basic"
}
}/users/774562384
{
"name": "John",
"birth": 1993
}It's so easy to work with JSON objects. In JavaScript as associative arrays:
let object = {}
object.name = "John"
if (object.birth === undefined) {
object.birth = 1993
}
delete object["name"]
let string = JSON.stringify(object)
let result = JSON.parse(string)
console.log("birth: " + result.birth)In Android, Rotor works with Java and/or Kotlin apps. For work with JSON objects in Java define classes with the fields of desired model. Rotor libraries work with GSON and make that work for you. Check object lifecycle interface page for more documentation:
class User {
SerializedName("name")
String name;
SerializedName("birth")
int birth;
public User() {
}
public User(String name, int birth) {
this.name = name;
this.birth = birth;
}
public void setName(String name) {
this.name = name;
}
public void getName() {
return name;
}
public void setBirth(int birth) {
this.birth = birth;
}
public void getBirth() {
return birth;
}
}In Kotlin you can't work with data classes for JSON de/serialization. Use Java objects instead.
I couldn't do GSON work with data class with
varfields (variables). Withvalfields (values) works, but it's immutable