How to extend JSON serialization (JsonConverter) with TypeNameHandling.Auto, WITHOUT adding $type manually?
up vote
0
down vote
favorite
I need to extend default serialization of any object (that implements a specific interface) with, let's say for simplicity, additional properties in the generated JSON (and then be able to load this correctly).
My problem is that I've got TypeNameHandling set to Auto and if I create my custom JsonConverter (Newtonsoft.Json) and even invoke the original serialization (with the original supplied serializer by disabling my one for that time to avoid a loop) I am not getting the "$type" property generated automatically (which is required for proper deserialization). It's not automatically added by my parent and I cannot force the serializer to do it (reason below).
I am also not able to generate it myself, because I don't have any access to my context, i.e. the Type of the property that contains the object instance that I'm serializing, which states the expected type (to be compared with the actual one, to decide whether the $type should be generated or not). I can only generate it always. Is there any way to resolve this?
I want to avoid replacing the default serialization (only to extend it) and to keep the Auto
TypeNameHandling
behavior.
json serialization json.net deserialization
add a comment |
up vote
0
down vote
favorite
I need to extend default serialization of any object (that implements a specific interface) with, let's say for simplicity, additional properties in the generated JSON (and then be able to load this correctly).
My problem is that I've got TypeNameHandling set to Auto and if I create my custom JsonConverter (Newtonsoft.Json) and even invoke the original serialization (with the original supplied serializer by disabling my one for that time to avoid a loop) I am not getting the "$type" property generated automatically (which is required for proper deserialization). It's not automatically added by my parent and I cannot force the serializer to do it (reason below).
I am also not able to generate it myself, because I don't have any access to my context, i.e. the Type of the property that contains the object instance that I'm serializing, which states the expected type (to be compared with the actual one, to decide whether the $type should be generated or not). I can only generate it always. Is there any way to resolve this?
I want to avoid replacing the default serialization (only to extend it) and to keep the Auto
TypeNameHandling
behavior.
json serialization json.net deserialization
You have a couple of options: 1) You could have aJsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using[JsonConvert()]
or Generic method of modifying JSON before being returned to client...
– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior ofTypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).
– P.W.
Nov 22 at 12:46
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to extend default serialization of any object (that implements a specific interface) with, let's say for simplicity, additional properties in the generated JSON (and then be able to load this correctly).
My problem is that I've got TypeNameHandling set to Auto and if I create my custom JsonConverter (Newtonsoft.Json) and even invoke the original serialization (with the original supplied serializer by disabling my one for that time to avoid a loop) I am not getting the "$type" property generated automatically (which is required for proper deserialization). It's not automatically added by my parent and I cannot force the serializer to do it (reason below).
I am also not able to generate it myself, because I don't have any access to my context, i.e. the Type of the property that contains the object instance that I'm serializing, which states the expected type (to be compared with the actual one, to decide whether the $type should be generated or not). I can only generate it always. Is there any way to resolve this?
I want to avoid replacing the default serialization (only to extend it) and to keep the Auto
TypeNameHandling
behavior.
json serialization json.net deserialization
I need to extend default serialization of any object (that implements a specific interface) with, let's say for simplicity, additional properties in the generated JSON (and then be able to load this correctly).
My problem is that I've got TypeNameHandling set to Auto and if I create my custom JsonConverter (Newtonsoft.Json) and even invoke the original serialization (with the original supplied serializer by disabling my one for that time to avoid a loop) I am not getting the "$type" property generated automatically (which is required for proper deserialization). It's not automatically added by my parent and I cannot force the serializer to do it (reason below).
I am also not able to generate it myself, because I don't have any access to my context, i.e. the Type of the property that contains the object instance that I'm serializing, which states the expected type (to be compared with the actual one, to decide whether the $type should be generated or not). I can only generate it always. Is there any way to resolve this?
I want to avoid replacing the default serialization (only to extend it) and to keep the Auto
TypeNameHandling
behavior.
json serialization json.net deserialization
json serialization json.net deserialization
asked Nov 21 at 12:31
P.W.
422513
422513
You have a couple of options: 1) You could have aJsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using[JsonConvert()]
or Generic method of modifying JSON before being returned to client...
– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior ofTypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).
– P.W.
Nov 22 at 12:46
add a comment |
You have a couple of options: 1) You could have aJsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using[JsonConvert()]
or Generic method of modifying JSON before being returned to client...
– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior ofTypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).
– P.W.
Nov 22 at 12:46
You have a couple of options: 1) You could have a
JsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using [JsonConvert()]
or Generic method of modifying JSON before being returned to client...– dbc
Nov 21 at 15:59
You have a couple of options: 1) You could have a
JsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using [JsonConvert()]
or Generic method of modifying JSON before being returned to client...– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior of
TypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).– P.W.
Nov 22 at 12:46
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior of
TypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).– P.W.
Nov 22 at 12:46
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412105%2fhow-to-extend-json-serialization-jsonconverter-with-typenamehandling-auto-wit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have a couple of options: 1) You could have a
JsonConverter
that generates a default serialization then modifies it, as shown in JSON.Net throws StackOverflowException when using[JsonConvert()]
or Generic method of modifying JSON before being returned to client...– dbc
Nov 21 at 15:59
... or 2) You could add the properties with a custom contract resolver instead, as in Json.net Add property to every class containing of a certain type or How to add metadata to describe which properties are dates in JSON.Net. Does either approach work for you? Should your question be a duplicate of any of those?
– dbc
Nov 21 at 15:59
Thanks dbc. I was able to resolve my issue using different approach, by using a custom ContractResolver and skipping the JsonConverter altogether (similar to your 2nd suggestion set) - BUT - this still does not answer my original question. If I would be forced to use a custom JsonConverter (which gets the default serialization to modify it), I'm not able to auto-generate the "$type" property when (and only when) the instance is different than its property type, which is the behavior of
TypeNameHandling == Auto
(and I'm not able to get the property type inside the converter from anywhere).– P.W.
Nov 22 at 12:46