How to extend JSON serialization (JsonConverter) with TypeNameHandling.Auto, WITHOUT adding $type manually?
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 |
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 '18 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 '18 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 '18 at 12:46
add a comment |
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 '18 at 12:31
P.W.P.W.
432513
432513
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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 12:46
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 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 '18 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 '18 at 12:46