1

I have a class like below, auto generated by Entity Framework, based in our database:

public partial class TB_Cliente
{
    public int IDCliente { get; set; }
    public string Nome { get; set; }

    // other properties
}

I'm using DataContractJsonSerializer and I need to change the properties' names in serialization. For instance, the property IDCliente must be serialized like ClientID.

I can't use [DataMember] in top of the property, because the class is auto generated, and any future changes will generate the class again and these changes will be lost.

I've had the same problem in the past, when I wanted to use data annotations. I've found the below solution, creating another file and using an interface, which works perfectly:

public interface ITB_Cliente
{
    [Required]
    string Nome { get; set; }

    // other properties
}

[MetadataType(typeof(ITB_Cliente))]
public partial class TB_Cliente : ITB_Cliente
{

}

But this solution doesn't help me now, because (as far as I know) this attribute must be set directly in the class. I've tried to set it in the interface and it didn't work.

Is there a way to change the properties' names in the serialization, in my case? Any help will be greatly appreciated.

Everton Lenger
  • 1,446
  • 2
  • 17
  • 32

2 Answers2

0

You probably want to use DTOs for serialization. I have not tried but AutoMapper can probably do the heavy lifting for you.

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • I don't know these things. Could you please provide a little bit more information about it to helping me start? How would be my class using it? :) – Everton Lenger Feb 25 '15 at 19:42
  • I don't know your architecture but if you bing/google "DTO entity framework" you will find a ton of useful links - e.g. http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 – Pawel Feb 25 '15 at 20:18
  • A doubt: in your suggestion, I'll have to create another class, set what I want in it and map this class to the one auto generated, is that? – Everton Lenger Feb 26 '15 at 13:13
  • Yes. Because you need to only serialize values and not the behavior. – Pawel Feb 26 '15 at 17:31
  • I've thought about creating another class, I think AutoMapper will help me with this. But my first intent with this question was see if there would be another option, without creating another class with the same properties. Just to be sure, there's no way to avoid that, then? – Everton Lenger Feb 26 '15 at 18:25
  • You can update the T4 template generating the code to generate the class the way you want it to look like... – Pawel Feb 26 '15 at 18:43
  • Huum, I've seen something like that, but I think it doesn't apply to my case, because, in each property, I have to specify what name it should have in the serialization. In any event, thanks for the attention and for the info! :) – Everton Lenger Feb 26 '15 at 19:37
0

I have been trying to overcome a similar problem this week for JSON output from some legacy VB.Net classes that I would prefer not to change if I can avoid it. The serialisation is returning underlying private member names rather than the public property names, e.g. "mFirstName".

Also for autogenerated property names I am getting json like

{"k__BackingField":"Brian","k__BackingField":"Furlong"}

which is not good.

I considered a similar approach to Pawel's above (create DTOs and use Automapper which I have used extensively before).

I am also checking to see if I can make a customised json serialiser but haven't got very far yet.

The third way I have investigated is to create an "Aspect" using PostSharp which will decorate the business entity classes with the DataContract.

This would allow me to create the necessary [DataContract] and [DataMember] attributes on the public properties at compile time without having to modify the legacy code base. As I am using the legacy assemblies within a new WebAPI assembly it effectively extends the code for me.

For guidance / hints please refer to the following links:

  1. For background information http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer
  2. For the question that gave the pointer: How to inject an attribute using a PostSharp attribute?
  3. For a walkthrough on how to do something similar which is enough to get going on this: http://www.postsharp.net/blog/post/PostSharp-Principals-Day-12-e28093-Aspect-Providers-e28093-Part-1
Community
  • 1
  • 1
Redeemed1
  • 3,953
  • 8
  • 38
  • 63