Strategies
Strategies control field naming and other serialization/deserialization behaviour globally. Pass a strategy as the first argument to any from_* or to_* function.
Built-in strategies
Serde.CamelCase — Type
CamelCaseSerialization/deserialization context that renames all struct fields to camelCase.
Pass a CamelCase() instance as the first argument to any from_* or to_* function to have all snake_case field names automatically converted to camelCase in the output (serialization) and looked up as camelCase in the input (deserialization).
This is the most common convention in JSON APIs. Field-specific overrides via Serde.ser_name or Serde.deser_name still take precedence over the context when defined for a type.
Examples
julia> struct Config
database_host::String
max_retries::Int
end
julia> to_json(CamelCase(), Config("localhost", 3))
"{"databaseHost":"localhost","maxRetries":3}"
julia> from_json(CamelCase(), Config, "{"databaseHost":"localhost","maxRetries":3}")
Config("localhost", 3)See also: PascalCase, KebabCase, LowerCase.
Serde.PascalCase — Type
PascalCaseSerialization/deserialization context that renames all struct fields to PascalCase.
All snake_case field names are converted to PascalCase (every word capitalized, no separator) when serializing or deserializing with this context.
Examples
julia> struct Event
event_type::String
user_id::Int
end
julia> to_json(PascalCase(), Event("login", 42))
"{"EventType":"login","UserId":42}"
julia> from_json(PascalCase(), Event, "{"EventType":"login","UserId":42}")
Event("login", 42)Serde.KebabCase — Type
KebabCaseSerialization/deserialization context that renames all struct fields to kebab-case.
All snake_case field names are converted to kebab-case (words joined with -, all lowercase) when serializing or deserializing with this context.
Examples
julia> struct Header
content_type::String
accept_encoding::String
end
julia> to_json(KebabCase(), Header("application/json", "gzip"))
"{"content-type":"application/json","accept-encoding":"gzip"}"See also: CamelCase, PascalCase, LowerCase.
Serde.LowerCase — Type
LowerCaseSerialization/deserialization context that lowercases all struct field names.
All field names are converted to lowercase (no separator changes, only case folding) when serializing or deserializing with this context.
Examples
julia> struct Params
Symbol_Name::String
Count::Int
end
julia> to_json(LowerCase(), Params("foo", 1))
"{"symbol_name":"foo","count":1}"See also: CamelCase, PascalCase, KebabCase.
Composing strategies
Serde.With — Type
With(strategies...) -> WithCompose multiple serialization/deserialization strategies into a single strategy.
With delegates every trait call to its component strategies using a well-defined combination rule per trait:
| Trait | Rule |
|---|---|
ser_name / deser_name | chain: each strategy receives the previous strategy's output |
ser_skip | OR: skip if any strategy says skip |
ser_value / ser_type / deser_transform | chain: apply each strategy in order |
has_default | OR: field has a default if any strategy provides one |
deser_default | first-wins: use the first strategy that has a default |
isempty_value | OR: treat as empty if any strategy says so |
deser_validate | all: run every strategy's validator |
tag_key | first non-nothing strategy wins |
tag_subtypes | first non-empty strategy wins |
Examples
julia> struct Order
order_id::Int
total_amount::Float64
end
julia> to_json(With(CamelCase()), Order(1, 99.9))
"{"orderId":1,"totalAmount":99.9}"
julia> from_json(With(CamelCase()), Order, "{"orderId":1,"totalAmount":99.9}")
Order(1, 99.9)See also: CamelCase, PascalCase, KebabCase.
Default strategy
Serde.DefaultStrategy — Type
Serde.DefaultStrategyDefault context singleton used when no explicit context is supplied to from_* / to_* functions.
You do not normally need to construct or pass DefaultStrategy() directly — it is inserted automatically. It exists so that trait methods can be dispatched on ::DefaultStrategy to define package-wide defaults that differ from the built-in behaviour.
Examples
# Apply a custom default deser_name for all types when no context is given:
Serde.deser_name(::Serde.DefaultStrategy, ::Type{T}, ::Val{x}) where {T,x} =
Symbol(replace(string(x), "_" => "-"))See also: CamelCase, PascalCase, KebabCase.