Search code examples
c#jsonserializationxamarin.formssimple.odata.client

Json.Net serialize/deserialize Class Name attribute C#


Sorry for the (maybe) trivial question but, I'm trying to consume a web service where the entities and my data model classes are named different.

I want to keep my model .Net Class name and use a Json Attribute name to map, serializer/deserializer, with the corresponding web service entity. For example:

Web Service Entity:

"People"

My Model Class:

"Employee"

What I've already do:

[JsonObject(Title="People")]
public class Employee 
{

   [JsonProperty("DifferentPropertyName")]
   string propertyName1 { get; set; }
}

But the json serializer/Deserializer continues to use the .Net class name and I need to set the jsonObject Title.

There is a way to achieve it?

EDIT

I'm working on a Xamarin Forms app, using Simple.OData.Client to consume an OData Service

Thanks


Solution

  • DataContractAttribute may be your solution.

    public class RichFilter
    {
       public Trolo item { get; set; }
    }
    [DataContract(Name = "item")]
    public class Trolo 
    { 
       public string connector { get; set; } 
    }
    

    If you serialize a RichFilter object, here is the output :

    {"item":{"connector":"AND"}}