JsonSerializer.Deserialize JsonSerializerOptions

Scott Rider 1 Reputation point
2021-04-15T14:57:36.183+00:00

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?

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-04-15T20:43:51.82+00:00

    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);  
      
            }  
      
        }  
    }  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.