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.CamelCaseType
CamelCase

Serialization/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.

source
Serde.PascalCaseType
PascalCase

Serialization/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)

See also: CamelCase, KebabCase, LowerCase.

source
Serde.KebabCaseType
KebabCase

Serialization/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.

source
Serde.LowerCaseType
LowerCase

Serialization/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.

source

Composing strategies

Serde.WithType
With(strategies...) -> With

Compose 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:

TraitRule
ser_name / deser_namechain: each strategy receives the previous strategy's output
ser_skipOR: skip if any strategy says skip
ser_value / ser_type / deser_transformchain: apply each strategy in order
has_defaultOR: field has a default if any strategy provides one
deser_defaultfirst-wins: use the first strategy that has a default
isempty_valueOR: treat as empty if any strategy says so
deser_validateall: run every strategy's validator
tag_keyfirst non-nothing strategy wins
tag_subtypesfirst 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.

source

Default strategy

Serde.DefaultStrategyType
Serde.DefaultStrategy

Default 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.

source