CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/60123376/jsonserializer-deserialize-fails
public class User {
public int Id { get; set; }
}
class TestClass
{
public static void Test()
{
int id = 9;
string str = "{\"id\": " + id + "}";
Console.WriteLine(str);
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var u = JsonSerializer.Deserialize<User>(str, options);
Console.WriteLine($"User ID: {u.Id}, Correct: {id == u.Id}"); // always 0/init/default value
Assert.IsTrue(u.Id == id);
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1}), {2}, {3}",
System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription, Environment.Version, Environment.OSVersion,
System.Runtime.InteropServices.RuntimeInformation.OSDescription);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
}