-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement To/FromJSON Empty
#5421
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Kyle Miller <kmill31415@gmail.com>
2333a92
to
5d364cf
Compare
CC: @kmill in case interested/relevant. |
Mathlib CI status (docs):
|
/-! Ensure we can parse the type back -/ | ||
/-- info: Except.ok { y := some 1 } -/ | ||
#guard_msgs in | ||
#eval fromStrHelper (Foo Nat) "{\"y\": 1}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it's not in core, but somewhere there's a json%
elaborator which should avoid the quoting awkwardness here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also raw strings:
#eval fromStrHelper (Foo Nat) "{\"y\": 1}" | |
#eval fromStrHelper (Foo Nat) r#"{"y": 1}"# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#eval fromStrHelper (Foo Nat) "{\"y\": 1}" | |
#eval Lean.fromJson? (α := Foo Nat) <| json% {"y": 1} |
@@ -28,6 +28,9 @@ instance : ToJson Json := ⟨id⟩ | |||
instance : FromJson JsonNumber := ⟨Json.getNum?⟩ | |||
instance : ToJson JsonNumber := ⟨Json.num⟩ | |||
|
|||
instance : FromJson Empty where | |||
fromJson? j := throw s!"no constructor matched JSON value '{j}'" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as the string is a literal here, rather than something generated by deriving
, it might save someone some debugging pain someday to write:
fromJson? j := throw s!"no constructor matched JSON value '{j}'" | |
fromJson? j := throw s!"type Empty has no constructor to match JSON value '{j}'" |
Resolve cases when the
To/FromJSON
type classes are used withEmpty
, e.g. in the following motivating example.This is a follow-up to this PR #5415, as suggested by @eric-wieser. It expands on the original suggestion by also handling
FromJSON
.