diff --git a/dev/.documenter-siteinfo.json b/dev/.documenter-siteinfo.json index 32415b9..5bb3db0 100644 --- a/dev/.documenter-siteinfo.json +++ b/dev/.documenter-siteinfo.json @@ -1 +1 @@ -{"documenter":{"julia_version":"1.10.1","generation_timestamp":"2024-02-24T19:39:14","documenter_version":"1.2.1"}} \ No newline at end of file +{"documenter":{"julia_version":"1.10.1","generation_timestamp":"2024-02-24T19:43:42","documenter_version":"1.2.1"}} \ No newline at end of file diff --git a/dev/index.html b/dev/index.html index efdb9b9..1fd51ca 100644 --- a/dev/index.html +++ b/dev/index.html @@ -46,4 +46,4 @@ end # Serialize the JuliaCon instance to YAML and print it -to_yaml(juliacon) |> print +to_yaml(juliacon) |> print diff --git a/dev/pages/csv/index.html b/dev/pages/csv/index.html index 6d2bc70..de61927 100644 --- a/dev/pages/csv/index.html +++ b/dev/pages/csv/index.html @@ -9,7 +9,7 @@ julia> parse_csv(csv) 2-element Vector{NamedTuple{(:id, :name, :grade), Tuple{String, String, String}}}: (id = "1", name = "Fred", grade = "78.2") - (id = "2", name = "Benny", grade = "82.0")source
Serde.ParCsv.CSVSyntaxErrorType
CSVSyntaxError <: Exception

Exception thrown when a parse_csv fails due to incorrect CSV syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeCsv.deser_csvFunction
deser_csv(::Type{T}, x; kw...) -> Vector{T}

Creates a new object of type T and fill it with values from CSV formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_csv.

Examples

julia> struct Data
+ (id = "2", name = "Benny", grade = "82.0")
source
Serde.ParCsv.CSVSyntaxErrorType
CSVSyntaxError <: Exception

Exception thrown when a parse_csv fails due to incorrect CSV syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeCsv.deser_csvFunction
deser_csv(::Type{T}, x; kw...) -> Vector{T}

Creates a new object of type T and fill it with values from CSV formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_csv.

Examples

julia> struct Data
            id::Int64
            name::String
            grade::Float64
@@ -24,7 +24,7 @@
 julia> deser_csv(Data, csv)
 2-element Vector{Data}:
  Data(1, "Fred", 78.2)
- Data(2, "Benny", 82.0)
source

Serialization

Serde.SerCsv.to_csvFunction
to_csv(data::Vector{T}; kw...) -> String

Uses data element values to make csv rows with fieldnames as columns headers. Type T may be a nested dictionary or a custom type. In case of nested data, names of resulting headers will be concatenate by "_" symbol using dictionary key-names or structure field names.

Keyword arguments

  • delimiter::String = ",": The delimiter that will be used in the returned csv string.
  • headers::Vector{String} = String[]: Specifies which column headers will be used and in what order.

Examples

Converting a vector of regular dictionaries with fixed headers order.

julia> data = [
+ Data(2, "Benny", 82.0)
source

Serialization

Serde.SerCsv.to_csvFunction
to_csv(data::Vector{T}; kw...) -> String

Uses data element values to make csv rows with fieldnames as columns headers. Type T may be a nested dictionary or a custom type. In case of nested data, names of resulting headers will be concatenate by "_" symbol using dictionary key-names or structure field names.

Keyword arguments

  • delimiter::String = ",": The delimiter that will be used in the returned csv string.
  • headers::Vector{String} = String[]: Specifies which column headers will be used and in what order.

Examples

Converting a vector of regular dictionaries with fixed headers order.

julia> data = [
            Dict("id" => 1, "name" => "Jack"),
            Dict( "id" => 2, "name" => "Bob"),
        ];
@@ -61,4 +61,4 @@
 julia> to_csv(data) |> print
 str,val
 a,1
-b,2
source
+b,2source diff --git a/dev/pages/extended_de/index.html b/dev/pages/extended_de/index.html index ff02c20..8249360 100644 --- a/dev/pages/extended_de/index.html +++ b/dev/pages/extended_de/index.html @@ -15,7 +15,7 @@ julia> person_data = Dict("name" => "Michael", "age" => 25, "info" => info_data); julia> Serde.deser(Person, person_data) -Person("Michael", 25, Info(12, 2500))source

Custom deserialization behavior

If you need to deserialize non-standard custom data types, it will be useful to define a behavior to handle them.

Serde.deserMethod
Serde.deser(::Type{T}, ::Type{E}, data::D) -> E

Internal function that is used to deserialize data to fields with type E of custom type T. Supports user overriding for custom types.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

Examples:

Let's make a custom type Order with fields price and date.

using Dates
+Person("Michael", 25, Info(12, 2500))
source

Custom deserialization behavior

If you need to deserialize non-standard custom data types, it will be useful to define a behavior to handle them.

Serde.deserMethod
Serde.deser(::Type{T}, ::Type{E}, data::D) -> E

Internal function that is used to deserialize data to fields with type E of custom type T. Supports user overriding for custom types.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

Examples:

Let's make a custom type Order with fields price and date.

using Dates
 
 struct Order
     price::Int64
@@ -27,7 +27,7 @@
 )::E where {T<:Order,E<:DateTime}
     return DateTime(x)
 end

After that, if we try to deserialize a dictionary that has a key date with a String value, it will correctly convert the String to a DateTime value.

julia> Serde.deser(Order, Dict("price" => 1000, "date" => "2024-01-01T10:20:30"))
-Order(1000, DateTime("2024-01-01T10:20:30"))
source

Empty values handling

We can also determine which data types and their values will be treated as nothing.

Base.isemptyFunction
Serde.isempty(::Type{T}, x) -> false

This function determines the condition under which the passed value x for some custom type T can be treated as nothing. Supports user overriding for custom types. Initially, all values are set to false.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.nulltype, Serde.default_value.

Examples:

Let's make a custom type Computer with the following fields. The gpu field may be either a String or Nothing.

struct Computer
+Order(1000, DateTime("2024-01-01T10:20:30"))
source

Empty values handling

We can also determine which data types and their values will be treated as nothing.

Base.isemptyFunction
Serde.isempty(::Type{T}, x) -> false

This function determines the condition under which the passed value x for some custom type T can be treated as nothing. Supports user overriding for custom types. Initially, all values are set to false.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.nulltype, Serde.default_value.

Examples:

Let's make a custom type Computer with the following fields. The gpu field may be either a String or Nothing.

struct Computer
     cpu::String
     ram::Int64
     gpu::Union{Nothing,String}
@@ -37,21 +37,21 @@
 Computer("i7-12900", 32, "rtx-4090")
 
 julia> Serde.deser(Computer, Dict("cpu" => "i3-12100", "ram" => 16, "gpu" => ""))
-Computer("i3-12100", 16, nothing)
source

Names aliases

Sometimes, the field names of the incoming data structure differ from their intended destination. In this case, it is convenient to specify name aliases.

Serde.custom_nameFunction
Serde.custom_name(::Type{T}, ::Val{x}) -> x

This function is used to define an alias name for field x of type T. Supports user overriding for custom types. Initially, all passed names must be equivalent to the target names. Methods of this function must return a Symbol or a String value.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

Examples:

Let's make a custom type Phone with one field price.

struct Phone
+Computer("i3-12100", 16, nothing)
source

Names aliases

Sometimes, the field names of the incoming data structure differ from their intended destination. In this case, it is convenient to specify name aliases.

Serde.custom_nameFunction
Serde.custom_name(::Type{T}, ::Val{x}) -> x

This function is used to define an alias name for field x of type T. Supports user overriding for custom types. Initially, all passed names must be equivalent to the target names. Methods of this function must return a Symbol or a String value.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

Examples:

Let's make a custom type Phone with one field price.

struct Phone
     price::Int64
 end

Now, we can define a new method Serde.custom_name for the type Phone and its field price.

function Serde.custom_name(::Type{Phone}, ::Val{:price})
     return "cost"
 end

After that, if we try to deserialize a dictionary with an alias key "cost", it will match with the field price of type Phone.

julia> Serde.deser(Phone, Dict("cost" => 1000))
-Phone(1000)
source

Custom default values

We can also define default values for certain data types.

Serde.default_valueFunction
Serde.default_value(::Type{T}, ::Val{x}) -> nothing

This function is used to define default values for field x of type T. Supports user overriding for custom types. Initially, all values are set to nothing.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.isempty, Serde.nulltype.

Examples:

Let's make a custom type TimeZone with the field gmt.

struct TimeZone
+Phone(1000)
source

Custom default values

We can also define default values for certain data types.

Serde.default_valueFunction
Serde.default_value(::Type{T}, ::Val{x}) -> nothing

This function is used to define default values for field x of type T. Supports user overriding for custom types. Initially, all values are set to nothing.

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.isempty, Serde.nulltype.

Examples:

Let's make a custom type TimeZone with the field gmt.

struct TimeZone
     gmt::String
 end

Now, we can define a new method Serde.default_value for the type TimeZone and its field gmt.

function Serde.default_value(::Type{TimeZone}, ::Val{:gmt})
     return "UTC+3"
 end

After that, if we try to deserialize a dictionary without a key gmt, it will be filled with the default value "UTC+3".

julia> Serde.deser(TimeZone, Dict{String,Any}())
-TimeZone("UTC+3")
source

Null types handling

Finally, we can determine the 'nulltype' for custom types when they are empty or not specified at all.

Serde.nulltypeFunction
Serde.nulltype(::Type{T}) -> nothing

Defines behavior when the value for a field of type T is empty (according to Serde.isempty) or not specified. Supports user overriding for custom types. Initially, for all types, it is set to nothing (in case of type Missing, it returns the missing value).

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.isempty, Serde.default_value.

Examples

Let's make a custom type Computer with the following fields.

struct Computer
+TimeZone("UTC+3")
source

Null types handling

Finally, we can determine the 'nulltype' for custom types when they are empty or not specified at all.

Serde.nulltypeFunction
Serde.nulltype(::Type{T}) -> nothing

Defines behavior when the value for a field of type T is empty (according to Serde.isempty) or not specified. Supports user overriding for custom types. Initially, for all types, it is set to nothing (in case of type Missing, it returns the missing value).

Note

This function is not used explicitly and can only be overridden for the deserialization process.

See also Serde.isempty, Serde.default_value.

Examples

Let's make a custom type Computer with the following fields.

struct Computer
     cpu::String
     gpu::String
 end

For clarity, we also define the Serde.isempty method.

Serde.isempty(::Type{Computer}, x::String) = x == ""

Next, we define a new method Serde.nulltype for the custom type Computer. This method will be called for each type String that has been passed to a Serde.deser method.

Serde.nulltype(::Type{String}) = "N/A"

And, if we try to deserialize a dictionary with values of type String containing an empty string or not specified at all, it will set a "N/A" value for such fields in Computer.

julia> Serde.deser(Computer, Dict("cpu" => "i7-12900", "gpu" => ""))
 Computer("i7-12900", "N/A")
 
 julia> Serde.deser(Computer, Dict{String,Any}())
-Computer("N/A", "N/A")
source
+Computer("N/A", "N/A")source diff --git a/dev/pages/extended_ser/index.html b/dev/pages/extended_ser/index.html index ac9c52d..086bb56 100644 --- a/dev/pages/extended_ser/index.html +++ b/dev/pages/extended_ser/index.html @@ -17,4 +17,4 @@ width::Int64 length::Int64 end

Let's add the SerJson.ignore_field method for type Box.

SerJson.ignore_field(::Type{Box}, ::Val{:length}) = true

Because 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}
+{"height":2,"width":5} diff --git a/dev/pages/json/index.html b/dev/pages/json/index.html index f6c8e1a..31b2d70 100644 --- a/dev/pages/json/index.html +++ b/dev/pages/json/index.html @@ -15,7 +15,7 @@ Dict{String, Any} with 3 entries: "number" => 123 "vector" => Any[1, 2, 3] - "dictionary" => Dict{String, Any}("string"=>"123")source
Serde.ParJson.JsonSyntaxErrorType
JsonSyntaxError <: Exception

Exception thrown when a parse_json fails due to incorrect JSON syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeJson.deser_jsonFunction
deser_json(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from JSON formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_json.

Examples

julia> struct Record
+  "dictionary" => Dict{String, Any}("string"=>"123")
source
Serde.ParJson.JsonSyntaxErrorType
JsonSyntaxError <: Exception

Exception thrown when a parse_json fails due to incorrect JSON syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeJson.deser_jsonFunction
deser_json(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from JSON formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_json.

Examples

julia> struct Record
            count::Float64
        end
 
@@ -28,7 +28,7 @@
 julia> json = """ {"body":{"count":100.0},"name":"json","id":100} """;
 
 julia> deser_json(Data, json)
-Data(100, "json", Record(100.0))
source

Serialization

Serde.SerJson.to_jsonFunction
to_json([f::Function], data) -> String

Serializes any data into a flat JSON string. This method support serialization of nested data like dictionaries or custom types.

Specifying fields for serialization

If you want to serialize only specific fields of some custom type, you may define a special function f. This function f must lead next signature:

f(::Type{CustomType}) = (:field_1, :field_2, ...)

Now to_json(f, CustomType(...)) will serialize only specified fields CustomType.field_1, CustomType.field_2, etc. You can also define multiple methods of f for nested custom data types, e.g:

# Custom type 'Foo' containing fields of other custom types 'bar::Bar' and 'baz::Baz'
+Data(100, "json", Record(100.0))
source

Serialization

Serde.SerJson.to_jsonFunction
to_json([f::Function], data) -> String

Serializes any data into a flat JSON string. This method support serialization of nested data like dictionaries or custom types.

Specifying fields for serialization

If you want to serialize only specific fields of some custom type, you may define a special function f. This function f must lead next signature:

f(::Type{CustomType}) = (:field_1, :field_2, ...)

Now to_json(f, CustomType(...)) will serialize only specified fields CustomType.field_1, CustomType.field_2, etc. You can also define multiple methods of f for nested custom data types, e.g:

# Custom type 'Foo' containing fields of other custom types 'bar::Bar' and 'baz::Baz'
 custom_field_names(::Type{Foo}) = (:bar, :baz, ...)
 
 # Another custom types
@@ -61,7 +61,7 @@
 # Or you can use a lambda function
 
 julia> to_json(x -> (:field, :simple_field), ManyFields(1, 2.0, "a", [true, false])) |> print
-{"field":1,"simple_field":"a"}
source
Serde.SerJson.to_pretty_jsonFunction
to_pretty_json([f::Function], data) -> String

Do the same as to_json but return pretty JSON string.

julia> struct Pet
+{"field":1,"simple_field":"a"}
source
Serde.SerJson.to_pretty_jsonFunction
to_pretty_json([f::Function], data) -> String

Do the same as to_json but return pretty JSON string.

julia> struct Pet
            name::String
            age::Int64
        end
@@ -87,4 +87,4 @@
     "name":"Buddy",
     "age":5
   }
-}
source
+}source diff --git a/dev/pages/query/index.html b/dev/pages/query/index.html index 65c5097..ba23a32 100644 --- a/dev/pages/query/index.html +++ b/dev/pages/query/index.html @@ -15,7 +15,7 @@ julia> parse_query(query, backbone = Template) Dict{String, Union{String, Vector{String}}} with 2 entries: "vector" => ["1", "2", "3"] - "value" => "abc"source
Serde.ParQuery.QuerySyntaxErrorType
QuerySyntaxError <: Exception

Exception thrown when a parse_query fails due to incorrect query syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The caught exception.
source

Deserialization

Serde.DeQuery.deser_queryFunction
deser_query(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from Query formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_query.

Examples

julia> struct Person
+  "value"  => "abc"
source
Serde.ParQuery.QuerySyntaxErrorType
QuerySyntaxError <: Exception

Exception thrown when a parse_query fails due to incorrect query syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The caught exception.
source

Deserialization

Serde.DeQuery.deser_queryFunction
deser_query(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from Query formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_query.

Examples

julia> struct Person
            age::Int64
            name::String
            pets::Vector{String}
@@ -24,7 +24,7 @@
 julia> query = "age=20&name=Nancy&pets=[Cat,Dog]";
 
 julia> deser_query(Person, query)
-Person(20, "Nancy", ["Cat", "Dog"])
source

Serealization

Serde.SerQuery.to_queryFunction
to_query(data; kw...) -> String

Converts dictionary data (or custom type) to the query string. Values of data must be of primitive types or a vector of such. In case of custom data, the names of the query elements are obtained from the field names of data.

Keyword arguments

  • delimiter::AbstractString = "&": The separator character between query string elements.
  • sort_keys::Bool = false: A flag that determines whether the keys should be sorted by lexicographic order.
  • escape::Bool = true: Option to construct a valid URI-encoded string.

Examples

julia> struct Data
+Person(20, "Nancy", ["Cat", "Dog"])
source

Serealization

Serde.SerQuery.to_queryFunction
to_query(data; kw...) -> String

Converts dictionary data (or custom type) to the query string. Values of data must be of primitive types or a vector of such. In case of custom data, the names of the query elements are obtained from the field names of data.

Keyword arguments

  • delimiter::AbstractString = "&": The separator character between query string elements.
  • sort_keys::Bool = false: A flag that determines whether the keys should be sorted by lexicographic order.
  • escape::Bool = true: Option to construct a valid URI-encoded string.

Examples

julia> struct Data
            int::Int64
            float::Float64
            strings::Vector{String}
@@ -41,4 +41,4 @@
        );
 
 julia> to_query(data, escape = false)
-"int=1&strings=[a,b,c]&float=2.0"
source
+"int=1&strings=[a,b,c]&float=2.0"source diff --git a/dev/pages/toml/index.html b/dev/pages/toml/index.html index 00403ee..36889dc 100644 --- a/dev/pages/toml/index.html +++ b/dev/pages/toml/index.html @@ -13,7 +13,7 @@ julia> parse_toml(toml) Dict{String, Any} with 3 entries: "tag" => "line" - "points" => Any[Dict{String, Any}("y"=>0, "x"=>1), Dict{String, Any}("y"=>3, "x"=>2)]source
Serde.ParToml.TomlSyntaxErrorType
TomlSyntaxError <: Exception

Exception thrown when a parse_toml fails due to incorrect TOML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeToml.deser_tomlFunction
deser_toml(::Type{T}, x) -> T

Creates a new object of type T and fill it with values from TOML formated string x (or vector of UInt8).

See also parse_toml.

Examples

julia> struct Point
+  "points" => Any[Dict{String, Any}("y"=>0, "x"=>1), Dict{String, Any}("y"=>3, "x"=>2)]
source
Serde.ParToml.TomlSyntaxErrorType
TomlSyntaxError <: Exception

Exception thrown when a parse_toml fails due to incorrect TOML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeToml.deser_tomlFunction
deser_toml(::Type{T}, x) -> T

Creates a new object of type T and fill it with values from TOML formated string x (or vector of UInt8).

See also parse_toml.

Examples

julia> struct Point
            x::Int64
            y::Int64
        end
@@ -34,7 +34,7 @@
        """;
 
 julia> deser_toml(MyPlot, toml)
-MyPlot("line", Point[Point(1, 0), Point(2, 3)])
source

Serialization

Serde.SerToml.to_tomlFunction
to_toml(data) -> String

Passes a dictionary data (or custom data structure) for making TOML string.

Examples

Make TOML string from nested dictionaries.

julia> data = Dict(
+MyPlot("line", Point[Point(1, 0), Point(2, 3)])
source

Serialization

Serde.SerToml.to_tomlFunction
to_toml(data) -> String

Passes a dictionary data (or custom data structure) for making TOML string.

Examples

Make TOML string from nested dictionaries.

julia> data = Dict(
            "points" => [
                Dict("x" => "100", "y" => 200),
                Dict("x" => 300, "y" => 400),
@@ -77,4 +77,4 @@
 
 [[points]]
 x = 2
-y = 3
source
+y = 3source diff --git a/dev/pages/utils/index.html b/dev/pages/utils/index.html index 2b34937..666301f 100644 --- a/dev/pages/utils/index.html +++ b/dev/pages/utils/index.html @@ -29,7 +29,7 @@ Dict{String, Any} with 3 entries: :bar_num => 1.0 :val => 1 - :str => "a"source
Serde.@serdeMacro
@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 (see Serde.default_value).
  • @de_name: Used to defines an alias names for fields of declared type (see Serde.custom_name).
  • @ser_json_name: Used to define custom output name for fields of declared type (see Serde.ser_name).

Next, the syntax template looks like this:

@serde @decor_1 @decor_2 ... struct
+  :str   => "a"
source
Serde.@serdeMacro
@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 (see Serde.default_value).
  • @de_name: Used to defines an alias names for fields of declared type (see Serde.custom_name).
  • @ser_json_name: Used to define custom output name for fields of declared type (see Serde.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_json_name mutable struct Foo
@@ -38,4 +38,4 @@
 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_json_name column will be used as output names for serialization.

julia> to_json(Foo(40, 50)) |> print
-{"bar":40,"second":50}
source
+{"bar":40,"second":50}source diff --git a/dev/pages/xml/index.html b/dev/pages/xml/index.html index b3dcf47..bf2a485 100644 --- a/dev/pages/xml/index.html +++ b/dev/pages/xml/index.html @@ -18,7 +18,7 @@ "year" => Dict{String, Any}("_"=>"2024") "id" => "bk101" "title" => Dict{String, Any}("_"=>"Advanced Julia Programming") - "authors" => Dict{String, Any}("author"=>Dict{String, Any}[Dict("lang"=>"en", "_"=>"John Doe"), Dict("lang"=>"es", "_"=>"Juan Pérez")])source
Serde.ParXml.XmlSyntaxErrorType
XmlSyntaxError <: Exception

Exception thrown when a parse_xml fails due to incorrect XML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The exception that was caught.
source

Deserialization

Serde.DeXml.deser_xmlFunction
deser_xml(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from XML formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_xml.

Examples

julia> struct Record
+  "authors" => Dict{String, Any}("author"=>Dict{String, Any}[Dict("lang"=>"en", "_"=>"John Doe"), Dict("lang"=>"es", "_"=>"Juan Pérez")])
source
Serde.ParXml.XmlSyntaxErrorType
XmlSyntaxError <: Exception

Exception thrown when a parse_xml fails due to incorrect XML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The exception that was caught.
source

Deserialization

Serde.DeXml.deser_xmlFunction
deser_xml(::Type{T}, x; kw...) -> T

Creates a new object of type T and fill it with values from XML formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_xml.

Examples

julia> struct Record
            count::Float64
        end
 
@@ -39,7 +39,7 @@
        """;
 
 julia> deser_xml(Data, xml)
-Data(100, "xml", Record(100.0))
source

Serialization

Serde.SerXml.to_xmlFunction
to_xml(val; key::String = "xml") -> String

Serializes any nested data val into an XML string that follows the next rules:

  • Values of primitive types are used as an element of the current tag.
  • Vector elements will be used as sub-tag elements.
  • Dictionaries are processed using the following rules:
    • Key names must be a string or a symbol types.
    • A key with a non-empty string value will be interpreted as a new sub-tag.
    • A key with an empty string value will be interpreted as an element of the current tag.
  • Custom types are handled as follows:
    • The field name containing the primitive type will be used as an attribute for the current tag.
    • A field name containing a composite type (dictionary or other custom type) will be used as the name for the next sub-tag.
    • A primitive type field with a special name "_" will be used as an element for the current tag.

Thus, this method can serialize all basic data types and can work with any nesting level of a combination of dictionaries and custom data types. The key keyword specifies the name of the root tag.

Examples

julia> struct Image
+Data(100, "xml", Record(100.0))
source

Serialization

Serde.SerXml.to_xmlFunction
to_xml(val; key::String = "xml") -> String

Serializes any nested data val into an XML string that follows the next rules:

  • Values of primitive types are used as an element of the current tag.
  • Vector elements will be used as sub-tag elements.
  • Dictionaries are processed using the following rules:
    • Key names must be a string or a symbol types.
    • A key with a non-empty string value will be interpreted as a new sub-tag.
    • A key with an empty string value will be interpreted as an element of the current tag.
  • Custom types are handled as follows:
    • The field name containing the primitive type will be used as an attribute for the current tag.
    • A field name containing a composite type (dictionary or other custom type) will be used as the name for the next sub-tag.
    • A primitive type field with a special name "_" will be used as an element for the current tag.

Thus, this method can serialize all basic data types and can work with any nesting level of a combination of dictionaries and custom data types. The key keyword specifies the name of the root tag.

Examples

julia> struct Image
            dpi::Int64
            _::String
        end
@@ -55,4 +55,4 @@
 <xml>
   <image dpi="200">profile.png</image>
   <info status="OK" id="451">employee</info>
-</xml>
source
+</xml>source diff --git a/dev/pages/yaml/index.html b/dev/pages/yaml/index.html index f6dddbb..1219b6c 100644 --- a/dev/pages/yaml/index.html +++ b/dev/pages/yaml/index.html @@ -30,7 +30,7 @@ "list" => Dict{String, Any}[Dict("int"=>63, "string"=>"foo", "quoted"=>"bar", "float"=>1.63), Dict("string"=>"baz", "braces"=>"{{ dd }}")] "anchorTest" => Dict{String, Any}("toMultiline"=>"this text will be considered \non multiple lines\n", "toSingleLine"=>"this text will be considered on a single line\n") "aliasTest" => Dict{String, Any}("toMultiline"=>"this text will be considered \non multiple lines\n", "toSingleLine"=>"this text will be considered on a single line\n") - "date" => Date("2024-01-01")source
Serde.ParYaml.YamlSyntaxErrorType
YamlSyntaxError <: Exception

Exception thrown when a parse_yaml fails due to incorrect YAML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeYaml.deser_yamlFunction
deser_yaml(::Type{T}, x) -> T

Creates a new object of type T and fill it with values from YAML formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_yaml.

Examples

julia> struct Status
+  "date"       => Date("2024-01-01")
source
Serde.ParYaml.YamlSyntaxErrorType
YamlSyntaxError <: Exception

Exception thrown when a parse_yaml fails due to incorrect YAML syntax or any underlying error that occurs during parsing.

Fields

  • message::String: The error message.
  • exception::Exception: The catched exception.
source

Deserialization

Serde.DeYaml.deser_yamlFunction
deser_yaml(::Type{T}, x) -> T

Creates a new object of type T and fill it with values from YAML formated string x (or vector of UInt8).

Keyword arguments kw is the same as in parse_yaml.

Examples

julia> struct Status
            id::Int64
            value::Float64
        end
@@ -59,7 +59,7 @@
        """;
 
 julia> deser_yaml(Server, yaml)
-Server("cloud_server", Status(42, 12.34), [1, 2, 3], true, Dict("Kevin" => 1, "George" => 2))
source

Serialization

Serde.SerYaml.to_yamlFunction
to_yaml([f::Function], data) -> String

Serializes any data into a YAML multiline string. This method support serialization of nested data like dictionaries or custom types.

Specifying fields for serialization

If you want to serialize only specific fields of some custom type, you may define a special function f. This function f must lead next signature:

f(::Type{CustomType}) = (:field_1, :field_2, ...)

Now to_yaml(f, CustomType(...)) will serialize only specified fields CustomType.field_1, CustomType.field_2, etc. You can also define multiple methods of f for nested custom data types, e.g:

# Custom type 'Foo' containing fields of other custom types 'bar::Bar' and 'baz::Baz'
+Server("cloud_server", Status(42, 12.34), [1, 2, 3], true, Dict("Kevin" => 1, "George" => 2))
source

Serialization

Serde.SerYaml.to_yamlFunction
to_yaml([f::Function], data) -> String

Serializes any data into a YAML multiline string. This method support serialization of nested data like dictionaries or custom types.

Specifying fields for serialization

If you want to serialize only specific fields of some custom type, you may define a special function f. This function f must lead next signature:

f(::Type{CustomType}) = (:field_1, :field_2, ...)

Now to_yaml(f, CustomType(...)) will serialize only specified fields CustomType.field_1, CustomType.field_2, etc. You can also define multiple methods of f for nested custom data types, e.g:

# Custom type 'Foo' containing fields of other custom types 'bar::Bar' and 'baz::Baz'
 custom_field_names(::Type{Foo}) = (:bar, :baz, ...)
 
 # Another custom types
@@ -103,4 +103,4 @@
 
 julia> to_yaml(x -> (:field, :simple_field), ManyFields(1, 2.0, "a", [true, false])) |> print
 field: 1
-simple_field: "a"
source
+simple_field: "a"source