I ran into a problem, while working on a VB6 project, when a need to serialize an Object into JSON object (or JSON formatted string) before sending it via HTTP Request (POST Method) to another web application's (MVC.NET) method that expects a complex object as a parameter. I am trying to take advantage of MVC.Net 3+ and the ability to de-serialize the JSON objects into my class (Almighty) object. At the moment I can go about using VB-JSON library that can convert properly formatted string into JSON object, however I would like to serialize an object into JSON object, or first into a properly formatted string and then into JSON object (by using VB-JSON).
VB6 Almighty Class Module is an example of an object I would like to serialize (although I need to be able to serialize pretty much any Class Object).
Is there a library (or a workaround) to do accomplish this in VB6?
Option Explicit
Private mName As String
Private mPhone As String
Private mAge As Integer
Public Property Get Name() As String
Name = mName
End Property
Public Property Let Name(ByVal vNewValue As String)
mName = vNewValue
End Property
Public Property Get Phone() As String
Phone = mPhone
End Property
Public Property Let Phone(ByVal vNewValue As String)
mPhone = vNewValue
End Property
Public Property Get Age() As Integer
Age = mAge
End Property
Public Property Let Age(ByVal vNewValue As Integer)
mAge = vNewValue
End Property
Here's a bit of that I got for now in the VB6 project for creating a JSON object before sending it to MVC.Net application. Problem is that JSON.parse() from VB-JSON only accepts string and not an object. Any ideas, how I would be able to do something like this:
Dim a As cAlmighty
Set a = New cAlmighty
a.Name = "Igor"
a.Age = 18
a.Phone = "123-123-1234"
Dim param As Object
Set param = JSON.parse(a) '<--- THIS IS THE PROBLEM, VB-JSON''s parse method accepts only string which means I would need to find a way to serialize the object into JSON which obviously preferred or actually build a string simply like this: { ""Name"": """ & a.Name & """, ""Age"": " & a.Age & " }" which is primitive way of serialization (no validation, etc.)
After I have param as JSON object, I would send it to my MVC.net controller via HTTP Post Request
Supporting Information
MVC.Net Controller Method
/// <summary>
/// 1. Takes an Almighty object as a parameter from an old VB6 (COM+) application
/// 2. Do stuff with Almighty
/// 3. Return an XML formatted string back to VB6 application.
/// </summary>
[HttpPost]
public string CalledByVB6App(Almighty param) {
if (param != null) return "<Success>True</Success>";
return "<Success>False</Success>";
}
C# Almighty Object Class
public class Almighty
{
public string Name { get; set; }
public string Phone { get; set; }
public int Age { get; set; }
}