Description
For my use case I have the following third party XSD (simplified) for which I generate an enum:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="YesOrNoOrEmpty">
<xs:restriction base="xs:string">
<xs:enumeration value="y" />
<xs:enumeration value="n" />
<xs:enumeration value="" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
I want the generated enum to be serializable in both XML and JSON, so I annotate the members with both XmlEnumAttribute as well as the new JsonStringEnumMemberNameAttribute from System.Text.Json v9-preview.
For XML this works fine, but with JSON I get an exception, because empty strings are not allowed. I assume this might have something to do with support for Flags enums? But for enums without FlagsAttribute this restriction should not apply.
Reproduction Steps
using System.Text.Json.Serialization;
using System.Xml.Serialization;
public enum YesOrNoOrEmpty
{
[JsonStringEnumMemberName("y")]
[XmlEnum("y")]
Yes,
[JsonStringEnumMemberName("n")]
[XmlEnum("n")]
No,
[JsonStringEnumMemberName("")]
[XmlEnum("")]
Empty,
}
Expected behavior
JsonStringEnumConverter is able to serialize and deserialize enums where the string representation of one of the enum members is an empty string. This matches behaviour of System.Xml's XmlEnumAttribute, and EnumMemberAttribute which is used by Newtonsoft.Json.
Actual behavior
JsonStringEnumConverter (using EnumConverter internally) does a string.IsNullOrEmpty check and throws an InvalidOperationException: Enum type '{0}' uses unsupported identifier '{1}'. It must not be null, empty, or containing leading or trailing whitespace. Flags enums must additionally not contain commas.
Regression?
No response
Known Workarounds
Implementing a custom JsonConverter for enums
Configuration
No response
Other information
No response
Description
For my use case I have the following third party XSD (simplified) for which I generate an enum:
I want the generated enum to be serializable in both XML and JSON, so I annotate the members with both XmlEnumAttribute as well as the new JsonStringEnumMemberNameAttribute from System.Text.Json v9-preview.
For XML this works fine, but with JSON I get an exception, because empty strings are not allowed. I assume this might have something to do with support for Flags enums? But for enums without FlagsAttribute this restriction should not apply.
Reproduction Steps
Expected behavior
JsonStringEnumConverteris able to serialize and deserialize enums where the string representation of one of the enum members is an empty string. This matches behaviour of System.Xml's XmlEnumAttribute, and EnumMemberAttribute which is used by Newtonsoft.Json.Actual behavior
JsonStringEnumConverter(usingEnumConverterinternally) does astring.IsNullOrEmptycheck and throws an InvalidOperationException:Enum type '{0}' uses unsupported identifier '{1}'. It must not be null, empty, or containing leading or trailing whitespace. Flags enums must additionally not contain commas.Regression?
No response
Known Workarounds
Implementing a custom JsonConverter for enums
Configuration
No response
Other information
No response