FastJson 编码器错误
FastJson encoder bug
我有一个保存销售数据的 class Sale,然后我在 Sale class 中有一个 属性,它是 SaleItem class 的列表。
Sale
-date
-SaleItems <-- List(Of SaleItem)
-total
特卖商品class
SaleItem
-productID
-amt
-qty
当我转换为 Json 字符串并调试打印输出时。 SaleItems 没有编码,只是丢失了。
我做错了什么,或者这可能是一个错误?
编辑
特卖会class
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
特卖商品Class
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)来证明这不是 lib 中的错误:
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()
returns 一个 Decimal
?
并且缺少 SaleItems
,因为 SaleItems
被设置为 private
。切换到 public
就在这里。如果 setter 不可访问,库如何设置该值?
您的代码具有有效输出:
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 coder给了我解决方案
我必须将 Sale 的 Saleitems 属性 从 Readonly 更改为 Read/Write 属性
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
谢谢
我有一个保存销售数据的 class Sale,然后我在 Sale class 中有一个 属性,它是 SaleItem class 的列表。
Sale
-date
-SaleItems <-- List(Of SaleItem)
-total
特卖商品class
SaleItem
-productID
-amt
-qty
当我转换为 Json 字符串并调试打印输出时。 SaleItems 没有编码,只是丢失了。
我做错了什么,或者这可能是一个错误?
编辑
特卖会class
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
特卖商品Class
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)来证明这不是 lib 中的错误:
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()
returns 一个 Decimal
?
并且缺少 SaleItems
,因为 SaleItems
被设置为 private
。切换到 public
就在这里。如果 setter 不可访问,库如何设置该值?
您的代码具有有效输出:
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 coder给了我解决方案
我必须将 Sale 的 Saleitems 属性 从 Readonly 更改为 Read/Write 属性
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
谢谢