Comments (5)
Hmmm, interesting. I'll take a look at that this evening and work out what's happening there. :)
from chiron.
👍
from chiron.
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"
from chiron.
(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)
from chiron.
Thanks for comments and sample, @kolektiv. But the issue I've reported was before I've traversed json using lens.
The exception occur when Json.deserialize
invoke StockQuote.FromJson
. Some time some field from Yahoo service are null, what I'd like to do is being able to supply a default in such case.
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 Json.read
in StockQuote.FromJson
?
from chiron.
Related Issues (20)
- DateTime should allow other formats
- Darren HOT 1
- Cani
- GetHashCode infinite loop
- How to serialize a list to a Json object HOT 4
- How about deserializeWith? HOT 1
- Convert to use FSharp.Core's new `Result` type
- Json.tryParse "null3" should raise Exception
- Releases are not tagged HOT 2
- No way to build on *nix/MacOS HOT 8
- Warnings when building about portable frameworks
- Null reference exception in Escaping.escape HOT 10
- Add .travis.yml for automated Linux testing HOT 1
- How-to-get-started tutorial HOT 2
- "serializeArrayWith" is missing.
- Object serialization: do not use a `Map<string, Json>`
- Missing tags for current beta release HOT 2
- v7 bind failure?
- v7 reference edge cases
- Add ObsoleteAttribute for old API to ease transition from v6.* HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chiron.