You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I stabbed myself by using a Dict because I needed to change some values for a configuration file used in another application. Turns out the said application relies on the order of entries in the json file.
How easy it is to support reading into an ordered dictionary (from DataStructures.jl?)
julia> a = tempname()
"/scratch/jl_YMCMDd"
julia> open(a, "w") do f
write(a, JSON3.write(OrderedDict("a1"=>1, "a2"=>2)))
end
julia> JSON3.read(read(a, String), Dict) |> JSON3.write
"{\"a2\":2,\"a1\":1}"
julia> JSON3.read(read(a, String)) |> JSON3.write
"{\"a1\":1,\"a2\":2}"
julia> JSON3.read(read(a, String), OrderedDict) |> JSON3.write
ERROR: MethodError: no method matching keytype(::Type{OrderedDict})
The text was updated successfully, but these errors were encountered:
@Moelf 's solution only does the first level and do not recurse through the structure like copy(::JSON3.Object) does for Dict.
To address this, I prototyped the following extension.
baremodule OrderedCollectionsExt
using OrderedCollections: OrderedDict
using JSON3: Object, Array
using Base: Base, copy, map
function Base.copy(obj::Object, ::Type{OrderedDict})
dict =OrderedDict{Symbol, Any}()
for (k, v) in obj
dict[k] = v isa Object || v isa Array ?copy(v, OrderedDict) : v
endreturn dict
end
Base.copy(arr::Array, ::Type{OrderedDict}) =map(x->x isa Object || x isa Array ?copy(x, OrderedDict) : x, arr)
end
Observe the difference in the returned type of :c:
I stabbed myself by using a
Dict
because I needed to change some values for a configuration file used in another application. Turns out the said application relies on the order of entries in the json file.How easy it is to support reading into an ordered dictionary (from
DataStructures.jl
?)The text was updated successfully, but these errors were encountered: