System.Text.Json-将嵌套对象反序列化为字符串

凯里洛M

我试图使用System.Text.Json.JsonSerializer来部分反序列化模型,因此属性之一被读取为包含原始JSON的字符串。

public class SomeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Info { get; set; }
}

示例代码

var json = @"{
                 ""Id"": 1,
                 ""Name"": ""Some Name"",
                 ""Info"": {
                     ""Additional"": ""Fields"",
                     ""Are"": ""Inside""
                 }
             }";

var model = JsonSerializer.Deserialize<SomeModel>(json);

应该生成模型,该模型Info包含原始JSON的Info对象作为字符串:

{
    "Additional": "Fields",
    "Are": "Inside"
}

它开箱即用,并引发异常:

System.Text.Json.JsonException:---> System.InvalidOperationException:无法获取标记类型“ StartObject”的值作为字符串。

到目前为止,我尝试了什么:

public class InfoToStringConverter : JsonConverter<string>
{
    public override string Read(
        ref Utf8JsonReader reader, Type type, JsonSerializerOptions options)
    {
        return reader.GetString();
    }

    public override void Write(
        Utf8JsonWriter writer, string value, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

并将其应用到模型中

[JsonConverter(typeof(InfoToStringConverter))]
public string Info { get; set; }

并添加选项到 JsonSerializer

var options = new JsonSerializerOptions();
options.Converters.Add(new InfoToStringConverter());
var model = JsonSerializer.Deserialize<SomeModel>(json, options);

尽管如此,它仍然抛出相同的异常:

System.Text.Json.JsonException:---> System.InvalidOperationException:无法获取标记类型“ StartObject”的值作为字符串。

什么才是我需要的正确食谱?使用,它以类似的方式工作Newtonsoft.Json

更新资料

对我来说,保持嵌套的JSON对象尽可能原始很重要。因此,我会避免像反Dictionary序列化和序列化那样的选项,因为我担心会引入不需要的更改。

凯里洛M

找到了正确的方法,如何正确读取中的嵌套JSON对象JsonConverter完整的解决方案如下:

public class SomeModel
{
    public int Id { get; set; }

    public string Name { get; set; }

    [JsonConverter(typeof(InfoToStringConverter))]
    public string Info { get; set; }
}

public class InfoToStringConverter : JsonConverter<string>
{
    public override string Read(
        ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        using (var jsonDoc = JsonDocument.ParseValue(ref reader))
        {
            return jsonDoc.RootElement.GetRawText();
        }
    }

    public override void Write(
        Utf8JsonWriter writer, string value, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }
}

在代码本身中,甚至不需要创建选项:

var json = @"{
                 ""Id"": 1,
                 ""Name"": ""Some Name"",
                 ""Info"": {
                     ""Additional"": ""Fields"",
                     ""Are"": ""Inside""
                 }
             }";

var model = JsonSerializer.Deserialize<SomeModel>(json);

Info属性中的原始JSON文本甚至包含示例中引入的额外空格,以提高可读性。

@PavelAnikhouski在他的回答中没有提及模型表示及其序列化。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否可以使用 System.Text.Json 将 json 字符串反序列化为动态对象?

如何使用System.Text.Json将所有`Nullable <T>`值类型的空字符串反序列化为空值?

System.Text.Json 将空字符串全局序列化为空字符串

反序列化为带有json字符串缺少的enum属性的模型时,强制System.Text.Json失败

c# system.text.json 将“不同”对象反序列化为一个类的数组

使用 System.Text.Json 将 json 反序列化为多个类

使用System.Text.Json将JSON反序列化为包含动态属性的类

如何让 System.Text.Json 将对象反序列化为其原始类型?

使用system.text.json从大量字符串数组中反序列化非常大的json

如何使用 System.Text.Json 將 json 字符串或流反序列化為 Dictionary<string,string>

如何使用 System.Text.Json 将 Arraylist 项反序列化为具体类型?

使用System.Text.Json反序列化数组json对象的嵌套数组

将JSON反序列化为C#对象以将嵌套数组显示为网格中的字符串

System.Text.Json对象数组反序列化

C#Json.net将嵌套的json反序列化为字符串

如何使用System.Text.Json API将流反序列化到对象

将JSON字符串反序列化为Class

将Json Schema反序列化为Json字符串或对象

ServiceStack.Text将字符串反序列化为单个对象空引用

将Json字符串反序列化为对象Java

将json字符串反序列化为对象c#.net

将json字符串反序列化为python中的对象

将json字符串反序列化为python中的对象

将JSON对象属性反序列化为字符串

使用Jackson将JSON字符串或对象反序列化为String字段

将Json字符串反序列化为多种对象类型

如何将json字符串反序列化为对象

将json字符串反序列化为.NET对象列表

将字符串反序列化为JSON对象时出错