42

I'm starting to migrate some code I have from Newtonsoft.Json to System.Text.Json in a .net Core 3.0 app.

I migrated the properties from

[JsonProperty("id")] to [JsonPropertyName("id")]

but I have some properties decorated with the JsonConverter attribute as:

[JsonConverter(typeof(DateTimeConverter))] [JsonPropertyName("birth_date")] DateTime BirthDate{ get; set; }

But I cannot find the equivalent of this Newtonsoft converter in System.Text.Json Does someone know how can this be achieved in .net Core 3.0?

Thanks!

dbc
  • 104,963
  • 20
  • 228
  • 340
Fritjof Berggren
  • 3,178
  • 5
  • 35
  • 57
  • Wouldn't it be easier to use Newtonsoft instead rather than migrating it all over? – DavidG May 29 '19 at 12:20
  • 7
    That's another question, but I'd like to know if this can be achieved in `System.Text.Json` – Fritjof Berggren May 29 '19 at 12:21
  • 2
    Not sure the new JSON objects are mature enough to cope with all circumstances right now, but happy to be proven wrong. Making .NET Core 3 use Newtonsoft though, is a one line change I believe. – DavidG May 29 '19 at 12:23

2 Answers2

44

System.Text.Json now supports custom type converters in .NET 3.0 preview-7 and above.

You can add converters that match on type, and use the JsonConverter attribute to use a specific converter for a property.

Here's an example to convert between long and string (because javascript doesn't support 64-bit integers).

public class LongToStringConverter : JsonConverter<long>
{
    public override long Read(ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
    {
        if (reader.TokenType == JsonTokenType.String)
        {
            // try to parse number directly from bytes
            ReadOnlySpan<byte> span = reader.HasValueSequence ? reader.ValueSequence.ToArray() : reader.ValueSpan;
            if (Utf8Parser.TryParse(span, out long number, out int bytesConsumed) && span.Length == bytesConsumed)
                return number;

            // try to parse from a string if the above failed, this covers cases with other escaped/UTF characters
            if (Int64.TryParse(reader.GetString(), out number))
                return number;
        }

        // fallback to default handling
        return reader.GetInt64();
    }

    public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString());
    }
}

Register the converter by adding it to the Converters list in JsonSerializerOptions

services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new LongToStringConverter());
});

Note: The current release doesn't support nullable types yet.

Mani Gandham
  • 7,688
  • 1
  • 51
  • 60
  • 16
    Just for reference: If someone is searching for `StringEnumConverter` in System.Text.Json: `using System.Text.Json.Serialization; [JsonConverter(typeof(JsonStringEnumConverter))]`. Check https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonstringenumconverter?view=netcore-3.0. – FranzHuber23 Oct 08 '19 at 13:26
  • 1
    How do I convert to a custom type? `{x: {foo:1,bar:2,baz:{apple:1,orange:2, juice:[1,2,3,4,5]}}` – boop Feb 24 '20 at 19:11
  • @boop You create your type then write a converter that inherits from `JsonConverter` with the read and write logic you need. The documentation has lots of samples that show you how to interact with the API: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to – Mani Gandham Feb 25 '20 at 00:01
  • @FranzHuber23 It does serialize, but it does not respect the `EnumMember` attribute value and thus the Enum name is serialized instead [System.Text.Json: JsonStringEnumConverter ignores its JsonNamingPolicy during deserialization. #31619](https://github.com/dotnet/runtime/issues/31619) – OL. Feb 07 '22 at 13:07
4

You can find the JsonConverterAttribute in the namespace System.Text.Json.Serialization.

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonconverterattribute?view=netcore-3.0

Lars
  • 6,421
  • 1
  • 23
  • 24