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

Then use lambda or linq to work with data plus using methods like I showed in my 1st post.
9 people are following this question.
Insert a node as child ,before or after a node in nested dynamic JSON Node using C#
Visual Studio 2019: Undefined behavior in a C++/CLI wrapper project.
Example for how to get Package Metadata from Azure DevOps Rest-Api Artifacts using c#
How to collapse individual nested grids/stackpanels inside a grid?