Skip to content

Serialization support #4

@LWEdslev

Description

@LWEdslev

Currently serialization using System.Xml.Serialization does not work as I expected.
The following works:

using System.Xml.Serialization;
using newtype;

var xml = new XmlSerializer(typeof(Order));
var order = new Order { Contract = "CTR-002" };
using var sw = new StringWriter();
xml.Serialize(sw, order);
Console.WriteLine($"XML: {sw}");

public class Order { public string Contract { get; set; } }

but using newtype it did not:

using System.Xml.Serialization;
using newtype;

var xml = new XmlSerializer(typeof(Order));
var order = new Order { Contract = new ContractId("CTR-002") };
using var sw = new StringWriter();
xml.Serialize(sw, order);
Console.WriteLine($"XML: {sw}");
// <Id /> — empty, value not serialized

public class Order { public ContractId Contract { get; set; } }

[newtype<string>]
public readonly partial struct ContractId;

I suspect similar problems exist for other serializers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions