0

Is it possible to specify a different name for a Json property when serializing vs de-serializing? For example, if we have the following class (in c#):-

public class SomeClass
{
    [JsonProperty("Is New Product")]
    public bool IsNewProduct { get; set; }
}

We are using the "Is New Product" property to aid de-serialization as we are unable to control the name within the Json. When we re-serialize the class we want to retain the C# property name "IsNewProduct", but (as expected) it serializes as "Is New Product".

Is it possible to specify different property names for Serialization vs De-Serialization, or somehow stipulate to ignore the JsonProperty name when serializing?

Stephen Garside
  • 1,185
  • 10
  • 15
  • Why do you want to achieve the same as the current mapping ensures consistency b/w contract and JSON, i.e how will you serialize and deserialize back the same JSON to the object? – Ipsit Gaur Feb 09 '18 at 11:53
  • JsonProperty •By default, the JSON property will have the same name as the .NET property. This attribute allows the name to be customized. – Johnny Feb 09 '18 at 11:55
  • 1
    Looks like a duplicate of [How to ignore JsonProperty(PropertyName = “someName”) when serializing json?](https://stackoverflow.com/q/20622492/3744182). – dbc Feb 09 '18 at 12:02
  • We dont have control of the initial property names which are not C# friendly e.g. "some name / other name" , so we are using the JsonProperty to deserialize. We then want to pass the data onto another service with the corrected property name - hence why we want to serialize with the C# property name – Stephen Garside Feb 09 '18 at 12:03
  • Thank you! I will look at that suggestion – Stephen Garside Feb 09 '18 at 12:05
  • Or, for something fancier, there's `JsonConditionalNameAttribute` from [JsonProperty WebApi request and response models](https://stackoverflow.com/q/34362628/3744182) if you need multiple different override names. – dbc Feb 09 '18 at 12:08

0 Answers0