Codecs and Serialization
The Codec module registers manual encoders and decoders as one
XmlConverter inside XmlSerializerOptions. Automatic serialization then
uses the manual codec only for the registered type.
A Manual Union Codec
type Shape =
| Circle of float
| Square of float
static member Encoder: Encoder<Shape> =
function
| Circle r -> Encode.element "Circle" [ Encode.float r ]
| Square s -> Encode.element "Square" [ Encode.float s ]
static member Decoder: Decoder<Shape> =
Decode.oneOf [
fun el -> decode {
let! r = Required.Element.get ("Circle", Required.float) el
return Circle r
}
fun el -> decode {
let! s = Required.Element.get ("Square", Required.float) el
return Square s
}
]
let options =
XmlSerializerOptions() |> Codec.useCodec(Shape.Encoder, Shape.Decoder)
let xml = XmlSerializer.Serialize(Circle 1.5, options)
printfn $"%s{xml}"
let shapeBack = XmlSerializer.Deserialize<Shape>(xml, options)
printfn $"%A{shapeBack}"
Use Codec.useEncoder or Codec.useDecoder when only one direction needs help.
Use Codec.useCodecWithOptions when a codec must read the active options.
Manual and Automatic Together
Decoding.auto delegates a whole payload to XmlSerializer.
Decoding.fromString runs one manual decoder on the root element.
Both return Result<'T, DecodeError>, so call sites handle both the same way.
[<CLIMutable>]
type Person = { Name: string; Age: int }
let autoResult =
Decoding.auto<Person>("<Person><Name>Ada</Name><Age>36</Age></Person>")
printfn $"Auto: %A{autoResult}"
Attributes Are a Manual Choice
The automatic serializer maps every property to a child element. A field
becomes an attribute only when the manual codec says so: the encoder writes it
with Encode.attribute, and the decoder reads it with Required.Attribute.get
or Optional.Attribute.get.
let taggedCircle =
Encode.element "Circle" [ Encode.float 1.5 ]
|> Encode.attribute("unit", "m")
let taggedUnit =
Decoding.fromString(taggedCircle.ToString(), Required.Attribute.get("unit", Required.string))
printfn $"Unit: %A{taggedUnit}"
val float: value: 'T -> float (requires member op_Explicit)
--------------------
type float = System.Double
--------------------
type float<'Measure> = float
<summary> Provides functions for encoding values to XML nodes. All primitives format invariantly. Text values carry no type marker: XML has no native boolean, date, or null literal, so decoders parse the same lexical forms back. </summary>
<summary> Builds an element node with the given local name and child nodes. </summary>
module Decode from JDeck.Xml.Decode
--------------------
module Decode from JDeck.Xml
<summary> Tries each decoder in order and returns the first success, otherwise the first error. </summary>
<summary> Decoders that fail when the value is missing, nil, or has the wrong shape. </summary>
<summary> Decodes a required child element by local name. Attributes hold scalars only; there are no sequence attributes. </summary>
static member Required.Element.get: name: string * decoder: Decoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError>)
static member Required.Element.get: namespaceUri: string * name: string * decoder: CollectErrorsDecoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError list>)
static member Required.Element.get: namespaceUri: string * name: string * decoder: Decoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError>)
type XmlSerializerOptions = new: unit -> unit + 2 overloads member GetConverter: typeToConvert: Type -> XmlConverter member GetTypeInfo: ``type`` : Type -> XmlTypeInfo + 1 overload member MakeReadOnly: unit -> unit + 1 overload member TryGetTypeInfo: ``type`` : Type * typeInfo: byref<XmlTypeInfo> -> bool + 1 overload member AllowDuplicateProperties: bool member AttributeNamingPolicy: XmlNamingPolicy member AttributeStyle: XmlAttributeStyle member AttributesPerLine: int member CharHandling: XmlCharHandling ...
<summary> Provides options to be used with <see cref="T:CloudNimble.Text.Xml.XmlSerializer" />. This class cannot be inherited. </summary>
<remarks> This class is the XML counterpart of <c>System.Text.Json.JsonSerializerOptions</c>. An instance becomes read-only the first time it is used to serialize or deserialize, or when <see cref="M:CloudNimble.Text.Xml.XmlSerializerOptions.MakeReadOnly" /> is called. Create one instance and reuse it: the converters and type info it resolves are cached on the instance. </remarks>
--------------------
XmlSerializerOptions() : XmlSerializerOptions
XmlSerializerOptions(defaults: XmlSerializerDefaults) : XmlSerializerOptions
XmlSerializerOptions(options: XmlSerializerOptions) : XmlSerializerOptions
<summary> Provides functionality to serialize objects or value types to XML and to deserialize XML into objects or value types. This class cannot be inherited. </summary>
<remarks> This class is the XML counterpart of <c>System.Text.Json.JsonSerializer</c>. Every overload resolves an <see cref="T:CloudNimble.Text.Xml.Serialization.XmlTypeInfo" /> and then goes through one of the internal <c>Read</c> or <c>Write</c> methods in this file. All reading is done by <see cref="T:CloudNimble.Text.Xml.Utf8XmlReader" /> and all writing by <see cref="T:CloudNimble.Text.Xml.Utf8XmlWriter" />. </remarks>
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(value: 'TValue, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : string
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(value: 'TValue, ?options: XmlSerializerOptions) : string
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(bufferWriter: System.Buffers.IBufferWriter<byte>, value: 'TValue, context: Serialization.XmlSerializerContext) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(bufferWriter: System.Buffers.IBufferWriter<byte>, value: 'TValue, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(bufferWriter: System.Buffers.IBufferWriter<byte>, value: 'TValue, ?options: XmlSerializerOptions) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(utf8Xml: System.IO.Stream, value: 'TValue, context: Serialization.XmlSerializerContext) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(utf8Xml: System.IO.Stream, value: 'TValue, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(utf8Xml: System.IO.Stream, value: 'TValue, ?options: XmlSerializerOptions) : unit
(+0 other overloads)
XmlSerializer.Serialize<'TValue>(writer: Utf8XmlWriter, value: 'TValue, context: Serialization.XmlSerializerContext) : unit
(+0 other overloads)
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(node: Nodes.XmlNode, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(node: Nodes.XmlNode, ?options: XmlSerializerOptions) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(element: XmlElement, context: Serialization.XmlSerializerContext) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(element: XmlElement, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(element: XmlElement, ?options: XmlSerializerOptions) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(document: XmlDocument, context: Serialization.XmlSerializerContext) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(document: XmlDocument, xmlTypeInfo: Serialization.XmlTypeInfo<'TValue>) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(document: XmlDocument, ?options: XmlSerializerOptions) : 'TValue
(+0 other overloads)
XmlSerializer.Deserialize<'TValue>(xml: System.IO.Stream, context: Serialization.XmlSerializerContext) : 'TValue
(+0 other overloads)
type CLIMutableAttribute = inherit Attribute new: unit -> CLIMutableAttribute
--------------------
new: unit -> CLIMutableAttribute
val string: value: 'T -> string
--------------------
type string = System.String
val int: value: 'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
<summary> Entry points that parse XML text and run a decoder on the document element. </summary>
static member Decoding.auto: value: byte array -> Result<'TResult,DecodeError>
static member Decoding.auto: value: string -> Result<'TResult,DecodeError>
<summary> Sets an attribute on an element node and returns the node. </summary>
static member Required.Attribute.get: name: string * decoder: Decoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError>)
static member Required.Attribute.get: namespaceUri: string * name: string * decoder: CollectErrorsDecoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError list>)
static member Required.Attribute.get: namespaceUri: string * name: string * decoder: Decoder<'TResult> -> (XmlElement -> Result<'TResult,DecodeError>)
JDeck