@@ -34,41 +34,78 @@ function lower(o::T) where {T<:UnionAPIModel}
3434 end
3535end
3636
37+ struct StyleCtx
38+ location:: Symbol
39+ name:: String
40+ is_explode:: Bool
41+ end
42+
43+ is_deep_explode (sctx:: StyleCtx ) = sctx. name == " deepObject" && sctx. is_explode
44+
45+ function deep_object_to_array (src:: Dict )
46+ keys_are_int = all (key -> occursin (r" ^\d +$" , key), keys (src))
47+ if keys_are_int
48+ sorted_keys = sort (collect (keys (src)), by= x-> parse (Int, x))
49+ final = []
50+ for key in sorted_keys
51+ push! (final, src[key])
52+ end
53+ return final
54+ else
55+ src
56+ end
57+ end
58+
3759to_json (o) = JSON. json (o)
3860
39- from_json (:: Type{Union{Nothing,T}} , json:: Dict{String,Any} ) where {T} = from_json (T, json)
40- from_json (:: Type{T} , json:: Dict{String,Any} ) where {T} = from_json (T (), json)
41- from_json (:: Type{T} , json:: Dict{String,Any} ) where {T <: Dict } = convert (T, json)
42- from_json (:: Type{T} , j:: Dict{String,Any} ) where {T <: String } = to_json (j)
43- from_json (:: Type{Any} , j:: Dict{String,Any} ) = j
61+ from_json (:: Type{Union{Nothing,T}} , json:: Dict{String,Any} ; stylectx= nothing ) where {T} = from_json (T, json; stylectx)
62+ from_json (:: Type{T} , json:: Dict{String,Any} ; stylectx= nothing ) where {T} = from_json (T (), json; stylectx)
63+ from_json (:: Type{T} , json:: Dict{String,Any} ; stylectx= nothing ) where {T <: Dict } = convert (T, json)
64+ from_json (:: Type{T} , j:: Dict{String,Any} ; stylectx= nothing ) where {T <: String } = to_json (j)
65+ from_json (:: Type{Any} , j:: Dict{String,Any} ; stylectx= nothing ) = j
66+ from_json (:: Type{Vector{T}} , j:: Vector{Any} ; stylectx= nothing ) where {T} = j
67+
68+ function from_json (:: Type{Vector{T}} , json:: Dict{String, Any} ; stylectx= nothing ) where {T}
69+ if ! isnothing (stylectx) && is_deep_explode (stylectx)
70+ cvt = deep_object_to_array (json)
71+ if isa (cvt, Vector)
72+ return from_json (Vector{T}, cvt; stylectx)
73+ else
74+ return from_json (T, json; stylectx)
75+ end
76+ else
77+ return from_json (T, json; stylectx)
78+ end
79+ end
4480
45- function from_json (o:: T , json:: Dict{String,Any} ) where {T <: UnionAPIModel }
46- return from_json (o, :value , json)
81+ function from_json (o:: T , json:: Dict{String,Any} ;stylectx = nothing ) where {T <: UnionAPIModel }
82+ return from_json (o, :value , json;stylectx )
4783end
4884
49- from_json (:: Type{T} , val:: Union{String,Real} ) where {T <: UnionAPIModel } = T (val)
50- function from_json (o:: T , val:: Union{String,Real} ) where {T <: UnionAPIModel }
85+ from_json (:: Type{T} , val:: Union{String,Real} ;stylectx = nothing ) where {T <: UnionAPIModel } = T (val)
86+ function from_json (o:: T , val:: Union{String,Real} ;stylectx = nothing ) where {T <: UnionAPIModel }
5187 o. value = val
5288 return o
5389end
5490
55- function from_json (o:: T , json:: Dict{String,Any} ) where {T <: APIModel }
91+ function from_json (o:: T , json:: Dict{String,Any} ;stylectx = nothing ) where {T <: APIModel }
5692 jsonkeys = [Symbol (k) for k in keys (json)]
5793 for name in intersect (propertynames (o), jsonkeys)
58- from_json (o, name, json[String (name)])
94+ from_json (o, name, json[String (name)];stylectx )
5995 end
6096 return o
6197end
6298
63- function from_json (o:: T , name:: Symbol , json:: Dict{String,Any} ) where {T <: APIModel }
99+ function from_json (o:: T , name:: Symbol , json:: Dict{String,Any} ;stylectx = nothing ) where {T <: APIModel }
64100 ftype = (T <: UnionAPIModel ) ? property_type (T, name, json) : property_type (T, name)
65- fval = from_json (ftype, json)
101+ fval = from_json (ftype, json; stylectx )
66102 setfield! (o, name, convert (ftype, fval))
67103 return o
68104end
69105
70- function from_json (o:: T , name:: Symbol , v) where {T <: APIModel }
106+ function from_json (o:: T , name:: Symbol , v; stylectx = nothing ) where {T <: APIModel }
71107 ftype = (T <: UnionAPIModel ) ? property_type (T, name, Dict {String,Any} ()) : property_type (T, name)
108+ atype = isa (ftype, Union) ? ((ftype. a === Nothing) ? ftype. b : ftype. a) : ftype
72109 if ftype === Any
73110 setfield! (o, name, v)
74111 elseif ZonedDateTime <: ftype
@@ -80,13 +117,15 @@ function from_json(o::T, name::Symbol, v) where {T <: APIModel}
80117 elseif String <: ftype && isa (v, Real)
81118 # string numbers can have format specifiers that allow numbers, ensure they are converted to strings
82119 setfield! (o, name, string (v))
120+ elseif atype <: Real && isa (v, AbstractString)
121+ setfield! (o, name, parse (atype, v))
83122 else
84123 setfield! (o, name, convert (ftype, v))
85124 end
86125 return o
87126end
88127
89- function from_json (o:: T , name:: Symbol , v:: Vector ) where {T <: APIModel }
128+ function from_json (o:: T , name:: Symbol , v:: Vector ; stylectx = nothing ) where {T <: APIModel }
90129 # in Julia we can not support JSON null unless the element type is explicitly set to support it
91130 ftype = property_type (T, name)
92131
@@ -111,7 +150,7 @@ function from_json(o::T, name::Symbol, v::Vector) where {T <: APIModel}
111150 if (vtype <: Vector ) && (veltype <: OpenAPI.UnionAPIModel )
112151 vec = veltype[]
113152 for vecelem in v
114- push! (vec, from_json (veltype (), :value , vecelem))
153+ push! (vec, from_json (veltype (), :value , vecelem;stylectx ))
115154 end
116155 setfield! (o, name, vec)
117156 elseif (vtype <: Vector ) && (veltype <: OpenAPI.APIModel )
@@ -129,7 +168,7 @@ function from_json(o::T, name::Symbol, v::Vector) where {T <: APIModel}
129168 return o
130169end
131170
132- function from_json (o:: T , name:: Symbol , :: Nothing ) where {T <: APIModel }
171+ function from_json (o:: T , name:: Symbol , :: Nothing ;stylectx = nothing ) where {T <: APIModel }
133172 setfield! (o, name, nothing )
134173 return o
135- end
174+ end
0 commit comments