How to ignore JsonProperty(PropertyName = “someName”) when serializing json?How to deserialize JSON property to class name, but ignore on serializingWhen Serializing a C# Class Using Json.Net can Custom Property Names be Ignored?JsonConvert - Ignore [PropertyName(“FooBar”)]?Remap JSON from JsonProperty to C# PropertyConfigure Json.Net to ignore property names specified via JsonPropertyAttributeSetting different Newtonsoft JSON conversion for serialization and deserializationASP.NET Core API JSON serializersettings per requestOverwrite Json property name in c#JsonProperty - Use different name for deserialization, but use original name for serialization?StackOverflow exception when using custom JsonConverter and custom ContractResolverSerializing to JSON in jQueryHow do I format a Microsoft JSON date?How can I pretty-print JSON in a shell script?.NET - JSON serialization of enum as stringHow can I pretty-print JSON using JavaScript?How do I turn a C# object into a JSON string in .NET?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?JSON.Net serializer ignoring JsonProperty?How can I change property names when serializing with Json.net?How to ignore @JsonProperty when Serialization in Java
"Correct me if I'm wrong"
Why are there no file insertion syscalls
King or Queen-Which piece is which?
How Hebrew Vowels Work
Would a 7805 5 V regulator drain a 9 V battery?
Is there a term for the belief that "if it's legal, it's moral"?
Story of a Witch Boy
What does it cost to buy a tavern?
Need help understanding the double sharp turn in Chopin's prelude in e minor
In a list with unique pairs A, B, how can I sort them so that the last B is the first A in the next pair?
How do I find which software is doing an SSH connection?
Boundaries and Buddhism
Elementary, my dear …
How to make all magic-casting innate, but still rare?
How is the idea of "girlfriend material" naturally expressed in Russian?
How can I ping multiple IP addresses at the same time?
I found a password with hashcat but it doesn't work
What is the most suitable position for a bishop here?
In the US, can a former president run again?
Justifying Affordable Bespoke Spaceships
Leaving job close to major deadlines
How did Frodo know where the Bree village was?
"What is the maximum that Player 1 can win?"
How can a clan of females defend themselves in the ancient world against wandering bands?
How to ignore JsonProperty(PropertyName = “someName”) when serializing json?
How to deserialize JSON property to class name, but ignore on serializingWhen Serializing a C# Class Using Json.Net can Custom Property Names be Ignored?JsonConvert - Ignore [PropertyName(“FooBar”)]?Remap JSON from JsonProperty to C# PropertyConfigure Json.Net to ignore property names specified via JsonPropertyAttributeSetting different Newtonsoft JSON conversion for serialization and deserializationASP.NET Core API JSON serializersettings per requestOverwrite Json property name in c#JsonProperty - Use different name for deserialization, but use original name for serialization?StackOverflow exception when using custom JsonConverter and custom ContractResolverSerializing to JSON in jQueryHow do I format a Microsoft JSON date?How can I pretty-print JSON in a shell script?.NET - JSON serialization of enum as stringHow can I pretty-print JSON using JavaScript?How do I turn a C# object into a JSON string in .NET?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?JSON.Net serializer ignoring JsonProperty?How can I change property names when serializing with Json.net?How to ignore @JsonProperty when Serialization in Java
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have some C# code using ASP.Net MVC, which is making use of Json.Net to serialize some DTOs. In order to reduce payload, I have made use of the [JsonProperty(PropertyName = "shortName")] attribute to use shorter property names during serialization.
This works great when the client is another .Net app or service, as the deserialization puts the object hierarchy back together, using the longer more friendly names, while keeping actual transfer payload low.
The problem comes into play when the client is javascript/ajax through a browser. It makes the request, and gets the json ... but that json is using the shortened less-friendly names.
How can I make the json.net serialization engine ignore the [JsonProperty(PropertyName = "shortName")] attribute programmatically? Ideally, my MVC service is going to sit there running and normally serialize using the shortened property names. When my code detects a particular parameter, I'd like to get the data serialized using the longer names and ignore the [JsonProperty()] attribute.
Any suggestions?
Thanks,
Kevin
c# json serialization json.net
add a comment |
I have some C# code using ASP.Net MVC, which is making use of Json.Net to serialize some DTOs. In order to reduce payload, I have made use of the [JsonProperty(PropertyName = "shortName")] attribute to use shorter property names during serialization.
This works great when the client is another .Net app or service, as the deserialization puts the object hierarchy back together, using the longer more friendly names, while keeping actual transfer payload low.
The problem comes into play when the client is javascript/ajax through a browser. It makes the request, and gets the json ... but that json is using the shortened less-friendly names.
How can I make the json.net serialization engine ignore the [JsonProperty(PropertyName = "shortName")] attribute programmatically? Ideally, my MVC service is going to sit there running and normally serialize using the shortened property names. When my code detects a particular parameter, I'd like to get the data serialized using the longer names and ignore the [JsonProperty()] attribute.
Any suggestions?
Thanks,
Kevin
c# json serialization json.net
just remove theJsonProperty
and return an anonymous object according to the parameter you passed. something likenewUserName=uname
– L.B
Dec 16 '13 at 22:57
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46
add a comment |
I have some C# code using ASP.Net MVC, which is making use of Json.Net to serialize some DTOs. In order to reduce payload, I have made use of the [JsonProperty(PropertyName = "shortName")] attribute to use shorter property names during serialization.
This works great when the client is another .Net app or service, as the deserialization puts the object hierarchy back together, using the longer more friendly names, while keeping actual transfer payload low.
The problem comes into play when the client is javascript/ajax through a browser. It makes the request, and gets the json ... but that json is using the shortened less-friendly names.
How can I make the json.net serialization engine ignore the [JsonProperty(PropertyName = "shortName")] attribute programmatically? Ideally, my MVC service is going to sit there running and normally serialize using the shortened property names. When my code detects a particular parameter, I'd like to get the data serialized using the longer names and ignore the [JsonProperty()] attribute.
Any suggestions?
Thanks,
Kevin
c# json serialization json.net
I have some C# code using ASP.Net MVC, which is making use of Json.Net to serialize some DTOs. In order to reduce payload, I have made use of the [JsonProperty(PropertyName = "shortName")] attribute to use shorter property names during serialization.
This works great when the client is another .Net app or service, as the deserialization puts the object hierarchy back together, using the longer more friendly names, while keeping actual transfer payload low.
The problem comes into play when the client is javascript/ajax through a browser. It makes the request, and gets the json ... but that json is using the shortened less-friendly names.
How can I make the json.net serialization engine ignore the [JsonProperty(PropertyName = "shortName")] attribute programmatically? Ideally, my MVC service is going to sit there running and normally serialize using the shortened property names. When my code detects a particular parameter, I'd like to get the data serialized using the longer names and ignore the [JsonProperty()] attribute.
Any suggestions?
Thanks,
Kevin
c# json serialization json.net
c# json serialization json.net
asked Dec 16 '13 at 22:44
KevinKevin
6614
6614
just remove theJsonProperty
and return an anonymous object according to the parameter you passed. something likenewUserName=uname
– L.B
Dec 16 '13 at 22:57
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46
add a comment |
just remove theJsonProperty
and return an anonymous object according to the parameter you passed. something likenewUserName=uname
– L.B
Dec 16 '13 at 22:57
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46
just remove the
JsonProperty
and return an anonymous object according to the parameter you passed. something like newUserName=uname
– L.B
Dec 16 '13 at 22:57
just remove the
JsonProperty
and return an anonymous object according to the parameter you passed. something like newUserName=uname
– L.B
Dec 16 '13 at 22:57
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46
add a comment |
1 Answer
1
active
oldest
votes
This can be done pretty easily using a custom contract resolver. Here's all the code you would need:
class LongNameContractResolver : DefaultContractResolver
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
prop.PropertyName = prop.UnderlyingName;
return list;
Here's a quick demo using the resolver:
class Program
static void Main(string[] args)
Foo foo = new Foo
CustomerName = "Bubba Gump Shrimp Company",
CustomerNumber = "BG60938"
;
Console.WriteLine("--- Using JsonProperty names ---");
Console.WriteLine(Serialize(foo, false));
Console.WriteLine();
Console.WriteLine("--- Ignoring JsonProperty names ---");
Console.WriteLine(Serialize(foo, true));
static string Serialize(object obj, bool useLongNames)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (useLongNames)
settings.ContractResolver = new LongNameContractResolver();
return JsonConvert.SerializeObject(obj, settings);
class Foo
[JsonProperty("cust-num")]
public string CustomerNumber get; set;
[JsonProperty("cust-name")]
public string CustomerName get; set;
Output:
--- Using JsonProperty names ---
"cust-num": "BG60938",
"cust-name": "Bubba Gump Shrimp Company"
--- Ignoring JsonProperty names ---
"CustomerNumber": "BG60938",
"CustomerName": "Bubba Gump Shrimp Company"
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
add a comment |
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%2f20622492%2fhow-to-ignore-jsonpropertypropertyname-somename-when-serializing-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be done pretty easily using a custom contract resolver. Here's all the code you would need:
class LongNameContractResolver : DefaultContractResolver
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
prop.PropertyName = prop.UnderlyingName;
return list;
Here's a quick demo using the resolver:
class Program
static void Main(string[] args)
Foo foo = new Foo
CustomerName = "Bubba Gump Shrimp Company",
CustomerNumber = "BG60938"
;
Console.WriteLine("--- Using JsonProperty names ---");
Console.WriteLine(Serialize(foo, false));
Console.WriteLine();
Console.WriteLine("--- Ignoring JsonProperty names ---");
Console.WriteLine(Serialize(foo, true));
static string Serialize(object obj, bool useLongNames)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (useLongNames)
settings.ContractResolver = new LongNameContractResolver();
return JsonConvert.SerializeObject(obj, settings);
class Foo
[JsonProperty("cust-num")]
public string CustomerNumber get; set;
[JsonProperty("cust-name")]
public string CustomerName get; set;
Output:
--- Using JsonProperty names ---
"cust-num": "BG60938",
"cust-name": "Bubba Gump Shrimp Company"
--- Ignoring JsonProperty names ---
"CustomerNumber": "BG60938",
"CustomerName": "Bubba Gump Shrimp Company"
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
add a comment |
This can be done pretty easily using a custom contract resolver. Here's all the code you would need:
class LongNameContractResolver : DefaultContractResolver
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
prop.PropertyName = prop.UnderlyingName;
return list;
Here's a quick demo using the resolver:
class Program
static void Main(string[] args)
Foo foo = new Foo
CustomerName = "Bubba Gump Shrimp Company",
CustomerNumber = "BG60938"
;
Console.WriteLine("--- Using JsonProperty names ---");
Console.WriteLine(Serialize(foo, false));
Console.WriteLine();
Console.WriteLine("--- Ignoring JsonProperty names ---");
Console.WriteLine(Serialize(foo, true));
static string Serialize(object obj, bool useLongNames)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (useLongNames)
settings.ContractResolver = new LongNameContractResolver();
return JsonConvert.SerializeObject(obj, settings);
class Foo
[JsonProperty("cust-num")]
public string CustomerNumber get; set;
[JsonProperty("cust-name")]
public string CustomerName get; set;
Output:
--- Using JsonProperty names ---
"cust-num": "BG60938",
"cust-name": "Bubba Gump Shrimp Company"
--- Ignoring JsonProperty names ---
"CustomerNumber": "BG60938",
"CustomerName": "Bubba Gump Shrimp Company"
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
add a comment |
This can be done pretty easily using a custom contract resolver. Here's all the code you would need:
class LongNameContractResolver : DefaultContractResolver
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
prop.PropertyName = prop.UnderlyingName;
return list;
Here's a quick demo using the resolver:
class Program
static void Main(string[] args)
Foo foo = new Foo
CustomerName = "Bubba Gump Shrimp Company",
CustomerNumber = "BG60938"
;
Console.WriteLine("--- Using JsonProperty names ---");
Console.WriteLine(Serialize(foo, false));
Console.WriteLine();
Console.WriteLine("--- Ignoring JsonProperty names ---");
Console.WriteLine(Serialize(foo, true));
static string Serialize(object obj, bool useLongNames)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (useLongNames)
settings.ContractResolver = new LongNameContractResolver();
return JsonConvert.SerializeObject(obj, settings);
class Foo
[JsonProperty("cust-num")]
public string CustomerNumber get; set;
[JsonProperty("cust-name")]
public string CustomerName get; set;
Output:
--- Using JsonProperty names ---
"cust-num": "BG60938",
"cust-name": "Bubba Gump Shrimp Company"
--- Ignoring JsonProperty names ---
"CustomerNumber": "BG60938",
"CustomerName": "Bubba Gump Shrimp Company"
This can be done pretty easily using a custom contract resolver. Here's all the code you would need:
class LongNameContractResolver : DefaultContractResolver
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
// Let the base class create all the JsonProperties
// using the short names
IList<JsonProperty> list = base.CreateProperties(type, memberSerialization);
// Now inspect each property and replace the
// short name with the real property name
foreach (JsonProperty prop in list)
prop.PropertyName = prop.UnderlyingName;
return list;
Here's a quick demo using the resolver:
class Program
static void Main(string[] args)
Foo foo = new Foo
CustomerName = "Bubba Gump Shrimp Company",
CustomerNumber = "BG60938"
;
Console.WriteLine("--- Using JsonProperty names ---");
Console.WriteLine(Serialize(foo, false));
Console.WriteLine();
Console.WriteLine("--- Ignoring JsonProperty names ---");
Console.WriteLine(Serialize(foo, true));
static string Serialize(object obj, bool useLongNames)
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
if (useLongNames)
settings.ContractResolver = new LongNameContractResolver();
return JsonConvert.SerializeObject(obj, settings);
class Foo
[JsonProperty("cust-num")]
public string CustomerNumber get; set;
[JsonProperty("cust-name")]
public string CustomerName get; set;
Output:
--- Using JsonProperty names ---
"cust-num": "BG60938",
"cust-name": "Bubba Gump Shrimp Company"
--- Ignoring JsonProperty names ---
"CustomerNumber": "BG60938",
"CustomerName": "Bubba Gump Shrimp Company"
edited Dec 17 '13 at 16:43
answered Dec 17 '13 at 16:37
Brian RogersBrian Rogers
81.5k19201213
81.5k19201213
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
add a comment |
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
1
1
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Love you Brian for this perfect answer. Hug from a fellow developer :)
– Prafulla
Aug 16 '18 at 12:44
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
Tbh I wish there was an easier solution
– Konrad
Apr 17 at 7:42
add a comment |
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%2f20622492%2fhow-to-ignore-jsonpropertypropertyname-somename-when-serializing-json%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
just remove the
JsonProperty
and return an anonymous object according to the parameter you passed. something likenewUserName=uname
– L.B
Dec 16 '13 at 22:57
I think the best approach would be to make a custom serializer (with json.NET, not from scratch) and remove the annotations. Make short names vs long names a setting of the serializer and just tell it which you want at the time of serialization. json.NET has no support for using/ignoring annotations at run time. If they are there at compile time, they will be used (baring some major hacking).
– evanmcdonnal
Dec 16 '13 at 23:03
I appreciate the comments there. @evanmcdonnal: if we went with a custom serializer, would we need to do this on a DTO-specific level (attached to every DTO?) We have many DTOs spread accross many solutions through our many development teams. Ideally we could find a relatively elegant way to attach into the serialization process without having to touch and maintain this through all DTOs accross the board.
– Kevin
Dec 17 '13 at 14:36
This can be done using a custom contract resolver. See my answer below.
– Brian Rogers
Dec 17 '13 at 16:45
@Kevin that really depends, you could do it in a general way without making any changes to the DTO's. In my code base there is some abstraction on top of json.NET. If I were to do the same I would be making my changes at that level and I would do something in the serializer like get the property name using reflection then take a substring of that to get the short name. Of course there's the minor trade off of losing control over the short names, but it would be a simple general solution.
– evanmcdonnal
Dec 17 '13 at 17:46