andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.52k stars 80 forks source link

Json serialization outputs a json object rather than a guid #87

Closed seanrockster closed 1 year ago

seanrockster commented 1 year ago

Hi

Am I missing something? I've read through the blog and I'm thinking the json serializer is auto generated, and I can see its there but still when I return an object from my api the stronlytypedid instance is seralized as an object rather than just its value.

using .net6, StronglyTypedId 0.2.1

{ "id": { "value": "2c2cfdf4-08a2-41fe-af97-063cc72b7fd8" },

Regards

y2kbugger commented 1 year ago

you need to configure it to use the right converter. By default it just creates the converter for Newtonsoft but not System.Text.Json. You can also configure the backing type.

See: https://github.com/andrewlock/StronglyTypedId#changing-the-defaults-globally

[StronglyTypedId(
    backingType: StronglyTypedIdBackingType.Int,
    converters: StronglyTypedIdConverter.SystemTextJson)]
public partial struct OrderId { }
andrewlock commented 1 year ago

Hope that's solved your problem @seanrockster. Feel free to re-open if it doesn't