-
Notifications
You must be signed in to change notification settings - Fork 84
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
Specify converter for JsonSerializerContext #141
Comments
Hey, sorry for the delay, I managed to miss this... did you ever get to the bottom of the issue? I've just given it a try and I can see that it has the behaviour you describe, but I'm not sure why 🤔 It's as though the source generator just isn't paying any attention to the EDIT: I stand corrected, I can reproduce the error, and that's likely the source of the issue. I haven't figured out why yet though... |
Ok, I've just figured it out. Source generators can't see the output of other source generators. So the Json Source Generator can't see the the converter generated by Unfortunately, currently, there's no workaround for this, other than having the IDs defined in a different project to the project in which you're creating your |
One possible workaround is the approach I use in the tests: StronglyTypedId/test/StronglyTypedIds.IntegrationTests/SystemTextJsonSerializerContext.cs Lines 18 to 30 in 2313453
This doesn't help with the [JsonSerializable(typeof(Project))]
internal partial class ProjectSourceContext : JsonSerializerContext
{
internal static SystemTextJsonSerializerContext Custom
=> new(new JsonSerializerOptions
{
Converters =
{
new ProjectId.ProjectIdSystemTextJsonConverter(),
}
});
} And then you can use it like this: var serialized = JsonSerializer.Serialize(project, ProjectSourceContext.Custom.Project); |
When using version
1.0.0-beta08
of the library in a.net 8
app, if you specify aSystem.Text.Json.Serialization.JsonSerializerContext
, the StronglyTypedId will be serialized as an empty object.Definition:
Ideal usage:
Value of
serialized
string:I am currently using the following solution:
In this case, you will get the expected JSON:
{"id":"00000000-0000-0000-0000-000000000001","name":"Project 1"}
. If you comment out the lineoptions.Converters.Add()
, you will still receive the incorrect serialized string with an empty object as the id.This is the ideal solution I'm looking for:
but I'm receiving the following compilation error:
Is there another way to specify the converter for the
Default
serializer context object, to avoid specifying the options everywhere?The text was updated successfully, but these errors were encountered: