JSON
Parsing
Serde.SerdeJson.parse_json — Function
parse_json(x::Union{AbstractString, Vector{UInt8}}; dict_type = Dict{String,Any}) -> AnyParse a JSON string or byte vector into Julia data structures without mapping to a target type.
Returns the raw parsed value: a Dict{String,Any} for JSON objects, a Vector{Any} for arrays, or a scalar (String, Int64, Float64, Bool, Nothing) for primitive values.
Arguments
x: JSON text as aStringorVector{UInt8}.
Keyword arguments
dict_type::Type{<:AbstractDict}: concrete dict type to use for JSON objects (defaultDict{String,Any}).
Returns
The parsed Julia value.
Throws
ParseError: ifxcontains malformed JSON.
Examples
julia> parse_json("{"x":1,"y":2}")
Dict{String, Any}("x" => 1, "y" => 2)
julia> parse_json("[1, 2, 3]")
3-element Vector{Any}: [1, 2, 3]
julia> parse_json("42")
42See also: from_json, try_from_json.
Deserialization
Serde.SerdeJson.from_json — Function
from_json(::Type{T}, x::Union{AbstractString, Vector{UInt8}}) -> T
from_json(strategy, ::Type{T}, x::Union{AbstractString, Vector{UInt8}}) -> T
from_json(f::Function, x) -> AnyParse a JSON string and deserialize it into type T.
The JSON backend uses a high-performance C parser (yyjson) and deserializes struct types directly from the parse tree without creating an intermediate Dict.
When called with a function f as the first argument, the raw parsed value is passed to f to determine the target type dynamically: to_deser(f(parsed), parsed).
Pass a context object strategy (e.g. CamelCase()) as the first argument to apply global field-renaming during deserialization.
Arguments
::Type{T}: target type to construct.x: JSON text as aStringorVector{UInt8}.strategy: optional context object for field renaming.f::Function: function(parsed_data) -> Typefor dynamic type selection.
Returns
A value of type T.
Throws
ParseError: ifxis malformed JSON.MissingFieldError: if a required struct field is absent.TypeMismatchError: if a value cannot be coerced to the field type.ValidationError: if a customdeser_validatecheck fails.
Examples
julia> struct Point; x::Int; y::Int; end
julia> from_json(Point, "{"x":1,"y":2}")
Point(1, 2)
julia> from_json(CamelCase(), Point, "{"x":1,"y":2}")
Point(1, 2)
julia> from_json(Vector{Int}, "[1,2,3]")
3-element Vector{Int64}: [1, 2, 3]See also: try_from_json, parse_json, to_json.
Serde.SerdeJson.try_from_json — Function
try_from_json(::Type{T}, x) -> Union{T, SerdeError}
try_from_json(strategy, ::Type{T}, x) -> Union{T, SerdeError}Like from_json but returns a SerdeError instead of throwing on failure.
Useful when processing untrusted input where parse or deserialization errors are expected and should be handled without try/catch boilerplate.
Returns
Ton success.- A
ParseErrororDeserErrorsubtype on failure.
Examples
julia> struct Point; x::Int; y::Int; end
julia> result = try_from_json(Point, "{"x":1,"y":2}")
Point(1, 2)
julia> result = try_from_json(Point, "not json")
ParseError (JSON): ...
julia> result isa SerdeError
trueSee also: from_json, SerdeError.
Serialization
Serde.SerdeJson.to_json — Function
to_json(data; pretty::Bool = false) -> String
to_json(strategy, data; pretty::Bool = false) -> String
to_json(f::Function, data; pretty::Bool = false) -> String
to_json(io::IO, data; pretty::Bool = false)Serialize data into a JSON string.
The compact (non-pretty) path uses a native C yyjson writer for maximum throughput. The pretty path uses an IOBuffer-based writer that produces two-space indented output.
Pass a context object strategy (e.g. CamelCase()) as the first argument to apply global field-renaming during serialization.
Pass a function f(::Type{T}) -> Tuple{Symbol...} to include only a specific subset of fields in the output. The function receives the struct type and must return a tuple of field name symbols.
When an io::IO is given as the first argument, JSON is written directly to that stream and the function returns nothing.
All serialization traits (Serde.ser_name, Serde.ser_value, Serde.ser_type, Serde.ser_skip) are applied.
Arguments
data: the value to serialize (struct, dict, array, or scalar).strategy: optional context object.f::Function: optional field selector(Type) -> Tuple{Symbol...}.io::IO: optional output stream.
Keyword arguments
pretty::Bool = false: emit indented, human-readable JSON.
Returns
String (or nothing when writing to io).
Examples
julia> struct Point; x::Int; y::Int; end
julia> to_json(Point(1, 2))
"{"x":1,"y":2}"
julia> to_json(CamelCase(), Point(1, 2)) # no renaming needed here, fields are single-char
"{"x":1,"y":2}"
julia> to_json(Point(1, 2); pretty=true) |> println
{
"x":1,
"y":2
}
julia> to_json(T -> (:x,), Point(1, 2)) # only serialize :x
"{"x":1}"See also: to_pretty_json, from_json.
Serde.SerdeJson.to_pretty_json — Function
to_pretty_json(data) -> String
to_pretty_json(strategy, data) -> String
to_pretty_json(f::Function, data) -> StringSerialize data into a human-readable, indented JSON string.
Alias for to_json(data; pretty=true). Accepts the same optional context strategy and field-selector function f as to_json.
Examples
julia> struct Config; host::String; port::Int; end
julia> to_pretty_json(Config("localhost", 8080)) |> println
{
"host":"localhost",
"port":8080
}See also: to_json.