question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using multiple custom serializers for complex type

See original GitHub issue

First of all, thank you for your good work and I found this library useful. I ran into a situation that needs to implement multiple custom serializers.

public class ParentType {
   public ChildType Child { get; set; }
}

public class ParentTypeSerializer : IExtendedXmlCustomSerializer<ParentType> {
    public void Serializer(XmlWriter writer, ParentType obj) {
          // How to tell this serializer that calls ChildTypeSerializer?
          // writer.WriteValue(obj.Child);
    }
}

public class ChildTypeSerializer : IExtendedXmlCustomSerializer<ChildType> {
    public void Serializer(XmlWriter writer, ChildType obj) {
          // Writer does something here
    }
}

// Registration
var serializer = new ConfigurationContainer()
    .Type<ParentType>()
    .CustomSerializer(new ParentTypeSerializer())
    .Type<ChildType>()
    .CustomSerializer(new ChildTypeSerializer())
    .Create();

If I got a customer serializer for both ParentType and ChildType, how do parent serializer ask ChildTypeSerializer to handle obj.Child property based on the type?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Mike-E-angelocommented, Feb 1, 2018

Closing this issue as it has been deployed in 2.1.0: #162

Please feel free to open an issue if you encounter any further problems

1reaction
Mike-E-angelocommented, Jan 27, 2018

Alright, I’ve just committed an improvement that addresses this and a couple of other issues around registering serializers around types. The best example of this is here: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/master/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue154Tests.cs#L50-L64

You can now reference a type as the serializer, and that type can hold registered dependencies (such as the global ISerializers class or the ICustomXmlSerializers that can get other serializers based on types. Here is an example: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/master/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue154Tests.cs#L174-L186

Again this works for both type-registration and member-registration. The IExtendedXmlCustomSerializer is for xml-based serialization (v1), but we also have the ISerializer which is sort of the root serialization object used in v2. This can be used with the Register extension method as described here: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/master/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue154Tests.cs#L66-L81

Let me know if you have any questions around any of this. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

[DRF]What are the benefits of using nested Serializers over ...
In general, I use nested serializers when I want an interface where model B is always read (or created) together with model A....
Read more >
Serializer relations
If you require more complex hyperlinked representation you'll need to customize the field, as described in the custom hyperlinked fields section, below.
Read more >
Custom serialization and deserialization contracts
Learn how to write your own contract resolution logic to customize the JSON contract for a type.
Read more >
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
Read more >
Gson Advanced — Custom Serialization for Simplification ...
In this blog post, we'll explore how we can customize the Gson serialization of Java objects. There are many reasons why you might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found