Utils
Serde.to_flatten
— FunctionSerde.to_flatten(data; delimiter = "_") -> Dict{String, Any}
Transforms a nested dictionary data
(or custom type) into a single-level dictionary. The keys in the new dictionary are created by joining the nested keys (or fieldnames) with delimiter
symbol.
Examples
Flatten the nested dictionary with custom delimiter
symbol.
julia> nested_dict = Dict(
"foo" => 1,
"bar" => Dict(
"foo" => 2,
"baz" => Dict(
"foo" => 3,
),
),
);
julia> Serde.to_flatten(nested_dict; delimiter = "__")
Dict{String, Any} with 3 entries:
:bar__baz__foo => 3
:foo => 1
:bar__foo => 2
Flatten the nested structure.
julia> struct Bar
num::Float64
end
julia> struct Foo
val::Int64
str::String
bar::Bar
end
julia> nested_struct = Foo(1, "a", Bar(1.0));
julia> Serde.to_flatten(nested_struct)
Dict{String, Any} with 3 entries:
:bar_num => 1.0
:val => 1
:str => "a"
Serde.@serde
— Macro@serde decorators... typedef
Helper macro that implements user friendly configuration of the (de)serialization process (see extended deserialization and serialization). Available decorators
:
@default_value
: Used to define default values for fields of declared type (seeSerde.default_value
).@de_name
: Used to defines an alias names for fields of declared type (seeSerde.custom_name
).@ser_name
: Used to define custom output name for fields of declared type (seeSerde.ser_name
).
Next, the syntax template looks like this:
@serde @decor_1 @decor_2 ... struct
var::T | val_1 | val_2 | ...
...
end
Any combination of available decorators is valid. Decorators must be placed between @serde
and struct
keyword. Decorator values belonging to a certain field must be separated by the |
symbol.
Examples
@serde @default_value @de_name @ser_name mutable struct Foo
bar::Int64 | 1 | "first" | "bar"
baz::Int64 | 2 | "baz" | "second"
end
If we do not specify any value, it will be taken from the column corresponding to @default_value
. Notice that bar was initialised with default 1.
julia> deser_json(Foo, """{"baz": 20}""")
Foo(1, 20)
Also, now names from the @de_name
column will be used for deserialization.
julia> deser_json(Foo, """{"first": 30}""")
Foo(30, 2)
Names from the @ser_name
column will be used as output names for serialization.
julia> to_json(Foo(40, 50)) |> print
{"bar":40,"second":50}
Serde.@serde_pascal_case
— Macro@serde_pascal_case T
Marks all fields of type T
with custom names corresponding to PascalCase
.
Field names of type T
must match snake_case
.
See also Serde.custom_name
Examples
julia> struct MySnakes
red_snake::Int64
end
julia> @serde_pascal_case MySnakes
julia> Serde.deser(MySnakes, Dict("RedSnake" => 1))
MySnakes(1)
Serde.@serde_camel_case
— Macro@serde_camel_case T
Marks all fields of type T
with custom names corresponding to camelCase
.
Field names of type T
must match snake_case
.
See also Serde.custom_name
Examples
julia> struct MySnakes
forest_snake::Int64
end
julia> @serde_camel_case MySnakes
julia> Serde.deser(MySnakes, Dict("forestSnake" => 2))
MySnakes(2)
Serde.@serde_kebab_case
— Macro@serde_kebab_case T
Marks all fields of type T
with custom names corresponding to kebab-case
.
Field names of type T
must match snake_case
.
See also Serde.custom_name
Examples
julia> struct MySnakes
pretty_snake::Int64
end
julia> @serde_kebab_case MySnakes
julia> Serde.deser(MySnakes, Dict("pretty-snake" => 3))
MySnakes(3)