Skip to main content

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.

data class User(val name: String, val age: Int)
  • If the field value is an array, define it as List type.
data class User(val name: String, val age: Int, val friends: List<String>)
  • If the field value can be null, in Kotlin define it as Optional type, in Java define it as Non-primitive type.
data class User(val name: String, val age: Int, val friends: List<String>, val money: Int?)
  • If the field is not subject to serialization, define the access modifier as private or internal, or add the Transient annotation.
data class User(
val name: String,
val age:Int,
val friends:List<String>,
val money:Int?,
private val internalUse:Long?,
@Transient val internalShare:Int?)
  • If you want to customize the field name, add the SerializedName annotation.
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)
[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.