反序列化为List(Of T)时传递构造函数参数 [英] Pass constructor arguments when deserializing into a List(Of T)

查看:104
本文介绍了反序列化为List(Of T)时传递构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过JSON.Net对List(of T)进行序列化和反序列化,其中T是一个对象,其中包含无法序列化的引用.这是一个简化的版本:

Class MyObject

    Private ReadOnly _Parent As Word.Document

    Property Foo As String
    Property Bar As String

    Sub New(Parent As Word.Document, Foo As String, Bar As String)
        Me.New(Parent)
        Me.Foo = Foo
        Me.Bar = Bar
    End Sub

    Sub New(Parent As Word.Document)
        _Parent = Parent
    End Sub

    <JsonConstructor>
    Private Sub New()
    End Sub

    Function GetFile() As System.IO.FileInfo
        Return New FileInfo(_Parent.FullName)
    End Function

End Class

对于这个故事,我将JSON字符串(序列化列表)存储在Word文档变量中.打开文档时,我将字符串取反序列化,然后希望能够将_Parent字段设置为引用同一文档. 困难不是知道_Parent应该引用什么,而是设置引用.请注意,我想将其保留为Private,但是如有必要,可以对其进行读取/写入.

是否可以告诉JSON.Net使用New(Parent As Word.Document)构造函数,并通过JsonConvert.DeserializeObject(Of T)传递此Parent参数? 还是至少要告诉JSON.Net,我想在反序列化之前/之后运行特定的Sub?

一个简单的绕过方法是在下面使用构造函数,但是我不喜欢它,因为如果同时打开多个文档,它可能会弄乱.

<JsonConstructor>
Private Sub New()
    _Parent = ThisWordApp.ActiveDocument
End Sub

我对C#中的响应很好.

解决方案

您可以采用此答案 将其他数据传递到JsonConverter 并创建

然后您可以按以下方式使用它:

Dim settings = New JsonSerializerSettings() With { .Converters = { new MyObjectConverter(document) } }
Dim list = JsonConvert.DeserializeObject(Of List(Of MyObject))(jsonString, settings)

注意:

演示小提琴此处.

I need to serialize and deserialize a List(of T) via JSON.Net, where T is an object which contains a reference which cannot be serialized. Here is a simplified version:

Class MyObject

    Private ReadOnly _Parent As Word.Document

    Property Foo As String
    Property Bar As String

    Sub New(Parent As Word.Document, Foo As String, Bar As String)
        Me.New(Parent)
        Me.Foo = Foo
        Me.Bar = Bar
    End Sub

    Sub New(Parent As Word.Document)
        _Parent = Parent
    End Sub

    <JsonConstructor>
    Private Sub New()
    End Sub

    Function GetFile() As System.IO.FileInfo
        Return New FileInfo(_Parent.FullName)
    End Function

End Class

For the story, I store the JSON string (serialized list) inside a Word document variable. When I open the document, I take the string, deserialize it, and then I would like to be able to set the _Parent field to refer to the same document. The difficulty is not in knowing what _Parent should reference to, but to set the reference. Note I want to keep it Private, however it could be read/write if necessary.

Is there a way to tell JSON.Net to use the New(Parent As Word.Document) constructor, and to pass this Parent argument via JsonConvert.DeserializeObject(Of T)? Or at least to tell JSON.Net I want to run a specific Sub before/after deserializing?

An easy bypass is be to have the constructor below, but I dot not like it as it may get messed up if several documents are opened at the same time.

<JsonConstructor>
Private Sub New()
    _Parent = ThisWordApp.ActiveDocument
End Sub

I'm fine with responses in C#.

解决方案

You could adopt the second approach from this answer to Pass additional data to JsonConverter and create a CustomCreationConverter(Of MyObject) that allocates an instance of MyObject using a Word.Document passed into the converter itself.

First, define the following converter:

Class MyObjectConverter
    Inherits CustomCreationConverter(Of MyObject)

    Private ReadOnly _Parent As Word.Document           

    Sub New(Parent As Word.Document)
        If Parent Is Nothing Then
            Throw New ArgumentNullException("Parent")
        End If
        _Parent = Parent
    End Sub

    Overrides Function Create(objectType as Type) As MyObject
        Return New MyObject(_Parent)
    End Function
End Class

Then you can use it as follows:

Dim settings = New JsonSerializerSettings() With { .Converters = { new MyObjectConverter(document) } }
Dim list = JsonConvert.DeserializeObject(Of List(Of MyObject))(jsonString, settings)

Notes:

  • This solution has the added advantage that you no longer need the <JsonConstructor> Private Sub New() constructor for MyObject and can completely remove it.

  • This converter would never be applied at compile time using JsonConverterAttribute, it should only be constructed in runtime given a known Word.Document (the document variable in the code sample above).

Demo fiddle here.

这篇关于反序列化为List(Of T)时传递构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆