Map class with json.net

Asked

Viewed 496 times

1

Is there any way to map the properties name of a class without using the Annotation JsonProperty of Json.NET?

The following structure shows two classes that inherit from the Bar class. However, the name json the ownership of the parent class (Bar) should be different in the serialization of the two daughter classes.

public class Bar
{
    [JsonProperty("property_1")]
    public string Property1 {get; set;}
}

public class Foo : Bar
{
    [JsonProperty("property_2")]
    public string Property2 {get;set;}
}

public class FooBar : Bar
{
    [JsonProperty("property_3")]
    public string Property3 {get;set;}
}

In addition, I would like to avoid placing Annotations on the application’s domain entities by performing the mapping to json in a separate project.

  • 1

    Have you ever tried to make the Property1 as virtual and change the annotation with Overload of the property?

1 answer

1

Vinicius, as an alternative, you can use the Attributo [DataContract]and [DataMember] in place of [JsonObject] and [JsonProperty] respectively.

But in doing so, you’ll have to put [DataMember] in all properties you want to serialize, once you omit the [DataMember] will be the same as adding a [JsonIgnore].

This approach can be quite useful if you want to expose your objects through a WebAPI or WCF, including the [DataMember] has the attribute EmitDefaultValue, if set to false, it will not serialize properties while having the value default.

But know if you have how to declare the name of the property in a way that does not use Attributes, unfortunately you will have to write your own JsonConverter, then maybe it’s not feasible.

if you prefer, you can add the attributes in other class, to make it use the Attributo [MetadataType], this approach is especially useful for maintaining attributes in automatically generated classes, so let’s take the following class as the basis:

public partial class MyClass
{
    public string Property1 {get; set;}
    public string Property2 {get; set;}
    public string Property3 {get; set;}
}

then we would have to do the following in a separate file:

[MetadataType(typeof(MyMetaData))]
public partial class MyClass
{

}

public class MyMetaData
{
    [DataMember(Name = "property_1")]
    public string Property1 {get; set;}

    [DataMember(Name = "property_2")]
    public string Property2 {get; set;}

    [DataMember(Name = "property_3")]
    public string Property3 {get; set;}
}

despite believing that the above answer has not solved your problem, I hope to have given you a north.

  • I tried using Jsonconverter but when calling an external library (NEST) that also uses Json.NET, it ignored the convert. I am testing with Datacontract and Datamember, overriding the properties I need to change the name. It’s not what I wanted, because I’m using DDD and I wanted to pass this configuration to the infrastructure layer, not the domain one, but should serve at the moment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.