Skip to content

Commit 56a9c96

Browse files
committed
Merge branch 'master' of github.com:treeform/jsony
2 parents fe7f3f6 + fe2359b commit 56a9c96

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# JSONy - A loose, direct to object json parser with hooks.
22

3+
`nimble install jsony`
4+
35
Real world json is *never what you want*. It might have extra fields that you don't care about. It might have missing fields requiring default values. It might change or grow new fields at any moment. Json might use `camelCase` or `snake_case`. It might use inconsistent naming.
46

57
With this library you can parse json your way, from the mess you get to the objects you want.
68

79
## Fast/No garbage.
810

9-
Current standard module first parses json into JsonNodes and then turns the JsonNodes into your objects with the `to()` macro. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
11+
Currently the Nim standard module first parses json into JsonNodes and then turns the JsonNodes into your objects with the `to()` macro. This is slower and creates unnecessary work for the garbage collector. This library skips the JsonNodes and creates the objects you want directly.
1012

1113
## Can parse most object types:
1214

@@ -30,7 +32,7 @@ var v = fromJson[Entry1](s)
3032
doAssert v.color == ""
3133
```
3234

33-
## Snake_case or CamelCase
35+
## Converts snake_case to camelCase.
3436

3537
Nim usually uses `camelCase` for its variables, while a bunch of json in the wild uses `snake_case`. This library will convert `snake_case` to `camelCase` for you when reading json.
3638

@@ -49,7 +51,7 @@ doAssert v.colorBlend == "red"
4951

5052
### `proc newHook()` Can be used to populate default values.
5153

52-
Some times absence of a field means it should have a default value. Normally this 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.
54+
Sometimes the absence of a field means it should have a default value. Normally this would just be Nim's default value for the variable type but that isn't always what you want. With the newHook() you can set initialize the object your defaults before the main parsing happens.
5355

5456
```nim
5557
type
@@ -68,7 +70,7 @@ doAssert v.visible == "yes"
6870

6971
### `proc enumHook()` Can be used to parse enums.
7072

71-
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.
73+
In the wild json enum names almost never match to Nim enum names which usually have a prefix. The enumHook() allows you to rename the enums to your internal names.
7274

7375
```nim
7476
type Color2 = enum
@@ -90,7 +92,7 @@ doAssert fromJson[Color2](""" "GREEN" """) == c2Green
9092

9193
### `proc renameHook()` Can be used to rename fields at run time.
9294

93-
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.
95+
In the 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.
9496

9597
```nim
9698
type Node = ref object
@@ -118,7 +120,7 @@ proc parseHook(s: string, i: var int, v: var DateTime) =
118120
var dt = fromJson[DateTime](""" "2020-01-01 00:00:00" """)
119121
```
120122

121-
Some times json gives you an object of entries with their id as keys, but you might want it as a sequence with ids inside the objects, again you can do anything with `parseHook()`:
123+
Sometimes json gives you an object of entries with their id as keys, but you might want it as a sequence with ids inside the objects. You can handle this and many other scenarios with `parseHook()`:
122124

123125
```nim
124126
type Entry = object

src/jsony.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ proc parseHook*[T: tuple](s: string, i: var int, v: var T)
1212
proc parseHook*[T: array](s: string, i: var int, v: var T)
1313

1414
template error(msg: string, i: int) =
15-
## Short cut to raise an exception.
15+
## Shortcut to raise an exception.
1616
raise newException(JsonError, msg)
1717

1818
proc eatSpace*(s: string, i: var int) =
19-
## Will consume white space.
19+
## Will consume whitespace.
2020
while i < s.len:
2121
let c = s[i]
2222
if c in whiteSpace:
@@ -296,7 +296,6 @@ proc parseHook*[T](s: string, i: var int, v: var Table[string, T]) =
296296

297297
proc fromJson*[T](s: string): T =
298298
## Takes json and outputs the object it represents.
299-
## * Create little intermediate values.
300299
## * Extra json fields are ignored.
301300
## * Missing json fields keep their default values.
302301
## * `proc newHook(foo: var ...)` Can be used to populate default values.

0 commit comments

Comments
 (0)