1

I have classes that has properties serialized and saved in json in dynamodb so it makes sense to use short names for these properties to save on dynamodb costs. Take for example the following class

[Serializable]
[DataContract]
public class UserState
{
    [DataMember(Name = "tis")]
    public HashSet<string> TableIds { get; set; }

    [DataMember(Name = "wp")]
    public Dictionary<string, int> PositionByTableId { get; set; }

    [DataMember(Name = "ut")]
    public DateTime UpdatedTime { get; set; }
}

We are also sending the serialized version of this class in a push notification to the frontend and would like to ignore short naming of properties when doing that.

Is there a way that we can serialize this class with short name properties for dynamodb and serialize with complete name properties for push notifications.

For dynamodb and push notifications both, we are using newtonsoft json serializer/deserializer.

Monku
  • 2,440
  • 4
  • 33
  • 57
  • You could either create a derived class and mark it with a `[JsonObject(MemberSerialization.OptOut)]` attribute or you could create your own custom contract resolver: https://stackoverflow.com/a/39300347/633098 – silkfire Oct 22 '21 at 21:56
  • Does [Configure JSON.NET to ignore DataContract/DataMember attributes](https://stackoverflow.com/q/11055225/3744182) answer your question? – dbc Oct 23 '21 at 02:07
  • 1
    Or if you just want to ignore the data member name and use the underlying property name, see [How to ignore JsonProperty(PropertyName = "someName") when serializing json?](https://stackoverflow.com/q/20622492/3744182). Does that answer your question? – dbc Oct 23 '21 at 14:58

0 Answers0