json Newtonsoft对具有只读属性的对象进行反序列化的最佳做法是什么?

zz2j4svz  于 2022-11-26  发布在  其他
关注(0)|答案(1)|浏览(207)

关于newtonsoft如何反序列化对象的文档不多
这意味着newtonsoft需要使用(非只读)公共属性。当然我不想使用(非只读)公共属性。我只使用只读公共属性。
但这会带来问题
https://www.newtonsoft.com/json/help/html/SerializingJSON.htm
基本上我有一个简单的类(我简化了它,这样你就可以看到)

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

看,这是个简单的课程
json看起来像这样(我修改了代码,但应该没有太大的不同。据我所知,没有)

{"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}]}

该类没有任何默认的新承包商
在为什么我不能反序列化我刚刚序列化的对象?我问为什么它不工作。
所发生的是newtonsoft将默认使用默认承包商,然后设置属性。
问题是newtonsoft要从类外设置属性,属性必须是公共的。嗯,我不喜欢公共属性。
那么,如何解决这一困境呢?
目前我的做法是
我把这些

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

也就是说,我根本不使用反序列化。相反,我只是手动插入这些东西一个接一个。
这不可能是正确的方法。
那么,反序列化类的正确方法是什么呢?
顺便说一下,我们需要公共只读变量。

qoefvg9y

qoefvg9y1#

如果你有一个构造函数,它的参数与json中的名称匹配,它就会工作,属性可以是只读的。

Sub Main
    Dim json As String = "
    {
       ""bidInBase"":0.00033485,
       ""askInBase"":0.00034407,
       ""high"":0.00045,
       ""low"":0.00025797,
       ""dateSinceLastTransactions"":""2022-11-21T20:48:00.1243771+07:00"",
       ""suggestedLow"":0.0002,
       ""suggestedhigh"":0.0006
    }"
    Dim result = JsonConvert.DeserializeObject(Of gridTradingState)(json)
End Sub

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

    Public Sub New(bidInBase As Decimal, askInBase As Decimal, high As Decimal, low As Decimal, dateSinceLastTransactions As Date, suggestedLow As Decimal, suggestedhigh As Decimal)
        Me.bidInBase=bidInBase
        Me.askInBase=askInBase
        Me.high=high
        Me.low=low
        Me.dateSinceLastTransactions=dateSinceLastTransactions
        Me.suggestedLow=suggestedLow
        Me.suggestedhigh=suggestedhigh
    End Sub
End Class

相关问题