-
Notifications
You must be signed in to change notification settings - Fork 30
add doc strings for json module #75
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
Conversation
Short question: Should we create more specific types for the |
If they can be typed, sure! cc @glennsl who also had similar comments. Here's the original PR this was taken from in |
My comment, for the record :) #16 (comment) |
Same thoughts. I could change the types. Maybe in another PR? |
I'm fine with you doing it in this PR if you want. Up to you. |
What about this: reviver: @val external parseExnWithReviver: (string, (string, 'a) => 'b) => JSON.t = "JSON.parse"
let reviver1 = (key, value) => {
let jsType = JSON.Classify.classify(value)
switch jsType {
| String(v) => String.length(v)
| _ => value
}
}
let reviver2 = (key, value) => {
if key->String.toLowerCase->String.includes("date") {
value->Date.fromString->Obj.magic
} else {
value
}
}
let jsonString = `{"parseAsDate":"2023-02-26","parseAsString":"2023-02-26","someNumber":42}`
parseExnWithReviver(jsonString, reviver1)->Console.log
parseExnWithReviver(jsonString, reviver2)->Console.log replacer: @val external stringifyWithReplacer: (JSON.t, (string, 'a) => string) => string = "JSON.stringify"
let json =
Dict.fromArray([
("foo", JSON.Encode.string("bar")),
("hello", JSON.Encode.string("world")),
])->JSON.Encode.object
let replacer1 = (key, value) => {
if key === "foo" {
String.toUpperCase(value)
} else if value === "world" {
value ++ "!"
} else {
value
}
}
stringifyWithReplacer(json, replacer1)->Console.log Unfortunately not really useful, because the generic types could be everything. |
@dkirchhof I guess it's as good as it gets. Thoughts @cknitt @glennsl ? |
For this to be sound,
This is trickier. The first parameter, The second parameter, And I believe the return value can also be And finally, tl;dr: taking all this into account I would propose: @val external parseExnWithReviver: (string, (string, JSON.t) => JSON.t) => JSON.t = "JSON.parse"
@val external stringifyWithReplacer: (JSON.t, (string, JSON.t) => JSON.t) => string = "JSON.stringify"
@val external stringifyWithFilter: (JSON.t, array<string>) => string = "JSON.stringify"
@val external stringifyAnyWithReplacer: ('a, (string, JSON.t) => JSON.t) => string = "JSON.stringify" |
@glennsl I would like to agree with you, but thinking about real use cases for these functions. Often I use a reviver like in my example (reviver2). Date strings will be returned as dates. So I don't need any additional codecs or whatever to map these "raw" objects to some kind of domain models. external jsonEncodeAny: 'a => JSON.t = "%identity"
let reviver2 = (key: string, value: JSON.t) => {
if key->String.toLowerCase->String.includes("date") {
value->JSON.Decode.string->Option.map(Date.fromString)->jsonEncodeAny
} else {
value
}
} |
I would be fine with removing |
Here the unsoundness is at least exposed. It's not possible to do this in a sound way anyway, and I think the next best alternative then is to make the unsoundness explicit and have the user make a conscious decision. This does do that.
|
I replaced the reviver and replacer functions and added all variants of |
Could you add the Example: |
Should I remove the "throws a JavaScript exception" in the description if a I add the exceptions block? |
I'm having a look at this very soon. @cknitt you up for taking a look too? |
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.
@dkirchhof fantastic work! So good to have all of these examples. I even learned a good amount of new things about these APIs just by reviewing 😄 Great work! 👍
I had a few suggestions, that's all. This is definitively mergable imo.
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
Co-authored-by: Gabriel Nordeborn <[email protected]>
@zth thanks for your review. Great to help you. |
You know what, let's skip |
Merging! 🎉 |
No description provided.