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
1 changed files with 30 additions and 7 deletions

View File

@ -157,15 +157,38 @@ Type fields can be annotated with `{.serialize.}` and `{.deserialize.}` and prop
For example,
```nim
type MyObj {.serialize(mode=OptOut).} = object
field1 {.serialize("mykey").}: bool
field2: bool
import pkg/serde/json
# 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
field1 {.serialize(key="mykey", ignore=true).}: bool
field2: bool
let person = Person(
name: "Lloyd Christmas",
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`