0
Dim serialized = Newtonsoft.Json.JsonConvert.SerializeObject(gridOrderNewState)
Dim gridOrderNewstate2 = Newtonsoft.Json.JsonConvert.DeserializeObject(Of gridTradingState)(serialized)

The result is almost empty version of gridOrderNewstate2

debug view

This is the value of serialized

{
   "bidInBase":0.99055,
   "askInBase":0.99333,
   "high":0.99448,
   "low":0.97528,
   "dateSinceLastTransactions":"2022-11-14T21:04:34.0103292+07:00",
   "suggestedLow":0.0,
   "suggestedhigh":0.0,
   "Orders":[
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807874",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.25,
         "priceInBasecoin":1.0,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":true,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807867",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.263,
         "priceInBasecoin":0.99778,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":true,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807859",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.277,
         "priceInBasecoin":0.99556,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":true,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807851",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.291,
         "priceInBasecoin":0.99333,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":true,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807835",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.334,
         "priceInBasecoin":0.98667,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":false,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807823",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.348,
         "priceInBasecoin":0.98444,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":false,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807816",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.363,
         "priceInBasecoin":0.98222,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":false,
         "marketURL":"https://gate.io/trade/frax_usdt"
      },
      {
         "time":"2022-11-14T13:09:54",
         "id":"227286807762",
         "customIdentifier":"t-FraxTest",
         "amountOfQuoteCurrencyBeingOrdered":6.377,
         "priceInBasecoin":0.98,
         "marketString":"gate",
         "Base":"USDT",
         "Quote":"FRAX",
         "isSellingInsteadOfBuying":false,
         "marketURL":"https://gate.io/trade/frax_usdt"
      }
   ]
}

So obviously something is there.

Yet I cannot deserialize into the same object class.

This is the class by the way

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
    ReadOnly Property Orders As OrderAlreadyPlacedAtanExchange()

    Public Shared Function creategridTradingOldState(market As MarketPairInanExchange, ordersInp As OrderAlreadyPlacedAtanExchange()) As gridTradingState
        Dim currentState = New gridTradingState

        'currentState._bidInBase = market.askbidInBase(False)
        'currentState._askInBase = market.askbidInBase(True)
        currentState._bidInBase = market.bidInBaseCoin
        currentState._askInBase = market.askInBaseCoin
        currentState._high = market.highInBaseCoin
        currentState._low = market.lowInBaseCoin
        currentState._Orders = ordersInp

        currentState._dateSinceLastTransactions = Now
        Return currentState
    End Function

    Public Sub updateSuggestedLowandHigh(suggestedLowNew As Decimal, suggestedHighNew As Decimal)
        _suggestedLow = suggestedLowNew
        _suggestedhigh = suggestedHighNew
    End Sub
End Class

Someone asked for a minimally reproduceable sample. I remove orders. The content of serialize becomes

{
    "bidInBase": 0.98735,
    "askInBase": 0.9918,
    "high": 0.99448,
    "low": 0.97528,
    "dateSinceLastTransactions": "2022-11-14T22:47:47.5532599+07:00",
    "suggestedLow": 0.0,
    "suggestedhigh": 0.0
}

And the problem stays. It seems that dbc is onto something

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user4951
  • 32,206
  • 53
  • 172
  • 282
  • Might you please [edit] your question to share a [mcve]? As it is your code doesn't compile, see https://dotnetfiddle.net/qgQ62K. – dbc Nov 14 '22 at 15:18
  • But as a guess, I notice that the properties of `gridTradingState` are readonly. So you must have one or more parameterized constructors for it. Json.NET can use a parameterized constructor to deserialize a class, but 1) If you have multiple constructors, you must mark the one to use with [`[JsonConstructor]`](https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm), see [JSON.net: how to deserialize without using the default constructor?](https://stackoverflow.com/q/23017716/3744182), or ... – dbc Nov 14 '22 at 15:20
  • ... 2) To match constructor arguments with JSON properties, the arguments must have the same (case-invariant) names as the properties, see [Newtonsoft json deserialise missing int values as nulls instead of zero](https://stackoverflow.com/q/52169522/3744182). – dbc Nov 14 '22 at 15:23
  • 3) Or (given that this is vb.net) maybe you don't have a parameterized constructor, and create it using static factory methods that set the underlying `_bidInBase` backing fields? If so Json.NET definitely won't set those automatically, it doesn't have any builtin logic for setting the private vb.net backing fields for readonly properties during deserialization. – dbc Nov 14 '22 at 15:34
  • Ah I see. So what should I do? I don't want to set those variables to be public. I want to keep it private – user4951 Nov 14 '22 at 15:41
  • Which guess was correct? 1, 2, or 3? – dbc Nov 14 '22 at 15:42
  • let me check by eliminating the Orders. the only complex object over there. – user4951 Nov 14 '22 at 15:44
  • After eliminating Orders the problem stay. I think all 3 is the problem. I need a constructor whose parameter is the same. Looks like I need to learn more about it. You can turn this into an answer. – user4951 Nov 14 '22 at 15:49
  • The parameters of your Constructor need to use ~the same names as the Properties, so you need to prepend `Me` to the name of the properties, as in, e.g. `'[...] Me.suggestedLow = SuggestedLow Me.suggestedhigh = SuggestedHigh '[...]`. Of course decorate the Constructor with ``. A parameter can also be, e.g., a `List(Of SomeType)`, if `SomeType` can be constructed in this context (with `[Assembly].CreateInstance()`) – Jimi Nov 14 '22 at 16:00
  • Please just turn this into answer. I will look around links you put – user4951 Nov 14 '22 at 16:21
  • @user4951 - OK, I'll do it later today (busy for the next few hours). – dbc Nov 14 '22 at 16:37
  • Anyway the correct answer is #3. I do not have default or parameterized constructor because I prefer to use Objective-C style creator function. – user4951 Nov 16 '22 at 07:50

0 Answers0