-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error message for dictionary key converters not implementing ReadAsPropertyName
/WriteAsPropertyName
.
#93406
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionWhen I serialize an object that contains a property of type JsonSerializer.Serialize(
new Custom
{
Dict = new Dictionary<CustomKey, string>
{
[new CustomKey {Value1 = "one", Value2 = "two"}] = "three",
[new CustomKey {Value1 = "four", Value2 = "five"}] = "six"
}}); class Custom
{
public Dictionary<CustomKey, string> Dict { get; set; }
}
[JsonConverter(typeof(CustomKeyJsonConverter))]
class CustomKey
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public static CustomKey Parse(string text)
{
var values = text.Split("-");
return new CustomKey {Value1 = values[0], Value2 = values[1]};
}
public override string ToString()
{
return $"{this.Value1}-{this.Value2}";
}
}
class CustomKeyJsonConverter : JsonConverter<CustomKey>
{
public override CustomKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return CustomKey.Parse(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, CustomKey value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
} Reproduction Stepssee above Expected behaviorThe serialization (and deserialization) should succeed if there is a JSON converter for the key. Actual behaviorThe following
|
Using converters for dictionary key serialization requires overriding the class CustomKeyJsonConverter : JsonConverter<CustomKey>
{
public override CustomKey? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> CustomKey.Parse(reader.GetString());
public override void Write(Utf8JsonWriter writer, CustomKey value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
public override CustomKey ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> CustomKey.Parse(reader.GetString());
public override void WriteAsPropertyName(Utf8JsonWriter writer, CustomKey value, JsonSerializerOptions options)
=> writer.WritePropertyName(value.ToString());
} Keeping the issue open since we could improve the error message to include this information. |
ReadAsPropertyName
/WriteAsPropertyName
.
I am interested in helping to contribute in this issue |
Thanks @naeemaei, I've assigned it to you. |
Considering that this error occurs when the methods are not implemented in the converter, is it better to create a method with [DoesNotReturn]
public static void ThrowNotImplementedException_DictionaryKeyTypeConvertorNotImplemented(Type keyType, JsonConverter converter)
{
throw new NotImplementedException(SR.Format(SR.DictionaryKeyTypeConvertorNotImplemented, keyType, converter.GetType()));
} |
Changing the exception type can break users that depend on catching the current exception. I think it would be preferable to just update the error message. |
Just for completeness and future readers: Changing the converter as @eiriktsarpalis suggested in #93406 (comment) does fix the problem, the exceptions goes away and the type is correctly (de)serialized (testet with .NET 6). |
* Improve DictionaryKeyTypeNotSupported message Fix #93406 * Improve error message Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com> --------- Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
Description
When I serialize an object that contains a property of type
Dictionary<TKey, TValue>
andTKey
is a custom class, I get aNotSupportedException
, even if there is a JSON converter for this.Reproduction Steps
see above
Expected behavior
The serialization (and deserialization) should succeed if there is a JSON converter for the key.
Actual behavior
The following
NotSupportedException
exception is thrownRegression?
The same behavior is in .NET 6, 7 and 8, so it probably not a regression.
Known Workarounds
Write a custom JSON for the whole
Dictionary<CustomKey, string>
type.Configuration
Windows 11, ,NET SDK 8.0.100-rc.2.23502.2
Other information
No response
The text was updated successfully, but these errors were encountered: