How to use converters for nullable types
See original GitHub issueI have a backward compatibility requirement to serialize all enums and booleans as integer values. So I need my xml to contain
<STATUS>1</STATUS>
<COMPLETED>1</COMPLETED>
instead of
<STATUS>Ok</STATUS>
<COMPLETED>true</COMPLETED>
Specifically I use nullable enum an boolean types in my entities.
I got it to work by registering IConverters for <int?> and <bool?>
private static IExtendedXmlSerializer CreateSerializer(string root) => new ConfigurationContainer()
.EnableImplicitTyping(implicitTypes)
.Type<T>().Name(root)
.Type<int?>().Register().Converter().Using(NullableIntConverter.Default)
.Type<bool?>().Register().Converter().Using(NullableBoolConverter.Default)
.EnableThreadProtection()
.Create();
private class NullableIntConverter : IConverter<int?>
{
public static readonly NullableIntConverter Default = new NullableIntConverter();
public bool IsSatisfiedBy(TypeInfo parameter)
=> parameter.AsType() == typeof(int?);
public int? Parse(string data)
=> string.IsNullOrEmpty(data) ? (int?)null : int.Parse(data);
public string Format(int? instance)
=> instance?.ToString();
}
private class NullableBoolConverter : IConverter<bool?>
{
public static readonly NullableBoolConverter Default = new();
public bool IsSatisfiedBy(TypeInfo parameter)
=> parameter.AsType() == typeof(bool?);
public bool? Parse(string data) => data switch
{
"0" => false,
"1" => true,
_ => null
};
This is working for me with version 3.4.2 but stopped working in version 3.4.3 My guess that with the introduction of nullable types support, the converters are not overriding the default serialization.
Is there a workaround? I thought about using ISerializerExtension or IAlteration but couldn’t figure how to use those APIs.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
NullableConverter Class (System.ComponentModel)
Provides automatic conversion between a nullable type and its underlying primitive ... Converts the given object to the type of this converter, using...
Read more >c# - TypeConverters and null
My type convertor looks like is below. The problem is that if the settings in the settings file is blank the settings class...
Read more >Allow HasConversion/ValueConverters to convert nulls
However, to be compatible with nullable types, this HasConversion does not support the practice of empty types, and I cannot complete the work....
Read more >Referencing complex data using Room
This document explains how to use type converters and why Room ... You identify type converters by using the @TypeConverter annotation.
Read more >What is TypeConverter in Room? How to use it properly
Look at the first item in the POJO. var img: Bitmap? = null. It's a custom object and will not fit into any...
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
Thanks very much for the very quick response!!! I tested the fix with my code and it works!
This has been deployed to NuGet: https://www.nuget.org/packages/ExtendedXmlSerializer/
Thank you for taking the time to report this and improving ExtendedXmlSerializer. 👍