update
This commit is contained in:
132
src/interface.jl
132
src/interface.jl
@@ -202,13 +202,13 @@ end
|
|||||||
if it is an `AbstractArray` its elements will be processed recursively.
|
if it is an `AbstractArray` its elements will be processed recursively.
|
||||||
|
|
||||||
# Keyword Arguments
|
# 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.
|
||||||
- `stringkey::Bool=false`
|
- `stringkey::Bool=false`
|
||||||
If `true`, every dictionary key is converted to `String` via `string(k)`.
|
If `true`, every dictionary key is converted to `String` via `string(k)`. This parameter is ignored when `keytype` is explicitly set.
|
||||||
If `false`, original key objects are preserved (useful when keys are already
|
|
||||||
`String`, `Symbol`, or other types you want to keep).
|
|
||||||
|
|
||||||
# Return
|
# Return
|
||||||
- A newly allocated nested structure composed of `Dict{Any,Any}` and
|
- A newly allocated nested structure composed of `Dict{keytype,Any}` and
|
||||||
`Vector{Any}` that mirrors the input shape but uses plain Julia containers.
|
`Vector{Any}` that mirrors the input shape but uses plain Julia containers.
|
||||||
|
|
||||||
# Notes
|
# Notes
|
||||||
@@ -236,79 +236,46 @@ julia> d = Dict(
|
|||||||
|
|
||||||
julia jsonstring = JSON.json(d)
|
julia jsonstring = JSON.json(d)
|
||||||
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
||||||
julia> A2 = dictify(A1)
|
julia> A2 = dictify(A1; keytype=String)
|
||||||
Dict{Any,Any} with 3 entries:
|
Dict{String,Any} with 3 entries:
|
||||||
"a" => 4
|
"a" => 4
|
||||||
"b" => 6
|
"b" => 6
|
||||||
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
"c" => Dict("d"=>7, "e"=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
|
|
||||||
julia> A3 = dictify(A1; stringkey=false) # preserves original key objects
|
julia> A3 = dictify(A1; keytype=Symbol)
|
||||||
julia> B1 = dictify(d; stringkey=true) # convert all keys in to string
|
Dict{Symbol,Any} with 3 entries:
|
||||||
Dict{Any, Any} with 3 entries:
|
:a => 4
|
||||||
"c" => Dict{Any, Any}("e"=>Dict{Any, Any}("f"=>"hey", "g"=>Dict{Any, Any}("world"=>Any[1, "2", 3, Dict{Any, Any}("dd"=>4.7)])), "d"=>7)
|
:b => 6
|
||||||
"b" => 6
|
:c => Dict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
"a" => 4
|
|
||||||
```
|
julia> B1 = dictify(d; keytype=String)
|
||||||
|
Dict{String, Any} with 3 entries:
|
||||||
"""
|
"""
|
||||||
function dictify(x; stringkey::Bool=false)
|
function dictify(x; keytype::Type=Any)
|
||||||
# Dict-like objects
|
# Dict-like objects
|
||||||
if x isa AbstractDict
|
if x isa AbstractDict
|
||||||
# choose output key type container (String keys when requested)
|
# choose output key type container
|
||||||
out = Dict{Any,Any}()
|
out = Dict{keytype,Any}()
|
||||||
for (k,v) in x
|
for (k,v) in x
|
||||||
newk = stringkey ? string(k) : k
|
if keytype === String
|
||||||
out[newk] = dictify(v; stringkey=stringkey)
|
newk = string(k)
|
||||||
|
elseif keytype === Symbol
|
||||||
|
newk = Symbol(string(k))
|
||||||
|
else
|
||||||
|
newk = k
|
||||||
|
end
|
||||||
|
out[newk] = dictify(v; keytype=keytype)
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
||||||
elseif x isa AbstractArray
|
elseif x isa AbstractArray
|
||||||
return [dictify(element; stringkey=stringkey) for element in x]
|
return [dictify(element; keytype=keytype) for element in x]
|
||||||
# everything else: return as-is (primitives, numbers, strings, etc.)
|
# everything else: return as-is (primitives, numbers, strings, etc.)
|
||||||
else
|
else
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
||||||
|
|
||||||
""" Array_to_JSON_str(data::AbstractArray)
|
|
||||||
|
|
||||||
encode Array to JSON String
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
a = [1.23 4.7889; 9987.1 -123.07; -0.0027 -6.75]
|
|
||||||
jsonStr = Array_to_JSON_str(a)
|
|
||||||
jsonStr = "{\"Array\":[[1.23,9987.1,-0.0027],[4.7889,-123.07,-6.75]],\"size\":[3,2]}"
|
|
||||||
"""
|
|
||||||
function Array_to_JSON_str(data::AbstractArray)
|
|
||||||
d = Dict("Array"=> data, "size"=>size(data))
|
|
||||||
jsonStr = JSON.json(d)
|
|
||||||
return jsonStr
|
|
||||||
end
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
||||||
|
|
||||||
""" JSON_str_to_Array(json_str::String)
|
|
||||||
|
|
||||||
decode JSON String to Array
|
|
||||||
|
|
||||||
# Example
|
|
||||||
|
|
||||||
jsonStr = "{\"Array\":[[1.23,9987.1,-0.0027],[4.7889,-123.07,-6.75]],\"size\":[3,2]}"
|
|
||||||
a = JSON_str_to_Array(jsonStr)
|
|
||||||
|
|
||||||
"""
|
|
||||||
function JSON_str_to_Array(jsonStr::String)
|
|
||||||
jsonObj = JSON.parse(jsonStr)
|
|
||||||
a = Array(jsonObj.Array)
|
|
||||||
array = hcat(a...)
|
|
||||||
return array
|
|
||||||
end
|
|
||||||
|
|
||||||
# ---------------------------------------------- 100 --------------------------------------------- #
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
||||||
|
|
||||||
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
|
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
|
||||||
@@ -328,13 +295,11 @@ end
|
|||||||
processed recursively.
|
processed recursively.
|
||||||
|
|
||||||
# Keyword Arguments
|
# Keyword Arguments
|
||||||
- `stringkey::Bool=false`
|
- `keytype::Type=Any`
|
||||||
If `true`, every dictionary key is converted to `String` via `string(k)`.
|
The key type for the output Dict. Use `String` for `OrderedDict{String,Any}`, `Symbol` for `OrderedDict{Symbol,Any}`, or `Any` to preserve original key types.
|
||||||
If `false`, original key objects are preserved (useful when keys are already
|
|
||||||
`String`, `Symbol`, or other types you want to keep).
|
|
||||||
|
|
||||||
# Return
|
# Return
|
||||||
- A newly allocated nested structure composed of `OrderedDict{Any,Any}` and
|
- A newly allocated nested structure composed of `OrderedDict{keytype,Any}` and
|
||||||
`Vector{Any}` that mirrors the input shape but uses ordered Julia containers.
|
`Vector{Any}` that mirrors the input shape but uses ordered Julia containers.
|
||||||
|
|
||||||
# Notes
|
# Notes
|
||||||
@@ -361,41 +326,52 @@ julia> d = Dict(
|
|||||||
|
|
||||||
julia jsonstring = JSON.json(d)
|
julia jsonstring = JSON.json(d)
|
||||||
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
|
||||||
julia> A2 = OrderedDict(A1)
|
julia> A2 = ordereddictify(A1; keytype=String)
|
||||||
Dict{Any,Any} with 3 entries:
|
OrderedDict{String,Any} with 3 entries:
|
||||||
"a" => 4
|
"a" => 4
|
||||||
"b" => 6
|
"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 = OrderedDict(A1; stringkey=false) # preserves original key objects
|
julia> A3 = ordereddictify(A1; keytype=Symbol)
|
||||||
julia> B1 = OrderedDict(d; stringkey=true) # convert all keys in to string
|
OrderedDict{Symbol,Any} with 3 entries:
|
||||||
Dict{Any, Any} with 3 entries:
|
:a => 4
|
||||||
"c" => Dict{Any, Any}("e"=>Dict{Any, Any}("f"=>"hey", "g"=>Dict{Any, Any}("world"=>Any[1, "2", 3, Dict{Any, Any}("dd"=>4.7)])), "d"=>7)
|
:b => 6
|
||||||
|
:c => OrderedDict(:d=>7, :e=>Dict("f"=>"hey", "g"=>Dict("world"=>[1, "2", 3, 4.7])))
|
||||||
|
|
||||||
|
julia> B1 = ordereddictify(d; keytype=String)
|
||||||
|
OrderedDict{String, Any} with 3 entries:
|
||||||
|
"c" => OrderedDict{String, Any}("e"=>OrderedDict{String, Any}("f"=>"hey", "g"=>OrderedDict{String, Any}("world"=>Any[1, "2", 3, OrderedDict{String, Any}("dd"=>4.7)])), "d"=>7)
|
||||||
"b" => 6
|
"b" => 6
|
||||||
"a" => 4
|
"a" => 4
|
||||||
```
|
```
|
||||||
Ref. https://github.com/andyferris/Dictionaries.jl
|
Ref. https://github.com/andyferris/Dictionaries.jl
|
||||||
"""
|
"""
|
||||||
function ordereddictify(x; stringkey::Bool=false)
|
function ordereddictify(x; keytype::Type=Any)
|
||||||
# Dict-like objects
|
# Dict-like objects
|
||||||
if x isa AbstractDict
|
if x isa AbstractDict
|
||||||
# choose output key type container (String keys when requested)
|
# choose output key type container
|
||||||
out = OrderedDict{Any,Any}()
|
out = OrderedDict{keytype,Any}()
|
||||||
for (k,v) in x
|
for (k,v) in x
|
||||||
newk = stringkey ? string(k) : k
|
if keytype === String
|
||||||
out[newk] = ordereddictify(v; stringkey=stringkey)
|
newk = string(k)
|
||||||
|
elseif keytype === Symbol
|
||||||
|
newk = Symbol(string(k))
|
||||||
|
else
|
||||||
|
newk = k
|
||||||
|
end
|
||||||
|
out[newk] = ordereddictify(v; keytype=keytype)
|
||||||
end
|
end
|
||||||
return out
|
return out
|
||||||
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
# Arrays / vectors: map elements recursively and return a Vector{Any}
|
||||||
elseif x isa AbstractArray
|
elseif x isa AbstractArray
|
||||||
return [ordereddictify(element; stringkey=stringkey) for element in x]
|
return [ordereddictify(element; keytype=keytype) for element in x]
|
||||||
# everything else: return as-is (primitives, numbers, strings, etc.)
|
# everything else: return as-is (primitives, numbers, strings, etc.)
|
||||||
else
|
else
|
||||||
return x
|
return x
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
#------------------------------------------------------------------------------------------------100
|
#----------------------------------------------100---------------------------------------------
|
||||||
|
|
||||||
"""
|
"""
|
||||||
print time of cpu executtion at the line inwhich this macro is used
|
print time of cpu executtion at the line inwhich this macro is used
|
||||||
|
|||||||
Reference in New Issue
Block a user