mirror of
https://github.com/logos-storage/nim-serde.git
synced 2026-01-07 16:13:06 +00:00
Apply suggestions from code review
Co-authored-by: Giuliano Mega <giuliano.mega@gmail.com>
This commit is contained in:
parent
6793ee53f4
commit
2d8fa4d940
@ -21,7 +21,7 @@ import questionable/results
|
|||||||
type Person = object
|
type Person = object
|
||||||
name {.serialize.}: string
|
name {.serialize.}: string
|
||||||
age {.serialize.}: int
|
age {.serialize.}: int
|
||||||
address: string # Not serialized by default in OptIn mode
|
address: string # By default, serde will not serialize non-annotated fields (OptIn mode)
|
||||||
|
|
||||||
# Create an instance
|
# Create an instance
|
||||||
let person = Person(name: "John Doe", age: 30, address: "123 Main St")
|
let person = Person(name: "John Doe", age: 30, address: "123 Main St")
|
||||||
@ -70,7 +70,7 @@ import std/streams
|
|||||||
|
|
||||||
# Define a type
|
# Define a type
|
||||||
type Person = object
|
type Person = object
|
||||||
name: string
|
name: string # Unlike JSON, CBOR always serializes all fields, and they do not need to be annotated
|
||||||
age: int
|
age: int
|
||||||
address: string
|
address: string
|
||||||
|
|
||||||
|
|||||||
@ -67,12 +67,14 @@ Types can be serialized and deserialized even without explicit annotations, usin
|
|||||||
|
|
||||||
```nim
|
```nim
|
||||||
# Type is not annotated
|
# Type is not annotated
|
||||||
# A default mode of OptIn (for serialize) and OptOut (for deserialize) is assumed.
|
# If you don't annotate the type, serde assumes OptIn by default for serialization, and OptOut for
|
||||||
|
# deserialization. This means your types will be serialized to an empty string, which is probably not what you want:
|
||||||
type MyObj1 = object
|
type MyObj1 = object
|
||||||
field1: bool
|
field1: bool
|
||||||
field2: bool
|
field2: bool
|
||||||
|
|
||||||
# Type is annotated, but mode not specified
|
# If you annotate your type but do not specify the mode, serde will default to OptOut for
|
||||||
|
# both serialize and de-serialize, meaning all fields get serialized/de-serialized by default:
|
||||||
# A default mode of OptOut is assumed for both serialize and deserialize.
|
# A default mode of OptOut is assumed for both serialize and deserialize.
|
||||||
type MyObj2 {.serialize, deserialize.} = object
|
type MyObj2 {.serialize, deserialize.} = object
|
||||||
field1: bool
|
field1: bool
|
||||||
@ -390,7 +392,7 @@ assert errorResult.error of UnexpectedKindError
|
|||||||
|
|
||||||
### Parsing JSON with `JsonNode.parse`
|
### Parsing JSON with `JsonNode.parse`
|
||||||
|
|
||||||
For parsing raw JSON without immediate deserialization:
|
To parse JSON string into a `JsonNode` tree instead of a deserializing to a concrete type, use `JsonNode.parse`:
|
||||||
|
|
||||||
```nim
|
```nim
|
||||||
import pkg/serde/json
|
import pkg/serde/json
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user