Extended serialization
Serde.jl users have the flexibility to customize the serialization process of their data.
Changing output names
If you want to change the output names of your custom type, you just have to extend the Serde.<SubModule>.ser_name function. This approach is supported by the following serialization methods:
to_json(SerJsonsubmodule).to_toml(SerTomlsubmodule).to_query(SerQuerysubmodule).to_xml(SerXmlsubmodule).to_yaml(SerYamlsubmodule).
The return value of your method must be of type Symbol or String. The default signature is:
ser_name(::Type{T}, ::Val{x})::Symbol where {T,x} = xExample
For convenience, we import the necessary SerJson submodule.
using Serde.SerJsonThen, let's define a simple custom type Ticket.
struct Ticket
cost::Int64
endNow, we can add a new method SerJson.ser_name for the custom type Ticket and its field cost.
SerJson.ser_name(::Type{Ticket}, ::Val{:cost}) = :priceAfter that, in the resulting JSON string the field cost will become price.
julia> to_json(Ticket(1000)) |> print
{"price":1000}Handling field values
Also, we can specify how to process certain fields of custom types. In that case, you need extend the Serde.<SubModule>.ser_value function. This approach is supported by such serialization methods:
to_json(SerJsonsubmodule).to_toml(SerTomlsubmodule).to_query(SerQuerysubmodule).to_xml(SerXmlsubmodule).to_yaml(SerYamlsubmodule).
The method can return a value of any type. The default signature is:
ser_value(::Type{T}, ::Val{x}, v::V) where {T,x,V} = vExample
For convenience, we import the necessary SerJson submodule.
using Dates
using Serde.SerJsonThen, let's define a simple custom type Calendar.
struct JuliaBirthday
date::DateTime
endIn the next line, we add a method SerJson.ser_value for the custom type JuliaBirthday and its field date of type DateTime.
function SerJson.ser_value(::Type{JuliaBirthday}, ::Val{:date}, v::DateTime)
return Dates.value(Nanosecond(datetime2unix(v)))
endNow, we will obtain a nanosecond value of the field v.
julia> to_json(JuliaBirthday(DateTime(2012, 2, 14))) |> print
{"date":1329177600}Handling values of specific types
If you want to override how to serialize a specific type, you need extend the Serde.<SubModule>.ser_type function. This approach is supported by the following serialization methods:
to_json(SerJsonsubmodule).to_toml(SerTomlsubmodule).to_query(SerQuerysubmodule).to_xml(SerXmlsubmodule).to_yaml(SerYamlsubmodule).
The method can return a value of any type. The default signature is:
ser_type(::Type{T}, v::V) where {T,V} = vExample
For convenience, we import the necessary SerJson submodule.
using Serde.SerJsonThen, let's define a simple custom type Computer with two string fields.
struct Computer
cpu::String
gpu::String
endAs well, we can add a method SerJson.ser_type for type Computer and all its fields of type String.
SerJson.ser_type(::Type{Computer}, v::String) = uppercase(v)Now, every string field of Computer will be in uppercase.
julia> to_json(Computer("i7-12900", "rtx-4090")) |> print
{"cpu":"I7-12900","gpu":"RTX-4090"}Ignoring fields
Finally, we can specify what fields must be ignored. In this case, you just need to extend the Serde.<SubModule>.ignore_field function. This approach is supported by the following serialization methods:
to_json(SerJsonsubmodule).
The return value of your method must be of type Bool. The default signature is:
ignore_field(::Type{T}, ::Val{x})::Bool where {T,x} = falseExample
For convenience, we import the necessary submodule.
using Serde.SerJsonThen, let's define a simple custom type Box.
struct Box
height::Int64
width::Int64
length::Int64
endLet's add the SerJson.ignore_field method for type Box.
SerJson.ignore_field(::Type{Box}, ::Val{:length}) = trueBecause the field length is ignorable, the resulting JSON string contains only height and width values.
to_json(Box(2, 5, 6)) |> print
{"height":2,"width":5}