3

Around here, it's very common to create POCOs with long properties names, such as "WarnIfAnyAmountInDebtLongerThan30Days", and some lists have up to 1k items which results on a 1MB Json to be cached/transferred...
I was wondering if there's a way to tell JSON.Net to, instead of serializing with that huge property name, compress it as "P1" (for example), creating some sort of internal dictionary for later deserialization...

Is this possible? or do I have to go to plan B and create another POCO with reduced property names and use a mapper to go back-forth...

EDIT 1:
Creating an alias is not what I want because I have thousands of javascripts and other moving parts depending on that property name... I would like to "compress" it only when serializing for caching!

Leonardo
  • 10,737
  • 10
  • 62
  • 155
  • 2
    Possible duplicate of [How can I change property names when serializing with Json.net?](http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net) – MakePeaceGreatAgain Jan 19 '17 at 12:28
  • 1
    As for every serializer you can put an alias on top of the member to be serialized indicating the name in the output-format. – MakePeaceGreatAgain Jan 19 '17 at 12:28
  • @HimBromBeere pls check edit 1 – Leonardo Jan 19 '17 at 12:32
  • I suppse you can make some `String.Replace` searching for something like `\S+:` as regex and replace it by a new name. But I guess doing so is somewhat ugly... Anyway I wonder how you want to get the right data from a so manipulated JSON. You have to be able to revert this mechanism in any way, don´t you? – MakePeaceGreatAgain Jan 19 '17 at 12:35
  • It's not standard, so either change the property names or create a wrapper/mapper both sides. – Jeroen van Langen Jan 19 '17 at 12:35
  • 3
    Honestly, if you're that worried about payload size, don't use serialisation technologies that use reflection and do serialization the old-school way that of binary serialization. This contemporary notion of a payload that includes both schema/property names together with the payload just for the benefit of humans is just too verbose; inefficient and silly. –  Jan 19 '17 at 12:39

2 Answers2

2

You can decorate your POCOs with the JsonProperty attribute and specify the field name that should be used in the Json...

    [JsonProperty("S1")]
    public string SomeReallyVeryLongName {get; set;}
}

Documentation: https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm

It would have to do be done on all your POCOs, but it's a one-off, consistent, predictable and machine-readable.

If you've got a lot of classes to decorate, you could use something like Fody or PostSharp to do it for you.

Basic
  • 26,321
  • 24
  • 115
  • 201
0

Depends on the usage you have on the deserialization side. You could try BSon which is a binary JSon allowing you to reduce the amount of data transfered...

http://www.newtonsoft.com/json/help/html/SerializeToBson.htm

Bruno Belmondo
  • 2,299
  • 8
  • 18