question

ScottRider-1472 avatar image
0 Votes"
ScottRider-1472 asked ScottRider-1472 commented

JsonSerializer.Deserialize JsonSerializerOptions

I have a Model can appears both as an Object and/or Array in data I am trying to deserialize. Is there an option that forces the deserializer to consume an Object as a single record Array?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ScottRider-1472 commented

There is no formal way to assert if json string is a single object of array.

If you were using Json.NET yes

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Newtonsoft.Json.Linq;
    
 namespace IsJsonType
 {
     class Program
     {
         static void Main(string[] args)
         {
             Test1();
             Test2();
             Console.ReadLine();
         }
    
         private static void Test1()
         {
             var json = @"[{""Name"": ""11"",""RootPath"": ""\\\\somepath\\HTTP\\abc""},{""Name"": ""22"",""RootPath"": ""\\\\somepath\\HTTP\\def""}]";
             var token = JToken.Parse(json);
    
             if (token is JArray)
             {
                 Console.WriteLine("array");
             }
             else if (token is JObject)
             {
                 Console.WriteLine("single object");
             }
    
         }
         private static void Test2()
         {
             var json = @"{""Name"": ""aaa"",""RootPath"": ""\\\\dddd\\HTTP\\qqqq""}";
             var token = JToken.Parse(json);
    
             if (token is JArray)
             {
                 Console.WriteLine("array");
             }
             else if (token is JObject)
             {
                 Console.WriteLine("single object");
             }
    
         }
    
     }
 }

Or this library yes

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using LightWeightJsonParser;
    
    
 namespace IsJsonType
 {
        
     class Program
     {
         static void Main(string[] args)
         {
             Test1();
             Test2();
             Console.ReadLine();
         }
    
         private static void Test1()
         {
             var json = @"[{""Name"": ""11"",""RootPath"": ""\\\\somepath\\HTTP\\abc""},{""Name"": ""22"",""RootPath"": ""\\\\somepath\\HTTP\\def""  }]";
             var jsonObject = LWJson.Parse(json);
             Console.WriteLine(jsonObject.IsArray);
    
         }
         private static void Test2()
         {
             var json = @"{""Name"": ""aaa"",""RootPath"": ""\\\\dddd\\HTTP\\qqqq""}";
             var jsonObject = LWJson.Parse(json);
             Console.WriteLine(jsonObject.IsArray);
    
         }
    
     }
 }



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your reply. I don't know Json.Net as well as say XPATH.

How do I target a section of a JSON response to see if I end up with [ {} {} {} ] or just {}.

I also had the idea of going to a XML node, then its parent and then counting the number of the original node(s) and add an attribute that Newtonsoft uses to declare a node is of the array type. But that is proving difficult as well.

0 Votes 0 ·

Also, I was thinking that ItemConverterType = typeof(List<Model>) for Newtonsoft visually looks it is a declaration in their decorator but that was also wrong.

0 Votes 0 ·

Json.net is the most downloaded NuGet package and rightfully so, you should check the library out. I've never had issues with Json.net. Although json.net can work with json in many ways the most common is to serialize and deserialize using a container class.

For example, I have a class with one or more properties of type list (which are arrays in json) we can read into a list, single object if needed e.g.

88421-f1.png

Then use lambda or linq to work with data plus using methods like I showed in my 1st post.


0 Votes 0 ·
f1.png (38.2 KiB)