This commit is contained in:
2026-07-03 20:54:16 +07:00
parent 0f2a33bcdd
commit 956adf0b93
3 changed files with 205 additions and 119 deletions
+17 -51
View File
@@ -203,44 +203,35 @@ function read_textfile_by_index(folder_path::String, read_file_number::Integer=1
end
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
Two methods are available:
**Method 1** (simple): Converts `AbstractDict` to `Dict` and `AbstractArray` to `Vector{Any}`.
**Method 2** (advanced): Also accepts a `sort_order` keyword argument to control key ordering
in the output `OrderedDict`. When `sort_order` is specified, the function returns an `OrderedDict`
with keys arranged according to the provided order, followed by any remaining keys.
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into an OrderedDict.
The function walks any nested structure composed of `AbstractDict` (e.g., `JSON.Object`,
`Dict`, `OrderedDict`) and `AbstractArray` and produces a new tree where
every dictionary-like node is a plain `Dict` (or `OrderedDict` when `sort_order` is used)
and every array-like node is a `Vector{Any}`. Scalar values (numbers, strings, booleans,
`nothing`, etc.) are returned unchanged.
every dictionary-like node is an `OrderedDict` and every array-like node is a `Vector{Any}`.
Scalar values (numbers, strings, booleans, `nothing`, etc.) are returned unchanged.
Does **not** mutate the input; it always allocates new containers.
# Arguments
- `x`
Any Julia value. If `x` is an `AbstractDict` it will be converted to a `Dict` (or `OrderedDict`);
Any Julia value. If `x` is an `AbstractDict` it will be converted to an `OrderedDict`;
if it is an `AbstractArray` its elements will be processed recursively.
# Keyword Arguments
- `keytype::Type=Any`
The key type for the output Dict. Use `String` for `Dict{String,Any}`, `Symbol` for `Dict{Symbol,Any}`, or `Any` to preserve original key types.
- `sort_order::Union{Nothing, Vector}=nothing` (Method 2 only)
Vector of keys specifying the desired order. When provided, output uses `OrderedDict` with
keys arranged in the specified order first, then any remaining keys appended.
The key type for the output OrderedDict. Use `String` for `OrderedDict{String,Any}`,
`Symbol` for `OrderedDict{Symbol,Any}`, or `Any` to preserve original key types.
- `sort_order::Union{Nothing, Vector}=nothing`
Vector of keys specifying the desired order. Keys are arranged in the specified order
first, followed by any remaining keys.
# Return
- A newly allocated nested structure composed of `Dict{keytype,Any}` (or `OrderedDict{keytype,Any}`
when `sort_order` is specified) and `Vector{Any}` that mirrors the input shape but uses plain Julia containers.
- A newly allocated nested structure composed of `OrderedDict{keytype,Any}` and `Vector{Any}`
that mirrors the input shape but uses ordered Julia containers.
# Notes
- The function treats any `AbstractDict` as a mapping source, so it works with
`JSON.Object`, `Dict`, `OrderedDict`, etc.
- Arrays are returned as `Vector{Any}` with their elements processed recursively.
- When `sort_order` is provided, the function uses `OrderedDict` to preserve key ordering.
# Examples
```jldoctest
@@ -262,22 +253,22 @@ julia> d = Dict(
julia> jsonstring = JSON.json(d)
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
julia> A2 = dictify(A1; keytype=String)
Dict{String,Any} with 3 entries:
OrderedDict{String,Any} with 3 entries:
"a" => 4
"b" => 6
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
"c" => OrderedDict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
julia> A3 = dictify(A1; keytype=Symbol)
Dict{Symbol,Any} with 3 entries:
OrderedDict{Symbol,Any} with 3 entries:
:a => 4
:b => 6
:c => Dict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
:c => OrderedDict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
julia> B1 = dictify(d; keytype=String)
Dict{String, Any} with 3 entries:
OrderedDict{String, Any} with 3 entries:
```
**With sort_order (returns OrderedDict):**
**With sort_order:**
```jldoctest
julia> d = Dict("a"=>1, "b"=>2, "c"=>3)
julia> dictify(d; sort_order=["c", "a"])
@@ -287,31 +278,6 @@ OrderedDict{String,Int} with 3 entries:
"b" => 2
```
"""
function dictify(x; keytype::Type=Any)
# Dict-like objects
if x isa AbstractDict
# choose output key type container
out = Dict{keytype,Any}()
for (k,v) in x
if keytype === String
newk = string(k)
elseif keytype === Symbol
newk = Symbol(string(k))
else
newk = k
end
out[newk] = dictify(v; keytype=keytype)
end
return out
# Arrays / vectors: map elements recursively and return a Vector{Any}
elseif x isa AbstractArray
return [dictify(element; keytype=keytype) for element in x]
# everything else: return as-is (primitives, numbers, strings, etc.)
else
return x
end
end
function dictify(x; keytype::Type=Any, sort_order::Union{Nothing, Vector}=nothing)
# Dict-like objects
if x isa AbstractDict