Skip to content

Commit 327ddb6

Browse files
committed
add RawJson
1 parent 981f868 commit 327ddb6

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,23 @@ type Entry = object
298298
}
299299
}""".fromJson(Entry)
300300
```
301+
302+
## Full support for raw-json.
303+
304+
Sometimes you don't need to parse the json, but just send it or store it in the database. You can speed this up by using `RawJson` type. What it does is prevents full parsing of that json tree and instead returns it is a `RawJson` (`disticnt string`) type. You can then do anything you want with that. Store it in a database or pass it along to some other API. Or maybe parse it later again with jsony.
305+
306+
```nim
307+
import jsony
308+
type
309+
Message = object
310+
id: uint64
311+
data: RawJson
312+
313+
let
314+
messageData = """{"id":123,"data":{"page":"base64","arr":[1,2,3]}}"""
315+
message = messageData.fromJson(Message)
316+
# make sure raw json was not parsed
317+
doAssert message.data.string == """{"page":"base64","arr":[1,2,3]}"""
318+
# make sure that dumping raw json produces same result
319+
doAssert message.toJson() == messageData
320+
```

src/jsony.nim

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import jsony/objvar, strutils, tables, sets, unicode, json, options, parseutils, typetraits
1+
import jsony/objvar, std/strutils, std/tables, std/sets, std/unicode, std/json, std/options, std/parseutils, std/typetraits
22

33
type JsonError* = object of ValueError
44

@@ -10,6 +10,7 @@ when defined(release):
1010
type
1111
SomeTable*[K, V] = Table[K, V] | OrderedTable[K, V] |
1212
TableRef[K, V] | OrderedTableRef[K, V]
13+
RawJson* = distinct string
1314

1415
proc parseHook*[T](s: string, i: var int, v: var seq[T])
1516
proc parseHook*[T: enum](s: string, i: var int, v: var T)
@@ -842,6 +843,14 @@ proc dumpHook*(s: var string, v: JsonNode) =
842843
of JBool:
843844
s.dumpHook(v.getBool)
844845

846+
proc parseHook*(s: string, i: var int, v: var RawJson) =
847+
let oldI = i
848+
skipValue(s, i)
849+
v = s[oldI ..< i].RawJson
850+
851+
proc dumpHook*(s: var string, v: RawJson) =
852+
s.add v.string
853+
845854
proc toJson*[T](v: T): string =
846855
dumpHook(result, v)
847856

tests/all.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ import test_tables
1818
import test_tojson
1919
import test_tuples
2020
import test_refs
21+
import test_rawjson
2122

2223
echo "all tests pass"

tests/test_rawjson.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import jsony
2+
3+
type
4+
Message = object
5+
id: uint64
6+
data: RawJson
7+
8+
let
9+
messageData = """{"id":123,"data":{"page":"base64","arr":[1,2,3]}}"""
10+
message = messageData.fromJson(Message)
11+
# make sure raw json was not parsed
12+
doAssert message.data.string == """{"page":"base64","arr":[1,2,3]}"""
13+
# make sure that dumping raw json produces same result
14+
doAssert message.toJson() == messageData

0 commit comments

Comments
 (0)