Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/std/json.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ when defined(nimony):
import std / [parsejson, streams, parseutils, assertions, formatfloat, syncio, typetraits, tables, opt]
import ../../src/lib/nifcore

export nifcore # the flat-buffer model is std/json's public substrate

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is the correct way to do it. I'm open to suggestions 🙏

export parsejson # callers may want JsonParser, getTok, etc.

# ── Types ────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -88,14 +89,14 @@ type
## skips it. Empty (default) ⇒ no discriminator.
allowMissingKeys*: bool ## `fromJson`: a field without a member keeps its default
allowExtraKeys*: bool ## `fromJson`: a member that names no field is skipped
readError: string ## `fromJson`: why a reader raised; the entry copies it to the tree
readError*: string ## `fromJson`: why a reader raised; the entry copies it to the tree

JsonTree* = object
## Owns the flat token buffer backing a JSON document. Move-only (the
## underlying `TokenBuf` forbids copy); a `JsonNode` obtained via `root` keeps
## the data alive independently through `nifcore`'s reference counting.
buf*: TokenBuf
readError: string ## why the last `fromJson(tree, T)` raised; see `errorMsg`
readError*: string ## why the last `fromJson(tree, T)` raised; see `errorMsg`

JsonNode* = object
## A handle to one node inside a `JsonTree`: a `nifcore.Cursor` wrapped so the
Expand Down
12 changes: 12 additions & 0 deletions tests/nimony/stdlib/tjson.nim
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,18 @@ proc main() {.raises.} =
check kind(t.root), JNull
check errorMsg(t), "tjson_no_such_file.json Error: cannot open file"

block public_read_error:
var opts = JsonOptions()
opts.readError = "custom reason"
check opts.readError, "custom reason"

block public_substrate:
# std/json exports nifcore: the flat token buffer is part of its API.
var tb = createTokenBuf(4)
tb.openTag JKNull.tagId
tb.closeTag()
check tb.len, 1

echo "json self-tests passed"

try: main()
Expand Down
Loading