JSON.jl
JSON.jl copied to clipboard
Throw exceptions on unknown/unreasonable types
I'd rather have JSON.json emit exceptions when trying to serialize unknown datatypes and/or types without a JSON equivalent.
For example _print(io::IO, state::State, f::Function) will silently accept functions and emit an useless JSON string. I'm pretty sure nobody wants to let functions slip through silently.
I'm also looking at d::DataType. Is there any case where this would result in useful behavior?
JSON is often used for things like collecting exception data or more generally to record the state of a/the system for inspection later. In situations like that you really want as much information as possible and want JSON serialisation to fail as rarely as possible.
Julia isn't designed for exception throw - catch - propagate - process rethrow - etc. in a standard workflow so I think JSON.jl should try it's best to serialise everything.
However I see your point so perhaps we should have a "fragile" mode where only datatypes with a reasonable JSON equivalent can be serialised.
Then again, my suspicion is that performance is an even higher priority than any of these things, and checking every datatype would definitely slow things down considerably. So I suggest we either:
- have a seperate preprocessing function/package which does the fragile check.
- or, have a separate high performance JSON serialiser which does away with pretty indents, and doesn't do any more type checks than strictly necessary. This could either be raw Julia or use some c, I started this in uJSON.jl but didn't get that far. I know people will say "JSON isn't designed for big data", maybe not but it's extremely convenient for a whole raft of things, things where Julia being slow really wouldn't look great.
On 05/26/2015 05:17 PM, Samuel Colvin wrote:
JSON is often used for things like collecting exception data or more generally to record the state of a/the system for inspection later. In situations like that you really want as much information as possible and want JSON serialisation to fail as rarely as possible.
I don't see much difference between a ::function and ::Module (except that module is explicitly forbidden). It's a bit inconsistent.
If that would be the case, I would add a catch-all function that just coerces any unhandled type into string(type(v), ": ", v) or something similar.
However I see your point so perhaps we should have a "fragile" mode where only datatypes with a reasonable JSON equivalent can be serialised.
I wouldn't say "fragile". If you're using JSON for data interchange, I would definitely consider any unknown type to be a strict error.
dumping internal state to a third-party observer might not always be desirable, although my primary concern is dumping types that I cannot restore.
Then again, my suspicion is that performance is an even higher priority than any of these things, and checking every datatype would definitely slow things down considerably.
You're already doing that implicitly through _print though.