1

I have the following class and wish to use JsonProperty's DefaultValueHandling.IgnoreAndPopulate but I'm having problems at setting a List's default value. I tried the following code but there is an error during compilation. How can I set the list default value in this case?

I showed the my 3 tries, the default value in list2 compiles propely but doesn't work, the default value in list1 and list3 dont compile.

      public class MyClass
      {
            [DefaultValue("")]
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
            public string String1 { get; set; }


            [DefaultValue(0)]
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
            public double Double2 { get; set; }


            [DefaultValue(new List<string>())]
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
            public List<string> list1 { get; set; }

            [DefaultValue(new string[] {})]
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
            public List<string> list2 { get; set; }

            [DefaultValue(new List<string>())]
            [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
            public List<string> list3 { get; set; }
     }

Error : An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

Carlos Siestrup
  • 1,031
  • 2
  • 13
  • 33
  • You can't use complex objects in attributes, those only accept simple constants. Won't it also simply work to not make the lists settable but have JSON.NET populate them, i.e. `public List list1 { get; } = new List()`? That way you don't need the default handling at all. (Disclaimer: I have very little experience with JSON.NET.) – Jeroen Mostert Feb 01 '19 at 15:28
  • Since I'm adding the json to a database,I'm trying decrease json`s size as much as possible in order to save disk. – Carlos Siestrup Feb 01 '19 at 17:17
  • But that's a problem of serialization only. It seems cleaner to override that so the serialization of the lists is omitted entirely if they're empty -- that would be even smaller than serializing them as `null` properties. (Again, disclaimer: I don't know how this is most conveniently done with JSON.NET; at the very least it's possible to do manually.) – Jeroen Mostert Feb 01 '19 at 17:54
  • Looks like a duplicate of [How to make Json.Net skip serialization of empty collections](https://stackoverflow.com/q/18471864/3744182). [Can Newtonsoft Json.NET skip serializing empty lists?](https://stackoverflow.com/q/11320968/3744182) also applies, but the contract resolver from the accepted answer to the first answer is the best. Is this a perfect duplicate, or do you also need the to automatically allocate all collections during deserialization as suggested by your use of `IgnoreAndPopulate`? – dbc Feb 02 '19 at 05:39

0 Answers0