-3

I have a JSON file like this:

[
  {
    "Id": 1,
    "Size": "big",
    "Order": 6
  },
  {
    "Id": 2,
    "Size": "small",
    "Order": 4
  },
  {
    "Id": 3,
    "Size": "medium",
    "Order": 2,
    "chips": []
  }
]

The chips property is an array that may or may not appear in some object (It is currently null at this point). Should I declare the class for the json file like this:

 public class Settings
    { 
        public int Id { get;}
        public string Size { get;}
        public int Order { get;}
        public string[]? Chips { get;}
    }

with the ? or something like Nullable[] for the property instead?

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
CatyCatCat
  • 17
  • 3
  • Have you tried it? You can only use Nullable with value types. – CodeCaster Jan 15 '21 at 07:35
  • @MikNiller https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references – CodeCaster Jan 15 '21 at 07:37
  • @MikNiller thank you so functionality wise they are the same just different compiler warnings I guess. – CatyCatCat Jan 15 '21 at 07:40
  • What is the concern ? You want to avoid this property if the collection is empty or null? Or the question about `type?[]` vs `type[]?` vs `type[]`, like this https://stackoverflow.com/questions/54010316/nullable-array-and-why-do-we-need-them? – Drag and Drop Jan 15 '21 at 07:45
  • @Mik yeah you don't _need_ nullable reference types, but if you want to declare that an array can be null, use `string[]?`. – CodeCaster Jan 15 '21 at 07:48
  • @Mik no, that'll give you _"CS8370 Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater."_. – CodeCaster Jan 15 '21 at 07:58
  • @Mik no, you cannot slap a `?` on a reference type prior to C# 8, period. – CodeCaster Jan 15 '21 at 08:22
  • @DragandDrop So basically the object from the json file may or may not have the chips property to begin with. Those that actually have the chips property may find that the array is null. – CatyCatCat Jan 15 '21 at 08:40

1 Answers1

0

In short: you don't need Nullable<T> or ? in this case at all.

string[] is reference type:

Console.WriteLine(typeof(string[]).IsValueType);

the printed output will be false.

So, it can be null without any decoration.


Back to your sample. You need to specify setters as well to be able deserialize the given json fragement:

public class Settings
{
    public int Id { get; set; }
    public string Size { get; set; }
    public int Order { get; set; }
    public string[] Chips { get; set; }
}

Because the top-level entity is not an object that's why you need to use JArray to parse it first and then convert it to Settings via the ToObject (1):

var json = "[\r\n  {\r\n    \"Id\": 1,\r\n    \"Size\": \"big\",\r\n    \"Order\": 6\r\n  },\r\n  {\r\n    \"Id\": 2,\r\n    \"Size\": \"small\",\r\n    \"Order\": 4\r\n  },\r\n  {\r\n    \"Id\": 3,\r\n    \"Size\": \"medium\",\r\n    \"Order\": 2,\r\n    \"chips\": []\r\n  }\r\n]";
var semiParsedData = JArray.Parse(json);
var settings = semiParsedData.ToObject<Settings[]>();
Peter Csala
  • 17,736
  • 16
  • 35
  • 75