There isn't much documentation on how newtonsoft deserialize object
This suggest that with newtonsoft we need to use (non read only) public properties. Of course I do not want to use (non read only) public properties. I use only read only public properties.
But that causes problem
https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
Basically I have a simple class (I simplify it so you can see)
Class gridTradingState
ReadOnly Property bidInBase As Decimal
ReadOnly Property askInBase As Decimal
ReadOnly Property high As Decimal
ReadOnly Property low As Decimal
ReadOnly Property dateSinceLastTransactions As Date
ReadOnly Property suggestedLow As Decimal
ReadOnly Property suggestedhigh As Decimal
end class
See. It's a simple class.
The json looks like this (I've changed the code after but should not be much different. As far as I know, none)
{"bidInBase":0.00033485,"askInBase":0.00034407,"highInBase":0.00045,"lowInBase":0.00025797,"dateSinceLastTransactions":"2022-11-21T20:48:00.1243771+07:00","suggestedLow":0.0002,"suggestedhigh":0.0006}]}
The class doesn't have any default new contractor
In Why I cannot deserialize objects that I just serialized? I asked why it doesn't work.
What happened is newtonsoft will default using the default contractor and then set the properties.
The problem is for newtonsoft to set the properties from outside the class, the properties must be public. Well I do not like public properties.
So how to resolve that dilemma?
Currently the way I am doing it is
I put these
Protected Sub New(bidInBase As Decimal, askInBase As Decimal, highInBase As Decimal, lowinbase As Decimal, orders As OrderAlreadyPlacedAtanExchange(), dateSinceLastTransactions As Date)
_bidInBase = bidInBase
_askInBase = askInBase
_highInBase = highInBase
_lowInBase = lowinbase
_dateSinceLastTransactions = dateSinceLastTransactions
End Sub
Public Shared Function creategridtradingOldStateFromJson(json As String) As gridTradingState
Dim jo = JToken.Parse(json)
Dim dateInJson = Convert.ToDateTime(jo.Item("dateSinceLastTransactions").ToString)
'Dim \
Dim orders As OrderAlreadyPlacedAtanExchange() = Nothing
Dim output = New gridTradingState(CDec(jo.Item("bidInBase")), CDec(jo.Item("askInBase")), CDec(jo.Item("high")), CDec(jo.Item("low")), orders, dateInJson)
Return output
End Function
That is, I do not use deserialize at all. Instead I just manually insert those stuffs one by one.
And that can't be the right way to do it.
So what is the right way to deserialize a class?
We want public read only variables by the way.