反序列化为List(Of T)时传递构造函数参数 [英] Pass constructor arguments when deserializing into a 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 并创建 然后您可以按以下方式使用它: 注意: 该解决方案的另一个优点是,您不再需要 演示小提琴此处. I need to serialize and deserialize a 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 Is there a way to tell JSON.Net to use the 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. 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 First, define the following converter: Then you can use it as follows: Notes: This solution has the added advantage that you no longer need the This converter would never be applied at compile time using Demo fiddle here. 这篇关于反序列化为List(Of T)时传递构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Dim settings = New JsonSerializerSettings() With { .Converters = { new MyObjectConverter(document) } }
Dim list = JsonConvert.DeserializeObject(Of List(Of MyObject))(jsonString, settings)
MyObject
的<JsonConstructor> Private Sub New()
构造函数,并且可以完全删除它.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
_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.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?<JsonConstructor>
Private Sub New()
_Parent = ThisWordApp.ActiveDocument
End Sub
CustomCreationConverter(Of MyObject)
that allocates an instance of MyObject
using a Word.Document
passed into the converter itself.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
Dim settings = New JsonSerializerSettings() With { .Converters = { new MyObjectConverter(document) } }
Dim list = JsonConvert.DeserializeObject(Of List(Of MyObject))(jsonString, settings)
<JsonConstructor> Private Sub New()
constructor for MyObject
and can completely remove it.JsonConverterAttribute
, it should only be constructed in runtime given a known Word.Document
(the document
variable in the code sample above).