-
Notifications
You must be signed in to change notification settings - Fork 41
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
Q: how to handle null in Json #41
Comments
Hmmm, interesting. I'll take a look at that this evening and work out what's happening there. :) |
👍 |
Trying things out, I'm looking at something like this, which seems to work - but I'm not sure what code you're actually running! Feel free to post up more repro if this isn't helpful :) let txt = """{"query":{"results":{"low":null}}}"""
let json = Json.parse txt
let low =
idLens
<-?> Json.ObjectPIso
>??> mapPLens "query"
<??> Json.ObjectPIso
>??> mapPLens "results"
<??> Json.ObjectPIso
>??> mapPLens "low"
<??> Json.StringPIso
let read =
function | Some v -> v
| _ -> "default"
<!> Json.tryGetLensPartial low
let low =
match read json with
| Value low, _ -> low
| _ -> failwith "unreachable" |
(On a side note, I think it's worth doing something which combines Json.ObjectPIso and mapPLens x, as that's a common combination. I'll look at that in the next release too, which will be shortly, to bring Chiron in to line with common naming convention for lenses) |
Thanks for comments and sample, @kolektiv. But the issue I've reported was before I've traversed json using lens. This was the original parseResponse before partial lenses: let private parseResponse json =
match Json.parse json with
| Object values ->
match values |> Map.find "query" with
| Object values ->
match values |> Map.find "results" with
| Object values ->
let node = values |> Map.find "quote"
match node with
| Array quotes ->
quotes |> List.map (Json.deserialize : _ -> StockQuote)
| Object _ ->
[Json.deserialize node]
| _ -> []
| _ -> []
| _ -> []
| _ -> [] I don't know if I completely understood your reply... Do I have to define a Json traversal for each field that could be null? There's nothing more terse I can't do with |
When Json is something like
When I try to parse with
Json.read
DaysLow
for example I get:Using
Json.readOrDefault "DaysLow" String.Empty
didn't solved the problem.If json contains a null value I'd like to supply a default. Is this possible?
Thank you.
The text was updated successfully, but these errors were encountered: