FastJson编码器错误



我有一个保存销售数据的类Sale,然后我在Sale类中有一个属性,它是一个salesitem类的列表。

Sale
-date
-SaleItems <-- List(Of SaleItem)
-total

SaleItem
-productID
-amt
-qty

当我转换为Json字符串和调试打印输出。salesitems没有编码,只是缺少了。

我做错了什么,或者这可能是一个bug?

编辑

Sale类

Public Class Sale
    Private _Paid As Decimal
    Private _ListSaleItems As List(Of SaleItem)
    Public Sub New()
        ClearSaleItem()
    End Sub
    Public ReadOnly Property SaleItems() As List(Of SaleItem)
        Get
            Return _ListSaleItems
        End Get
    End Property

    Public Property Paid() As Decimal
        Get
            Return _Paid
        End Get
        Set
            _Paid = value
        End Set
    End Property

    Public Sub ClearSaleItem()
        _ListSaleItems = New List(Of SaleItem)()
    End Sub

    Public Function AddSaleItem(value As SaleItem) As Decimal
        If value Is Nothing Then
            Throw New ApplicationException("SaleItem cannot be empty")
        End If
        _ListSaleItems.Add(value)
    End Function
End Class

salesitem类

Public Class SaleItem
    Private _ID As Int32
    Private _Quantity As Int32
    Private _SellPrice As Decimal

    Public Sub New()
        _Quantity = 0
        _SellPrice = 0
    End Sub
    Public Property SellPrice() As Decimal
        Get
            Return _SellPrice
        End Get
        Set
            _SellPrice = value
        End Set
    End Property
    Public Property Quantity() As Int32
        Get
            Return _Quantity
        End Get
        Set
            _Quantity = value
        End Set
    End Property
End Class

我用来转换的代码是

sockClient.Send(fastJSON.JSON.ToJSON(ObjSales))

好的,这里是我的工作代码(c#而不是VB)来证明这不是来自库的bug:

namespace TestFastJson
{
    internal class Program
    {
            private static void Main()
            {
                    var sale = new Sale
                    {
                        SaleItems = new List<SaleItem>
                        {
                            new SaleItem(),
                            new SaleItem()
                        }
                    };
                    string jsonText = fastJSON.JSON.ToJSON(sale);
                    var newobj = fastJSON.JSON.ToObject<Sale>(jsonText);
                    Console.WriteLine(newobj.SaleItems.Count);
            }
    }
    public class Sale
    {
            public DateTime Date { get; set; }
            public List<SaleItem> SaleItems { get; set; }
            public int Total { get; set; }
    }
    public class SaleItem
    {
            public string ProductId { get; set; }
            public int Amount { get; set; }
            public string Quality { get; set; }
    }
}

输出
2

现在为您的代码。为什么AddSaleItem()返回一个Decimal ?SaleItems不存在,因为SaleItems被设置为private。切换到public,这里是。如果setter不可访问,lib如何设置值?

有效输出的代码:

    Friend Class Program
        Private Shared Sub Main()
            Dim sale = New Sale()
            sale.AddSaleItem(New SaleItem())
            sale.AddSaleItem(New SaleItem())
            Dim jsonText As String = fastJSON.JSON.ToJSON(sale)
            Console.WriteLine(jsonText)
            Dim newobj = fastJSON.JSON.ToObject(Of Sale)(jsonText)
            Console.WriteLine(newobj.SaleItems.Count)
        End Sub
    End Class
    Public Class SaleItem
        Private _ID As Int32
        Public Sub New()
            Me.Quantity = 0
            Me.SellPrice = 0
        End Sub
        Public Property SellPrice() As Decimal
            Get
                Return m_SellPrice
            End Get
            Set
                m_SellPrice = Value
            End Set
        End Property
        Private m_SellPrice As Decimal
        Public Property Quantity() As Integer
            Get
                Return m_Quantity
            End Get
            Set
                m_Quantity = Value
            End Set
        End Property
        Private m_Quantity As Integer
    End Class
    Public Class Sale
        Public listSaleItems As List(Of SaleItem)
        Public Sub New()
            Me.ClearSaleItem()
        End Sub
        Public ReadOnly Property SaleItems() As List(Of SaleItem)
            Get
                Return Me.listSaleItems
            End Get
        End Property
        Public Property Paid() As Decimal
            Get
                Return m_Paid
            End Get
            Set
                m_Paid = Value
            End Set
        End Property
        Private m_Paid As Decimal
        Public Sub ClearSaleItem()
            Me.listSaleItems = New List(Of SaleItem)()
        End Sub
        Public Sub AddSaleItem(value As SaleItem)
            If value Is Nothing Then
                Throw New ApplicationException("SaleItem cannot be empty")
            End If
            Me.listSaleItems.Add(value)
        End Sub
    End Class

输出
2

感谢您的贡献,碰巧dll编码器给了我解决方案

我必须将Sale的salesitems属性从只读更改为读写属性

Public Property SaleItems() As List(Of SaleItem)
            Get
                Return Me.listSaleItems
            End Get
Set(value as List(Of SaleItem))
                listSaleItems = value 
            End Get
        End Property

谢谢

相关内容

  • 没有找到相关文章

最新更新