6

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

5
  • Maybe this will help : stackoverflow.com/questions/13839426/… Commented Nov 25, 2016 at 15:40
  • Maybe this can help: [JsonProperty(Title="People")] public string propertyName1{get; set;} Commented Nov 25, 2016 at 18:31
  • Please show serialization/deserialization code that is doing the wrong thing. Commented Nov 25, 2016 at 18:46
  • Thanks... The problem is not with the properties but with the class name. As I'm using the simple.odata.client nuget package I'm trying to deserialize a resource entity that is named "People" into a .net class (model) named "Employee". At this point the simple.odata.client looks for a service resource (entity) named "Employee" that can't be found. Commented Nov 25, 2016 at 19:05
  • Have you tried serializing to JSON string first then deserializing that string into you Employee class? Might not be good for performance but it's a workaround. Commented Nov 26, 2016 at 15:50

1 Answer 1

6

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"}}
Sign up to request clarification or add additional context in comments.

2 Comments

When using the DataContractAttribute you must also specify the DataMemberAttribute on properties you want to serialize.
Since I was using Simple.OData.Client, that is not able to recognize the DataContract attribute, I had to pass the name of my remote element explicitly. I, anyway, used a custom attribute like "ClassName" with a property that returns the name of the class but this is too much work for its purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.