0

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; }
}
iboros
  • 317
  • 2
  • 5
  • 15
  • I doubt it, VB 6 is really really old – Matt Aug 01 '14 at 20:09
  • Oh I know, it's ancient... reality is that there are still lot of Classic ASP front ends out there, still running COM+ apps written in VB6 for their business logic... which is how I ended up asking for help here... :) – iboros Aug 01 '14 at 20:13
  • I don't think you need (or should use) a general library for serializing objects. Your objects should be responsible for serializing themselves, i.e. by implementing for each something like a toString() method. This way, each class will have a defined behaviour and you won't spend your time tweaking your general serializing function for each new object you're adding support for. Unless of course if you need to serialize objects you're not implementing yourself, but be ready to deal with vtables (or maybe using TLI could be useful for that) – johnwait Aug 02 '14 at 00:36
  • Some corrections/addition to the previous comment: Of course, I meant toJSON() for the hypothetical function name. Also, TLI would only work for public object modules (i.e. exposed through COM) – johnwait Aug 02 '14 at 01:03
  • I don't know the VB-JSON library, but at a guess I would say that "parse" method is for deserialising not serialising. You could use COM-interop in C# to create a COM-visible library that can be used from the VB6. – MarkJ Aug 02 '14 at 15:08
  • 2
    http://www.vbforums.com/showthread.php?738845-VB6-JsonBag-Another-JSON-Parser-Generator – wqw Aug 03 '14 at 17:31
  • See [this question](http://stackoverflow.com/questions/2782076/is-there-a-json-parser-for-vb6-vba) for a list of other libraries for parsing/emitting JSON from VB6 – MarkJ Aug 04 '14 at 11:50

1 Answers1

0

In VB6 Class modules get a save event.

Persistable Property

Sets a value that determines if an object can save and restore data across instances. May only be set at design time.

Syntax

object.Persistable [= number]

The Persistable property syntax has these parts:

Part Description object An object expression that evaluates to an object in the Applies To list. number An integer that specifies persistence behavior, as described in Settings.

Settings

The settings for number are:

Constant Setting Description vbNotPersistable 0 (Default) The object can't be persisted. vbPersistable 1 The object can be persisted.

Remarks

The Persistable property is only available for classes that are public and creatable. When Persistable is set to vbPersistable, the following events are added to the class: InitProperties, ReadProperties, and WriteProperties. The PropertyChanged method is added to the class as well.


Send feedback to MSDN.Look here for MSDN Online resources.

Noodles
  • 1,981
  • 1
  • 11
  • 4