3 Commits
v0.4.0 ... main

Author SHA1 Message Date
f33f4f0790 up version 2026-06-24 12:55:40 +07:00
ton
fcf2044dd9 Merge pull request 'update' (#7) from v0.4.0-dictify_key into main
Reviewed-on: #7
2026-06-24 05:33:31 +00:00
05d8cb9c02 update 2026-06-24 12:29:01 +07:00
2 changed files with 55 additions and 79 deletions

View File

@@ -1,6 +1,6 @@
name = "GeneralUtils"
uuid = "c6c72f09-b708-4ac8-ac7c-2084d70108fe"
version = "0.3.2"
version = "0.4.0"
authors = ["tonaerospace <tonaerospace.etc@gmail.com>"]
[deps]

View File

@@ -202,13 +202,13 @@ end
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.
- `stringkey::Bool=false`
If `true`, every dictionary key is converted to `String` via `string(k)`.
If `false`, original key objects are preserved (useful when keys are already
`String`, `Symbol`, or other types you want to keep).
If `true`, every dictionary key is converted to `String` via `string(k)`. This parameter is ignored when `keytype` is explicitly set.
# 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.
# Notes
@@ -236,79 +236,46 @@ julia> d = Dict(
julia jsonstring = JSON.json(d)
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
julia> A2 = dictify(A1)
Dict{Any,Any} with 3 entries:
julia> A2 = dictify(A1; keytype=String)
Dict{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])))
julia> A3 = dictify(A1; stringkey=false) # preserves original key objects
julia> B1 = dictify(d; stringkey=true) # convert all keys in to string
Dict{Any, Any} with 3 entries:
"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
"a" => 4
```
julia> A3 = dictify(A1; keytype=Symbol)
Dict{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])))
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
if x isa AbstractDict
# choose output key type container (String keys when requested)
out = Dict{Any,Any}()
# choose output key type container
out = Dict{keytype,Any}()
for (k,v) in x
newk = stringkey ? string(k) : k
out[newk] = dictify(v; stringkey=stringkey)
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; stringkey=stringkey) for element in x]
return [dictify(element; keytype=keytype) for element in x]
# everything else: return as-is (primitives, numbers, strings, etc.)
else
return x
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 --------------------------------------------- #
""" Recursively convert dictionary-like variable (e.g. JSON.Object) into a dictionary.
@@ -328,13 +295,11 @@ end
processed recursively.
# Keyword Arguments
- `stringkey::Bool=false`
If `true`, every dictionary key is converted to `String` via `string(k)`.
If `false`, original key objects are preserved (useful when keys are already
`String`, `Symbol`, or other types you want to keep).
- `keytype::Type=Any`
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.
# 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.
# Notes
@@ -361,41 +326,52 @@ julia> d = Dict(
julia jsonstring = JSON.json(d)
julia> A1 = JSON.parse(jsonstring) # A1 type is JSON.Object
julia> A2 = OrderedDict(A1)
Dict{Any,Any} with 3 entries:
julia> A2 = ordereddictify(A1; keytype=String)
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 = OrderedDict(A1; stringkey=false) # preserves original key objects
julia> B1 = OrderedDict(d; stringkey=true) # convert all keys in to string
Dict{Any, Any} with 3 entries:
"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)
julia> A3 = ordereddictify(A1; keytype=Symbol)
OrderedDict{Symbol,Any} with 3 entries:
:a => 4
: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
"a" => 4
```
Ref. https://github.com/andyferris/Dictionaries.jl
"""
function ordereddictify(x; stringkey::Bool=false)
function ordereddictify(x; keytype::Type=Any)
# Dict-like objects
if x isa AbstractDict
# choose output key type container (String keys when requested)
out = OrderedDict{Any,Any}()
# choose output key type container
out = OrderedDict{keytype,Any}()
for (k,v) in x
newk = stringkey ? string(k) : k
out[newk] = ordereddictify(v; stringkey=stringkey)
if keytype === String
newk = string(k)
elseif keytype === Symbol
newk = Symbol(string(k))
else
newk = k
end
out[newk] = ordereddictify(v; keytype=keytype)
end
return out
# Arrays / vectors: map elements recursively and return a Vector{Any}
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.)
else
return x
end
end
#------------------------------------------------------------------------------------------------100
#----------------------------------------------100---------------------------------------------
"""
print time of cpu executtion at the line inwhich this macro is used