我正在使用 System.Text.Json 包来使用序列化和反序列化。
当明确指定类型时,我可以将 json 字符串反序列化为对象,如下所示。
var data = JsonSerializer.Deserialize<PersonType>(jsonString);
Run Code Online (Sandbox Code Playgroud)
但动态类型不行。是否可以在不指定类型的情况下反序列化?谢谢你!
var data = JsonSerializer.Deserialize<dynamic>(jsonString);
Run Code Online (Sandbox Code Playgroud) 我正在BigInteger使用以下命令将 a 序列化为 JSON System.Text.Json:
JsonSerializer.Serialize(new {foo = new BigInteger(ulong.MaxValue) + 1})
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
{"foo":{"IsPowerOfTwo":true,"IsZero":false,"IsOne":false,"IsEven":true,"Sign":1}}
Run Code Online (Sandbox Code Playgroud)
如果我添加一个将值转换BigInteger为 a 的转换器ulong,它当然会失败,因为该BigInteger值太大:
var options = new JsonSerializerOptions();
options.Converters.Add(new BigIntegerConverter());
JsonSerializer.Serialize(new {foo = new BigInteger(ulong.MaxValue) + 1}, options);
Run Code Online (Sandbox Code Playgroud)
这是转换器:
public class BigIntegerConverter : JsonConverter<BigInteger>
{
public override BigInteger Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
public override void Write(Utf8JsonWriter writer, BigInteger value, JsonSerializerOptions options) => writer.WriteNumberValue((ulong)value);
}
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
{"foo":18446744073709551616}
Run Code Online (Sandbox Code Playgroud)
我知道这可以通过JsonWriter.WriteRawValueJson.NET 来实现,但我仅限于使用 …
c# biginteger json-serialization jsonconverter system.text.json
我想将动态对象转换为 json 字符串。当我过去使用 Newtonsoft.Json 时,它工作得很好。当我将 .net core 升级到 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
// …Run Code Online (Sandbox Code Playgroud) 我有这个jsonconverter,它需要将给定的属性值转换为小数或长整型,具体取决于该值 - 但我似乎无法确定属性值何时为小数或长整型,因为 tokentype 只能检测数字。 .我该如何解决这个问题?
public override IDictionary<string, object> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
IDictionary<string, object> output = new Dictionary<string, object>();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
string propertyName = reader.GetString();
reader.Read();
object? propertyValue = null;
switch (reader.TokenType)
{
case JsonTokenType.Number:
propertyValue = reader.GetInt64(); // or could be a decimal for where I should reader.GetDecimal()
break;
case JsonTokenType.String:
if (reader.TryGetDateTime(out DateTime value))
{
propertyValue = value;
}
else
{
propertyValue = reader.GetString();
} …Run Code Online (Sandbox Code Playgroud)