Using multiple custom serializers for complex type
See original GitHub issueFirst 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:
- Created 6 years ago
- Reactions:2
- Comments:6 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
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 theICustomXmlSerializers
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-L186Again this works for both type-registration and member-registration. The
IExtendedXmlCustomSerializer
is for xml-based serialization (v1), but we also have theISerializer
which is sort of the root serialization object used in v2. This can be used with theRegister
extension method as described here: https://github.com/wojtpl2/ExtendedXmlSerializer/blob/master/test/ExtendedXmlSerializer.Tests/ReportedIssues/Issue154Tests.cs#L66-L81Let me know if you have any questions around any of this. 😃