parent
8c81cddf7e
commit
e134a722de
2 changed files with 29 additions and 0 deletions
|
|
@ -47,6 +47,10 @@ type
|
||||||
- Added `system.getOsFileHandle` which is usually more useful
|
- Added `system.getOsFileHandle` which is usually more useful
|
||||||
than `system.getFileHandle`. This distinction is only meaningful on
|
than `system.getFileHandle`. This distinction is only meaningful on
|
||||||
Windows.
|
Windows.
|
||||||
|
- Added a `json.parseJsonFragments` iterator that can be used to speedup
|
||||||
|
JSON processing substantially when there are JSON fragments separated
|
||||||
|
by whitespace.
|
||||||
|
|
||||||
|
|
||||||
## Library changes
|
## Library changes
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -841,10 +841,27 @@ proc parseJson(p: var JsonParser): JsonNode =
|
||||||
raiseParseErr(p, "{")
|
raiseParseErr(p, "{")
|
||||||
|
|
||||||
when not defined(js):
|
when not defined(js):
|
||||||
|
iterator parseJsonFragments*(s: Stream, filename: string = ""): JsonNode =
|
||||||
|
## Parses from a stream `s` into `JsonNodes`. `filename` is only needed
|
||||||
|
## for nice error messages.
|
||||||
|
## The JSON fragments are separated by whitespace. This can be substantially
|
||||||
|
## faster than the comparable loop
|
||||||
|
## ``for x in splitWhitespace(s): yield parseJson(x)``.
|
||||||
|
## This closes the stream `s` after it's done.
|
||||||
|
var p: JsonParser
|
||||||
|
p.open(s, filename)
|
||||||
|
try:
|
||||||
|
discard getTok(p) # read first token
|
||||||
|
while p.tok != tkEof:
|
||||||
|
yield p.parseJson()
|
||||||
|
finally:
|
||||||
|
p.close()
|
||||||
|
|
||||||
proc parseJson*(s: Stream, filename: string = ""): JsonNode =
|
proc parseJson*(s: Stream, filename: string = ""): JsonNode =
|
||||||
## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
|
## Parses from a stream `s` into a `JsonNode`. `filename` is only needed
|
||||||
## for nice error messages.
|
## for nice error messages.
|
||||||
## If `s` contains extra data, it will raise `JsonParsingError`.
|
## If `s` contains extra data, it will raise `JsonParsingError`.
|
||||||
|
## This closes the stream `s` after it's done.
|
||||||
var p: JsonParser
|
var p: JsonParser
|
||||||
p.open(s, filename)
|
p.open(s, filename)
|
||||||
try:
|
try:
|
||||||
|
|
@ -1778,3 +1795,11 @@ when isMainModule:
|
||||||
)
|
)
|
||||||
|
|
||||||
doAssert(obj == to(%obj, type(obj)))
|
doAssert(obj == to(%obj, type(obj)))
|
||||||
|
|
||||||
|
when not defined(js):
|
||||||
|
const fragments = """[1,2,3] {"hi":3} 12 [] """
|
||||||
|
var res = ""
|
||||||
|
for x in parseJsonFragments(newStringStream(fragments)):
|
||||||
|
res.add($x)
|
||||||
|
res.add " "
|
||||||
|
doAssert res == fragments
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue