Fix README field options example

This commit is contained in:
Eric 2024-02-08 08:09:26 +11:00
parent d877872127
commit 88ce7fb56c
No known key found for this signature in database

View File

@ -157,15 +157,38 @@ Type fields can be annotated with `{.serialize.}` and `{.deserialize.}` and prop
For example, For example,
```nim ```nim
type MyObj {.serialize(mode=OptOut).} = object import pkg/serde/json
field1 {.serialize("mykey").}: bool
field2: bool
# equivalent to type
Person {.serialize(mode=OptOut), deserialize(mode=OptIn).} = object
id {.serialize(ignore=true), deserialize(key="personid").}: int
name: string
birthYear: int
address: string
phone: string
type MyObj {.serialize(mode=OptOut).} = object let person = Person(
field1 {.serialize(key="mykey", ignore=true).}: bool name: "Lloyd Christmas",
field2: bool birthYear: 1970,
address: "123 Sesame Street, Providence, Rhode Island 12345",
phone: "555-905-justgivemethedamnnumber!⛽️🔥")
let createRequest = """{
"name": "Lloyd Christmas",
"birthYear": 1970,
"address": "123 Sesame Street, Providence, Rhode Island 12345",
"phone": "555-905-justgivemethedamnnumber!⛽️🔥"
}"""
assert person.toJson(pretty=true) == createRequest
let createResponse = """{
"personid": 1,
"name": "Lloyd Christmas",
"birthYear": 1970,
"address": "123 Sesame Street, Providence, Rhode Island 12345",
"phone": "555-905-justgivemethedamnnumber!⛽️🔥"
}"""
assert !Person.fromJson(createResponse) == Person(id: 1)
``` ```
### `key` ### `key`