0

I have an external data contract, which cannot be modified. Json is used to serialize these classes. I need to save on output data size, so I would like to change original fields names to their shorter versions, i.e.:

public ClassWithLongName
{
    public string FieldWithLongName = "field1";
    public string FieldWithEvenLongerName = "field2";
} 

should be serialzed to something like:

{"f1" = "field1", "f2" = "field2"}

To have ability to deserialize it back properly, mapping between original and short names must be provided somehow (I was thinking about dictionary. Should I use CustomConverter or IContractResolver for that?

EDIT:

There is a way to do it using CustomContractResolver : DefaultContractResolver. I am overriding CreateProperty method to swap the property name. A the moment the problem is that I can't get serialized object type (so all the properties' names are being held in one common map, instead of separate maps per type). Do you know how can I get serialized object type from DefaultContractResolver?

EDIT2:

The type can be taken from ResolveContract method (it is passed as parameter here). I have achieved the functionality I needed, but there is one huge problem with it. Performance. I counted time elapsed fo serializing simple object 10000 times in a loop. For default serialization it was about 150ms and for custom serialization about 15s. Do you know if there is a way to avoid it. I suppose that the overhead is caused by reflection. But how can default serializer avoid it? I am using DefaultContractResolver as parent class for my CustomContractResolver, so it should use all the mechanisms that parent class is using..

2 Answers 2

1

Use anonymouse object:

ClassWithLongName obj = new ClassWithLongName();
var anonymouse  =  new { FieldWithLongName = obj.FieldWithEvenLongerName, FieldWithEvenLongerName = obj.FieldWithEvenLongerName };

JavaScriptSerializer serializer = new JavaScriptSerializer();
var output = serializer.Serialize(anonymouse);
Sign up to request clarification or add additional context in comments.

3 Comments

And how would I deserialize objects? How would deserializer know which field in json corresponds to which field in c# object?
JsonConvert.DeserializeObject<type>(myjsondata);
And the first question should be: how whould the output looks like (and how can I customize it)?
1

If you didn't want to use anonymous objects, you could create your own object and use AutoMapper to map the external data contract into your own class and then serialize that using JavaScriptSerializer classes

3 Comments

As mentioned in the title, I would like to use Json.Net library. Additionally at the moment copying objects (like Automapper does) is not an option. I am asking for possibilities of customization of serialization (and deserialization) in that specific library (Newtonsoft Json.Net).
you are not going to be able to customize the output without having access to the classes. You will either need to map to your own classes and serialize or create an anonymous object and serialize that
I think there is a simple solution to it. I can use decorator pattern to decorate original properties with json attributes.

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.