using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static partial class JsonExtensions
public static T ToObject<T>(this JsonElement element, JsonSerializerOptions options = null)
var bufferWriter = new System.Buffers.ArrayBufferWriter<byte>();
using (var writer = new Utf8JsonWriter(bufferWriter))
return JsonSerializer.Deserialize<T>(bufferWriter.WrittenSpan, options);
public static T ToObject<T>(this JsonDocument document, JsonSerializerOptions options = null)
throw new ArgumentNullException(nameof(document));
return document.RootElement.ToObject<T>(options);
public class CarteGraphique
public string displayName { get; set; }
public string PrdStatus { get; set; }
public List<Retailer> retailers { get; set; }
public static void Test()
var url = @"https://api.nvidia.partners/edge/product/search?page=1&limit=9&locale=fr-fr&category=GPU&gpu=RTX%203090,RTX%203080%20Ti,RTX%203080,RTX%203070%20Ti,RTX%203070,RTX%203060%20Ti,RTX%203060&gpu_filter=RTX%203090~12,RTX%203080%20Ti~7,RTX%203080~16,RTX%203070%20Ti~3,RTX%203070~18,RTX%203060%20Ti~8,RTX%203060~2,RTX%202080%20SUPER~1,RTX%202080~0,RTX%202070%20SUPER~0,RTX%202070~0,RTX%202060~6,GTX%201660%20Ti~0,GTX%201660%20SUPER~9,GTX%201660~8,GTX%201650%20Ti~0,GTX%201650%20SUPER~3,GTX%201650~17";
using var client = new WebClient();
var json = client.DownloadString(url);
TestSystemTextJson(json);
static void TestSystemTextJson(string json)
Console.WriteLine("\nParsing with System.Text.Json...");
using var jsonParse = JsonDocument.Parse(json);
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var products = jsonParse.RootElement
.GetProperty("searchedProducts")
.GetProperty("productDetails")
.Where(n => n.GetProperty("isFounderEdition").GetBoolean())
.Select(n => n.ToObject<CarteGraphique>(options))
var featuredProduct = jsonParse.RootElement
.GetProperty("searchedProducts")
.GetProperty("featuredProduct")
.ToObject<CarteGraphique>(options);
products.Add(featuredProduct);
foreach (var item in products)
Console.WriteLine(item.displayName);
public static void TestNewtonsoft(string json)
Console.WriteLine("\nParsing with Newtonsoft...");
JObject jsonParse = JObject.Parse(json);
IList<CarteGraphique> products = new List<CarteGraphique>();
IList<JToken> productDetailsParse = jsonParse["searchedProducts"]["productDetails"]
.Where(n => n["isFounderEdition"].Value<bool>() == true)
var featuredProductParse = jsonParse["searchedProducts"]["featuredProduct"];
foreach (JToken item in productDetailsParse)
CarteGraphique result = item.ToObject<CarteGraphique>();
var featuredProduct = featuredProductParse.ToObject<CarteGraphique>();
products.Add(featuredProduct);
foreach (var item in products)
Console.WriteLine(item.displayName);
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(Newtonsoft.Json.JsonSerializer).Assembly.GetName().Name, typeof(Newtonsoft.Json.JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];