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

Use System.Text.Json instead of Newtonsoft #9

Closed
darbio opened this issue Dec 31, 2019 · 4 comments
Closed

Use System.Text.Json instead of Newtonsoft #9

darbio opened this issue Dec 31, 2019 · 4 comments
Assignees

Comments

@darbio
Copy link

darbio commented Dec 31, 2019

ASP.Net Core 3.1 uses System.Text.Json to serialize objects. It would be good to support this new method of serialization.

I currently use the following (obviously it would need adapting to support the relevant use cases in this package).

public class MemberIdConverter : JsonConverter<MemberId>
{
    public override MemberId Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options) =>
            new MemberId(Guid.Parse(reader.GetString()));

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

This issue depends upon #5

@andrewlock
Copy link
Owner

Agreed, seems like a good addition!

@vebbo2
Copy link
Contributor

vebbo2 commented Apr 9, 2020

Hi @andrewlock, I am up for implementing this, but I need your opinion. I think it's good idea to enable to pick which serializer you want to use, and have support for both System.Text.Json and Newtonsoft.Json.

This requires changes in StronglyTypedIdAttribute parameters. My proposition is to add new enum parameter jsonProvider (which would default to StronglyTypedIdJsonProvider.NewtonsoftJson):

// no provider needed
[StronglyTypedId(generateJsonConverter: false, backingType: StronglyTypedIdBackingType.Int)]

// old behavior, generates converter for Newtonsoft.Json
[StronglyTypedId(backingType: StronglyTypedIdBackingType.Int)]

// one specific
[StronglyTypedId(jsonConverter: StronglyTypedIdJsonProvider.NewtonsoftJson, backingType: StronglyTypedIdBackingType.Int)]
[StronglyTypedId(jsonConverter: StronglyTypedIdJsonProvider.SystemTextJson, backingType: StronglyTypedIdBackingType.Int)]

// both of them, because why not?
[StronglyTypedId(jsonConverter: StronglyTypedIdJsonProvider.NewtonsoftJson | StronglyTypedIdJsonProvider.SystemTextJson, backingType: StronglyTypedIdBackingType.Int)]

Are you okay with that approach?

@andrewlock
Copy link
Owner

Sounds good to me, thanks! 🙂

@andrewlock
Copy link
Owner

Thanks to @vebbo2 for implementing this in #11, I'll push out a release to NuGet shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants