Skip to content

Commit 0181d2a

Browse files
committed
Update readme.
1 parent 4121040 commit 0181d2a

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

README.md

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ With this library you can parse json your way, from the mess you get to the obje
88

99
Current standard module first parses json into JsonNodes and then turns the JsonNodes into objects you want. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
1010

11+
## Can parse most object types:
12+
13+
* numbers and strings
14+
* objects and ref objects
15+
* enums
16+
* tuples
17+
* seq and arrays
18+
* tables
19+
1120
## Not strict.
1221

1322
Extra json fields are ignored and missing json fields keep their default values. Json is never exactly what you want.
@@ -20,9 +29,26 @@ var v = fromJson[Entry1](s)
2029
doAssert v.color == ""
2130
```
2231

32+
## Snake_case or CamelCase
33+
34+
Nim usually uses camalCase for its variables, while a bunch of json in the wild uses snake_case. This library will convert snake_case to camalCase for you when reading json.
35+
36+
```nim
37+
type Entry4 = object
38+
colorBlend: string
39+
40+
var v = fromJson[Entry4]("""{"colorBlend":"red"}""")
41+
doAssert v.colorBlend == "red"
42+
43+
v = fromJson[Entry4]("""{"color_blend":"red"}""")
44+
doAssert v.colorBlend == "red"
45+
```
46+
2347
## Has hooks.
2448

25-
### `proc newHook(foo: var ...)` Can be used to populate default values.
49+
### `proc newHook()` Can be used to populate default values.
50+
51+
Some times absence of a field means it should have a default value. Normally hits would just be Nim's default value for the variable type. But with the newHook() you can setup the object with defaults before the main parsing happens.
2652

2753
```nim
2854
type
@@ -39,15 +65,17 @@ doAssert v.id == "123"
3965
doAssert v.visible == "yes"
4066
```
4167

42-
### `proc enumHook[...](v: string): ...` Can be used to parse enums.
68+
### `proc enumHook()` Can be used to parse enums.
69+
70+
In wild json enums name almost never match to nim enum names that usually have a prefix. The enumHook() allows you to rename the enums to your internal names.
4371

4472
```nim
4573
type Color2 = enum
4674
c2Red
4775
c2Blue
4876
c2Green
4977
50-
proc enumHook[Color2](v: string): Color2 =
78+
proc enumHook(v: string): Color2 =
5179
case v:
5280
of "RED": c2Red
5381
of "BLUE": c2Blue
@@ -57,4 +85,20 @@ proc enumHook[Color2](v: string): Color2 =
5785
doAssert fromJson[Color2](""" "RED" """) == c2Red
5886
doAssert fromJson[Color2](""" "BLUE" """) == c2Blue
5987
doAssert fromJson[Color2](""" "GREEN" """) == c2Green
60-
```
88+
```
89+
90+
### `proc renameHook()` Can be used to rename fields at run time.
91+
92+
In wild json field names can be reserved words such as type, class, or array. With the renameHook you can rename fields to what you want on the type you need.
93+
94+
```nim
95+
type Node = ref object
96+
kind: string
97+
98+
proc renameHook(v: var Node, fieldName: var string) =
99+
if fieldName == "type":
100+
fieldName = "kind"
101+
102+
var node = fromJson[Node]("""{"type":"root"}""")
103+
doAssert node.kind == "root"
104+
```

0 commit comments

Comments
 (0)