update
This commit is contained in:
+56
-56
@@ -235,7 +235,7 @@ Does **not** mutate the input; it always allocates new containers.
|
|||||||
|
|
||||||
# Examples
|
# Examples
|
||||||
```jldoctest
|
```jldoctest
|
||||||
julia> using JSON
|
julia> using JSON, DataStructures
|
||||||
julia> d = Dict(
|
julia> d = Dict(
|
||||||
"a" => 4,
|
"a" => 4,
|
||||||
"b" => 6,
|
"b" => 6,
|
||||||
@@ -278,67 +278,67 @@ OrderedDict{String,Int} with 3 entries:
|
|||||||
"b" => 2
|
"b" => 2
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
function dictify(x; keytype::Type=Any, sort_order::Union{Nothing, Vector}=nothing)
|
function dictify(x::T; keytype::Type=Any, sort_order::Union{Nothing, Vector}=nothing
|
||||||
# Dict-like objects
|
)::OrderedDict where {T<:AbstractDict}
|
||||||
if x isa AbstractDict
|
# Dict-like objects
|
||||||
out = OrderedDict{keytype, Any}()
|
out = OrderedDict{keytype, Any}()
|
||||||
|
|
||||||
# 1. Process and normalize all keys from the input dictionary
|
# 1. Process and normalize all keys from the input dictionary
|
||||||
processed_dict = OrderedDict{keytype, Any}()
|
processed_dict = OrderedDict{keytype, Any}()
|
||||||
for (k, v) in x
|
for (k, v) in x
|
||||||
if keytype === String
|
if keytype === String
|
||||||
newk = string(k)
|
newk = string(k)
|
||||||
elseif keytype === Symbol
|
elseif keytype === Symbol
|
||||||
newk = Symbol(string(k))
|
newk = Symbol(string(k))
|
||||||
else
|
else
|
||||||
newk = k
|
newk = k
|
||||||
end
|
end
|
||||||
processed_dict[newk] = dictify(v; keytype=keytype, sort_order=sort_order)
|
processed_dict[newk] = dictify(v; keytype=keytype, sort_order=sort_order)
|
||||||
end
|
end
|
||||||
|
|
||||||
# 2. If a sort order is specified, apply it
|
# 2. If a sort order is specified, apply it
|
||||||
if !isnothing(sort_order)
|
if !isnothing(sort_order)
|
||||||
# Normalize the sort_order elements to match the requested keytype
|
# Normalize the sort_order elements to match the requested keytype
|
||||||
normalized_order = map(sort_order) do tk
|
normalized_order = map(sort_order) do tk
|
||||||
if keytype === String
|
if keytype === String
|
||||||
return string(tk)
|
return string(tk)
|
||||||
elseif keytype === Symbol
|
elseif keytype === Symbol
|
||||||
return Symbol(string(tk))
|
return Symbol(string(tk))
|
||||||
else
|
else
|
||||||
return tk
|
return tk
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# First, insert keys that match the requested order
|
# First, insert keys that match the requested order
|
||||||
for target_key in normalized_order
|
for target_key in normalized_order
|
||||||
if haskey(processed_dict, target_key)
|
if haskey(processed_dict, target_key)
|
||||||
out[target_key] = processed_dict[target_key]
|
out[target_key] = processed_dict[target_key]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Then, append any remaining keys that weren't in the sort_order
|
# Then, append any remaining keys that weren't in the sort_order
|
||||||
for (k, v) in processed_dict
|
for (k, v) in processed_dict
|
||||||
if !haskey(out, k)
|
if !haskey(out, k)
|
||||||
out[k] = v
|
out[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# If no sort order is given, just use the processed dict
|
# If no sort order is given, just use the processed dict
|
||||||
out = processed_dict
|
out = processed_dict
|
||||||
end
|
end
|
||||||
|
|
||||||
return out
|
return out
|
||||||
|
|
||||||
# Arrays / vectors: map elements recursively
|
|
||||||
elseif x isa AbstractArray
|
|
||||||
return [dictify(element; keytype=keytype, sort_order=sort_order) for element in x]
|
|
||||||
|
|
||||||
# Everything else: return as-is
|
|
||||||
else
|
|
||||||
return x
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function dictify(x::T; keytype::Type=Any, sort_order::Union{Nothing, Vector}=nothing
|
||||||
|
) where {T<:AbstractArray}
|
||||||
|
return [dictify(element; keytype=keytype, sort_order=sort_order) for element in x]
|
||||||
|
end
|
||||||
|
|
||||||
|
function dictify(x; keytype::Type=Any, sort_order::Union{Nothing, Vector}=nothing
|
||||||
|
)
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user