-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
System.Text.Json converter append additional property during serialization #56297
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsI'm implementing a converter. Here is the method signature I have to implement for the converter:
This is because I want a flat json representation of the object: {
"MyDescrimator":"foo",
"PropA":"bar",
"PropB":"bat
} However on the serialization side, when writing the object, I need to write the object, but first include this descrimator as an additional property. The problem is, when implementing this
I can workaround this problem by changing the structure of the json so that descrimator property sits outside the object: {
"MyDescrimator":"foo",
"Object": {
"PropA":"bar",
"PropB":"bat
}
} But that's un-necessary wrapper purely added to get around this underlying issue!
|
Have you considered exposing a Other than that, I would expect this issue to be addressed when #30083 is implemented. |
Yes, I'd prefer not to do this for multiple reasons which I'd be happy to go into.
Yeah thats not really going to be workable when you can have 10-15 derived types. I'd prefer a generic implementation so I can make proper use of generic types, and paramters so I don't have to write essentially the same class 10-15 times!
Ok will keep my eyes peeled. Another alternative i've considered is:
|
Thinking on it some more, I think i've found this workaround:
|
That might work, assuming you can tolerate the additional cycles used in the intermediate serializations and deserializations. |
Side note related to same problem in different area of the stack: Even if this solves the problem for |
I've forked your last question into a separate issue. Assuming your initial question has been answered, I'm going to close this as a duplicate of #30083. |
I'm implementing a converter for system.text.json
When writing / serializing the object I want to add an additional property that isn't part of its type definition.
Here is the method signature I have to implement for the converter:
TriggerConfig
is an abstract class. I'm doing polymorphic serialisation / deserilisation here. I've followed the docs for the deserialisation side, by taking a clone of the reader (which is a struct) reading ahead to validate the descriminator property, then if it's allowed, using the original reader to read the full object.This is because I want a flat json representation of the object:
However on the serialization side, when writing the object, I need to write the object, but first include this descrimator as an additional property.
The problem is, when implementing this
Write
method on the converter, I can't write the object, and then tag on an additional property becauseJsonSerializer.Serialize(writer, value, value.GetType());
writes the start and end object tokens giving me no opportunity to add a shadow property (a property that isn't part of the type definition).I can workaround this problem by changing the structure of the json so that descrimator property sits outside the object:
But that's un-necessary wrapper purely added to get around this underlying issue!
The text was updated successfully, but these errors were encountered: