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.

How to use converters for nullable types

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zvikaracommented, Aug 29, 2021

Thanks very much for the very quick response!!! I tested the fix with my code and it works!

0reactions
Mike-E-angelocommented, Aug 31, 2021

This has been deployed to NuGet: https://www.nuget.org/packages/ExtendedXmlSerializer/

Thank you for taking the time to report this and improving ExtendedXmlSerializer. 👍

<div>ExtendedXmlSerializer 3.7.3</div><div>An extensible Xml Serializer for .NET that builds on the functionality of the classic XmlSerializer with a powerful and robust extension model.</div>
Read more comments on GitHub >

github_iconTop 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 >

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