-
Notifications
You must be signed in to change notification settings - Fork 98
/
Json.jl
64 lines (57 loc) · 1.24 KB
/
Json.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
from_json(::Type{T},s::AbstractString) where T
"""
function from_json(::Type{T},s::AbstractString) where T
json_dict = JSON.parse(s;inttype=Int)
dict = _decode_json_dict(json_dict)
from_dict(T,dict)
end
"""
from_json_file(::Type{T},s::AbstractString) where T
"""
function from_json_file(::Type{T},s::AbstractString) where T
json_dict = JSON.parsefile(s;inttype=Int)
dict = _decode_json_dict(json_dict)
from_dict(T,dict)
end
"""
to_json(object)
"""
function to_json(object)
dict = to_dict(object)
JSON.json(dict)
end
"""
to_json_file(object,filename)
"""
function to_json_file(object,filename)
dict = to_dict(object)
open(filename,"w") do f
JSON.print(f,dict)
end
end
JSON.lower(object::GridapType) = to_dict(object)
function _decode_json_dict(json_dict::Dict{String,Any})
dict = Dict{Symbol,Any}()
for k in keys(json_dict)
v = json_dict[k]
if isa(v,Dict{String,Any})
w = _decode_json_dict(v)
elseif isa(v,AbstractVector)
_w = []
for vi in v
if isa(vi,Dict{String,Any})
wi = _decode_json_dict(vi)
else
wi = vi
end
push!(_w,wi)
end
w = collect(_w)
else
w = v
end
dict[Symbol(k)] = w
end
dict
end