I have an Object representing an entity and I would like to have a constructor with a JSON string as parameter representing the Object instead of having a JsonUtils (or whatever) to desearialize.
Public Class Person
Public Property Age As Integer
Public Property Children As Integer
Public Property Married As Boolean
Public Sub New()
Public Sub New(ByVal sJson as String)
Me = serializer.Deserialize(Of Person)(sJson)
The simplest thing is simply to let the serializer create the new object:
Dim p As Person = serializer.Deserialize(Of Person)(sJson)
To keep the deserializtion code in the class, you could use a shared method to create the new object:
Dim p As Person = Person.FromJson(sJson)
It is not all that different from the syntax for self-deserialization:
Dim p As Person = New Person(sJson)
Finally, you could copy each property from a deserialized temp var to
Me.Age etc. But whether property by property or thru Reflection, this seems needlessly complex for what you describe.