使用 System.Text.Json.Serialization 将动态对象转换为 json 时抛出异常
Throw exception when converting dynamic object to json with System.Text.Json.Serialization
我想将动态对象转换为 json 字符串。当我过去使用 Newtonsoft.Json 时,它工作得很好。当我将 .net 核心升级到 3.0 并改用 System.Text.Json 时,它很糟糕。
查看代码:
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
dynamic product = new ExpandoObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
product.Tags = new string[] { "Real", "OnSale" };
// Will output: { "ProductName":"Elbow Grease","Enabled":true,"Price":4.90,
// "StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}
var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(product);
Console.WriteLine(json1);
// Both will throw exception: System.InvalidCastException:“Unable to
// cast object of type '<GetExpandoEnumerator>d__51' to type
// 'System.Collections.IDictionaryEnumerator'.”
var json2 = System.Text.Json.JsonSerializer.Serialize(product);
Console.WriteLine(json2);
var json3 = System.Text.Json.JsonSerializer.Serialize<IDictionary<string, object>>(product as IDictionary<string, object>);
Console.WriteLine(json3);
Console.ReadKey();
}
}
}
如果我想坚持使用 System.Text.Json 转换动态对象,因为我听说它比其他 json 转换更快,我该怎么办?
这被记录为 JsonSerializer support for ExpandoObject #38007 截至 2019 年 10 月 21 日仍然开放。
您可以改用匿名类型。
var product = new {
ProductName = "Elbow Grease",
Enabled = true,
Price = 4.90m,
StockCount = 9000,
StockValue = 44100,
Tags = new string[] { "Real", "OnSale" }
};
var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(product);
Console.WriteLine(json1);
var json2 = System.Text.Json.JsonSerializer.Serialize(product);
Console.WriteLine(json2);
{"ProductName":"Elbow Grease","Enabled":true,"Price":4.90,"StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}
{"ProductName":"Elbow Grease","Enabled":true,"Price":4.90,"StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}
我想将动态对象转换为 json 字符串。当我过去使用 Newtonsoft.Json 时,它工作得很好。当我将 .net 核心升级到 3.0 并改用 System.Text.Json 时,它很糟糕。
查看代码:
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
dynamic product = new ExpandoObject();
product.ProductName = "Elbow Grease";
product.Enabled = true;
product.Price = 4.90m;
product.StockCount = 9000;
product.StockValue = 44100;
product.Tags = new string[] { "Real", "OnSale" };
// Will output: { "ProductName":"Elbow Grease","Enabled":true,"Price":4.90,
// "StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}
var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(product);
Console.WriteLine(json1);
// Both will throw exception: System.InvalidCastException:“Unable to
// cast object of type '<GetExpandoEnumerator>d__51' to type
// 'System.Collections.IDictionaryEnumerator'.”
var json2 = System.Text.Json.JsonSerializer.Serialize(product);
Console.WriteLine(json2);
var json3 = System.Text.Json.JsonSerializer.Serialize<IDictionary<string, object>>(product as IDictionary<string, object>);
Console.WriteLine(json3);
Console.ReadKey();
}
}
}
如果我想坚持使用 System.Text.Json 转换动态对象,因为我听说它比其他 json 转换更快,我该怎么办?
这被记录为 JsonSerializer support for ExpandoObject #38007 截至 2019 年 10 月 21 日仍然开放。
您可以改用匿名类型。
var product = new {
ProductName = "Elbow Grease",
Enabled = true,
Price = 4.90m,
StockCount = 9000,
StockValue = 44100,
Tags = new string[] { "Real", "OnSale" }
};
var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(product);
Console.WriteLine(json1);
var json2 = System.Text.Json.JsonSerializer.Serialize(product);
Console.WriteLine(json2);
{"ProductName":"Elbow Grease","Enabled":true,"Price":4.90,"StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}
{"ProductName":"Elbow Grease","Enabled":true,"Price":4.90,"StockCount":9000,"StockValue":44100,"Tags":["Real","OnSale"]}