Structuring data
SyncDatabase is stored locally and remotely in JSON format. Therefore, when using the Sync SDK library to store application data, there may be cases where you need to define the fields representing the objects you want to save.
Data Structure Definition
The data you want to use in the application must be capable of JSON serialization. Therefore, you need to define the data structure as JSON Object
, JSON Array
, and JSON Primitive
.
The following code is a simple example that defines User
data that includes name and age.
- Kotlin
- Java
data class User(val name: String, val age: Int)
public class User {
public String name;
public int age;
}
- If the field value is an array, define it as List type.
- Kotlin
- Java
data class User(val name: String, val age: Int, val friends: List<String>)
public class User {
public String name;
public int age;
public List<String> friends;
}
- If the field value can be null, in Kotlin define it as Optional type, in Java define it as Non-primitive type.
- Kotlin
- Java
data class User(val name: String, val age: Int, val friends: List<String>, val money: Int?)
public class User {
public String name;
public int age;
public List<String> friends;
public Integer money;
}
- If the field is not subject to serialization, define the access modifier as private or internal, or add the Transient annotation.
- Kotlin
- Java
data class User(
val name: String,
val age:Int,
val friends:List<String>,
val money:Int?,
private val internalUse:Long?,
@Transient val internalShare:Int?)
public class User {
public String name;
public int age;
public List<String> friends;
public Integer money;
private Long internalUse;
@Transient
public Integer internalShare;
}
- If you want to customize the field name, add the SerializedName annotation.
- Kotlin
- Java
data class User(
val name: String,
val age:Int,
val friends:List<String>,
val money:Int?,
private val internalUse:Long?,
@Transient val internalShare:Int?,
@SerializedName(value = "priority") val level: String)
public class User {
public String name;
public int age;
public List<String> friends;
public Integer money;
private Long internalUse;
@Transient
public Integer internalShare;
@SerializedName(value = "priority")
public String level;
}
[Constraints]
- You cannot define the field name as
_id
. - Do not include periods in field names, such as
a.b
. - Design so that the total size of the data does not exceed 5 MB.