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#.