quinnj / JSON3.jl

Other
214 stars 47 forks source link

Read JSON and materialize to plain Julia type #215

Open aplavin opened 2 years ago

aplavin commented 2 years ago

It would be useful to provide a generic function, let's call it materialize for now, that always makes a plain Julia type from the result of JSON3.read. Currently, copy is close to being that function, but not quite:

julia> read_plain(str) = JSON3.read(str) |> copy

# ok:
julia> read_plain("[1, 2]")
2-element Vector{Int64}:
 1
 2

# ok:
julia> read_plain("1")
1

# not ok:
julia> read_plain("\"1\"")
ERROR: MethodError: no method matching copy(::String)
quinnj commented 2 years ago

Yeah, good catch. Part of the issue is that copy isn't defined by default for immutables in Base. I think there's been some discussion on defining that though recently. In the mean time, I'd be fine defining a JSON3.materialize function that does what you're after. It'd be a pretty simple definition, if you'd like to put up a PR!