Maryk DataObjects can be serialized into three formats: YAML, JSON, and ProtoBuf. Each format has its own advantages depending on the use case.
- ProtoBuf is the preferred format for its compact size and efficiency.
- YAML is ideal when human readability is a priority.
- JSON is suitable for interoperability with external libraries and systems.
YAML is a human-readable data serialization format that uses indentation to structure data. It is particularly useful for configuration files and data exchange between languages with different data structures. Maryk provides its own YAML library, which supports features such as anchors, aliases, complex field names, and comments. This makes YAML an excellent choice for defining model definitions, queries, and data objects.
name: User
key:
- !Ref username
? 0: username
: !String
required: true
final: true
unique: true
? 1: email
: !String
required: true
unique: true
username: john.smith
email: john.smith@gmail.com
val user: Values<User, User.Properties> // instance of a user
val yamlWriter: YamlWriter // instance of YAML writer
User.writeYaml(user, yamlWriter)
val yamlReader: YamlReader // instance of YAML reader
val user: Values<User, User.Properties> = User.readYaml(yamlReader)
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It is widely used across various platforms and programming languages. Maryk includes its own streaming JSON library to ensure consistent functionality across all supported platforms. JSON can also be formatted in "pretty mode" for enhanced readability.
{
"name": "User",
"key": [["Ref", "username"]],
"properties": [{
"index": 0,
"name": "username",
"definition": ["String", {
"required": true,
"final": true,
"unique": true
}]
}, {
"index": 1,
"name": "email",
"definition": ["String", {
"required": true,
"unique": true
}]
}]
}
{
"username": "john.smith",
"email": "john.smith@gmail.com"
}
val user: Values<User, User.Properties> // instance of a user
val jsonWriter: JsonWriter // instance of JSON writer
User.writeJson(user, jsonWriter)
val jsonReader: JsonReader // instance of JSON reader
val user: Values<User, User.Properties> = User.readJson(jsonReader)
ProtoBuf (Protocol Buffers) is a language-neutral, platform-neutral extensible mechanism for serializing structured data. It is highly efficient in terms of both speed and space, making it suitable for high-performance applications. For more details, read here.
val user: Values<User, User.Properties> // instance of a user
val byteWriter: (Byte) -> Unit // instance of byte writer
val cache = WriteCache()
// Calculate the total byte length to write
val byteLength = User.calculateProtoBufLength(user, cache)
// Reserve space on your byte writer
User.writeProtoBuf(user, cache, byteWriter)
val byteReader: () -> Byte // function to read bytes
val byteLength: Int // Amount of bytes to read, typically defined in the request
val user = User.readProtoBuf(byteLength, byteReader)
It is also possible to obtain a map of index-value pairs when deserializing from ProtoBuf. This is useful when a generated model is not available.
val user: Values<User, User.Properties> = User.readProtoBuf(byteLength, byteReader)