Skip to content
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

Fix regression where NSE is thrown when serializing string dictionary keys with custom converter #56445

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,15 @@ internal virtual T ReadWithQuotes(ref Utf8JsonReader reader)
}

internal virtual void WriteWithQuotes(Utf8JsonWriter writer, [DisallowNull] T value, JsonSerializerOptions options, ref WriteStack state)
=> ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
{
if (typeof(T) == typeof(string))
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
JsonMetadataServices.StringConverter.WriteWithQuotes(writer, (string)(object)value, options, ref state);
return;
}

ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(TypeToConvert, this);
}

internal sealed override void WriteWithQuotesAsObject(Utf8JsonWriter writer, object value, JsonSerializerOptions options, ref WriteStack state)
=> WriteWithQuotes(writer, (T)value, options, ref state);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -181,5 +182,35 @@ public static void GetConverterTypeToConvertNull()
{
Assert.Throws<ArgumentNullException>(() => (new JsonSerializerOptions()).GetConverter(typeToConvert: null!));
}

[Fact]
public static void CustomStringConverter_UsedInDictionaries()
{
var options = new JsonSerializerOptions
{
Converters = { new CustomStringConverter() },
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
layomia marked this conversation as resolved.
Show resolved Hide resolved
};

var value = new Dictionary<string, string>()
{
["Key"] = "value"
};

string serialized = JsonSerializer.Serialize(value, options);
Assert.Equal(@"{""key"":""value""}", serialized);

value = JsonSerializer.Deserialize<Dictionary<string, string>>(serialized, options);
Assert.Equal("value", value["key"]);
}

internal sealed class CustomStringConverter : JsonConverter<string>
{
public override string? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> reader.GetString();

public override void Write(Utf8JsonWriter writer, string value, JsonSerializerOptions options)
=> writer.WriteStringValue(value);
}
}
}