I am trying to format Quote items data into a JSON Object to later save to MySQL. the following code produces JSONObject, which is returning an empty JSON object, like this: [{}], even though Quote_Items_Info_Data IS NOT empty. No errors are being triggered! Why on earth is JsonConvert is not serializing?
Friend Class JSON_Quote_Item
Friend Property ItemNumber As String
Friend Property ItemDescription As String
Friend Property ItemQty As String
Friend Property ItemPrice As String
Public Sub New()
End Sub
Public Sub New(ByVal _ItemNumber As String, ByVal _ItemDescription As String, ByVal _itemQty As String, ByVal _itemPrice As String)
ItemNumber = _ItemNumber.Trim.Replace(vbCrLf, "\r\n").Replace(Chr(34), "\" & Chr(34)).Replace("'", "\'")
ItemDescription = _ItemDescription.Trim.Replace(vbCrLf, "\r\n").Replace(Chr(34), "\" & Chr(34)).Replace("'", "\'")
ItemQty = _itemQty.Trim
ItemPrice = _itemPrice.Trim
End Sub
End Class
'
'
''' <summary>
''' Convert all Quote items to a JSON object on the form and save it to the server DB.
''' senderFRM is the Quote Creator/Editor. Following an example from: https://www.newtonsoft.com/json/help/html/SerializingCollections.htm
''' and https://www.newtonsoft.com/json
''' </summary>
''' <param name="senderFRM"></param>
''' <returns></returns>
Friend Function SaveAllQuoteItems(senderFRM As frmQuotesCreatorEditor) As Boolean
Try
'make sure I will not run into an exception!
If senderFRM IsNot Nothing AndAlso String.IsNullOrEmpty(senderFRM.txtQuoteNumber.Text.Trim) = False Then
Dim QuoteNumber As String = senderFRM.txtQuoteNumber.Text.Trim
'Holds a list of the custom class JSON_Quote_Item as T and contain all the form quote items data.
Dim Quote_Items_Info_Data As List(Of JSON_Quote_Item) = New List(Of JSON_Quote_Item)
'
'
'Go thru all FlowLayout controls in the form with the finality of finding the Quote items data and put this into a JSON (serialized) string.
For Each myPanel As Panel In senderFRM.flpQuoteItems.Controls.OfType(Of Panel)
Dim myElementHost As ElementHost = myPanel.Controls.OfType(Of ElementHost).FirstOrDefault
Dim MyUserControl_QuoteItem As UserControl_QuoteItem = TryCast(myElementHost.Child, UserControl_QuoteItem)
'
Dim myQuoteItemNumber As String = MyUserControl_QuoteItem.txtItemNumber.Text
Dim myQuoteItemDescription As String = MyUserControl_QuoteItem.txtItemDescription.Text
Dim myQuoteItemQty As String = MyUserControl_QuoteItem.txtItemQty.Text
Dim MyQuoteItemPrice As String = MyUserControl_QuoteItem.txtItemListedCustomerPrice.Text
'
'Perhaps not necessary but here to check that all parameters contain data.
If String.IsNullOrEmpty(myQuoteItemNumber) = False And String.IsNullOrEmpty(myQuoteItemDescription) = False And
String.IsNullOrEmpty(myQuoteItemQty) = False And String.IsNullOrEmpty(MyQuoteItemPrice) = False Then
'add the data to the list of UserControl_QuoteItem
Quote_Items_Info_Data.Add(New JSON_Quote_Item(myQuoteItemNumber, myQuoteItemDescription, MyQuoteItemPrice, myQuoteItemQty))
End If
Next
'
Dim JSONObject As Object = DBNull.Value
If Quote_Items_Info_Data IsNot Nothing AndAlso Quote_Items_Info_Data.Count > 0 Then
'
'NuGet - Newtonsoft.Json.JsonConvert
'Return as string, but will keep as an object to be able to use it on the parameterization of the SQL query later!
JSONObject = JsonConvert.SerializeObject(Quote_Items_Info_Data)
End If
'
Console.WriteLine(JSONObject.ToString)
'JSONObject is returning an empty JSON Object returning this: [{}] , even though Quote_Items_Info_Data was not empty.
'Why on earth is JsonConvert is not serializing?
'
'Code to save to the quote column in the database code goes here if the above ever Works!
End If
Return True
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return False
End Function
CodePudding user response:
Found the solution! The problem lies is that the new short form of properties (a one-liner). Changed:
Friend Property ItemNumber As String
Friend Property ItemDescription As String
Friend Property ItemQty As String
Friend Property ItemPrice As String
To this:
Private itemNumber As String
Public Property _itemNumber() As String
Get
Return itemNumber
End Get
Set(ByVal value As String)
itemNumber = value
End Set
End Property
'
Private ItemDescription As String
Public Property _ItemDescription() As String
Get
Return ItemDescription
End Get
Set(ByVal value As String)
ItemDescription = value
End Set
End Property
Private ItemQty As String
Public Property _ItemQty() As String
Get
Return ItemQty
End Get
Set(ByVal value As String)
ItemQty = value
End Set
End Property
Private ItemPrice As String
Public Property _ItemPrice() As String
Get
Return ItemPrice
End Get
Set(ByVal value As String)
ItemPrice = value
End Set
End Property
And it all worked!! No getter in the short form?
CodePudding user response:
The problem was indeed the Declaration Access Level being FRIEND.So: this:
Public Property ItemNumber As String
Public Property ItemDescription As String
Public Property ItemQty As String
Public Property ItemPrice As String
Do work Too and is much cleaner! Thanks, @Jimi, sometimes one gets blinded!