skip to Main Content

I can serialize the primary tree of the JSON (frm); however, I don’t know how to assign values two the second tree titled controls.

To assign a value to the primary tree, I can do this:

Dim form As New frm()

form.form_name = "FORM"

I was expecting to be able to assign controls something like this:

form.controls.parent = "something"

Am I going about the assignment the wrong way or is my class not setup correctly? Below I’m going to show my code to serialize the information, then what the jSON file looks like, and then my classes for the JSON file. Any help is appreciated.

CODE:

  Dim form As New frm()

  form.form_name = "name"
  form.form_text = "text"
  form.form_icon = "icon"
  form.form_default_button = "default"
  form.form_cancel_button = "cancel"
  form.form_width = "width"
  form.form_height = "height"

  Dim JSON As String = JsonConvert.SerializeObject(form)

JSON Example:

{
      "form_name":"FORM",
      "form_text":"Setup Buyers PO Issue Limit",
      "form_icon":"c:test.ico",
      "form_default_button":"BTNSAVE",
      "form_cancel_button":"BTNCLOSE",
      "form_width":"300",
      "form_height":"800",
      "controls":[
         {
            "parent":"FORM",
            "type":"LABEL",
            "name":"LBLUser",
            "text":"Buyers Operator ID:",
            "align":"LEFT",
            "enabled":"TRUE",
            "mandatory":"",
            "validator":"",
            "populate":""
         },
         {
            "parent":"FORM",
            "type":"COMBO",
            "name":"CBOOperator",
            "text":"",
            "align":"LEFT",
            "tooltip":"",
            "enabled":"TRUE",
            "mandatory":"TRUE",
            "validator":"",
            "populate":"SUB"
         }
      ]
   }

CLASS CODE:

Public Class controls
    Public Property parent() As String
        Get
            Return m_parent
        End Get
        Set
            m_parent = Value
        End Set
    End Property
    Private m_parent As String

    Public Property type() As String
        Get
            Return m_type
        End Get
        Set
            m_type = Value
        End Set
    End Property
    Private m_type As String
    Public Property name() As String
        Get
            Return m_name
        End Get
        Set
            m_name = Value
        End Set
    End Property
    Private m_name As String
    Public Property text() As String
        Get
            Return m_text
        End Get
        Set
            m_text = Value
        End Set
    End Property
    Private m_text As String
    Public Property align() As String
        Get
            Return m_align
        End Get
        Set
            m_align = Value
        End Set
    End Property
    Private m_align As String

    Public Property enabled() As Boolean
        Get
            Return m_enabled
        End Get
        Set
            m_enabled = Value
        End Set
    End Property
    Private m_enabled As Boolean
    Public Property mandatory() As Boolean
        Get
            Return m_mandatory
        End Get
        Set
            m_mandatory = Value
        End Set
    End Property
    Private m_mandatory As Boolean
    Public Property validator() As String
        Get
            Return m_validator
        End Get
        Set
            m_validator = Value
        End Set
    End Property
    Private m_validator As String
    Public Property populate() As String
        Get
            Return m_populate
        End Get
        Set
            m_populate = Value
        End Set
    End Property
    Private m_populate As String
    Public Property tooltip() As String
        Get
            Return m_tooltip
        End Get
        Set
            m_tooltip = Value
        End Set
    End Property
    Private m_tooltip As String
End Class
Public Class frm
    Public Property form_name() As String
        Get
            Return m_form_name
        End Get
        Set
            m_form_name = Value
        End Set
    End Property
    Private m_form_name As String

    Public Property form_text() As String
        Get
            Return m_form_text
        End Get
        Set
            m_form_text = Value
        End Set
    End Property
    Private m_form_text As String

    Public Property form_icon() As String
        Get
            Return m_form_icon
        End Get
        Set
            m_form_icon = Value
        End Set
    End Property
    Private m_form_icon As String
    Public Property form_default_button() As String
        Get
            Return m_form_default_button
        End Get
        Set
            m_form_default_button = Value
        End Set
    End Property
    Private m_form_default_button As String
    Public Property form_cancel_button() As String
        Get
            Return m_form_cancel_button
        End Get
        Set
            m_form_cancel_button = Value
        End Set
    End Property
    Private m_form_cancel_button As String
    Public Property form_width() As String
        Get
            Return m_form_width
        End Get
        Set
            m_form_width = Value
        End Set
    End Property
    Private m_form_width As String
    Public Property form_height() As String
        Get
            Return m_form_height
        End Get
        Set
            m_form_height = Value
        End Set
    End Property
    Private m_form_height As String
    Public Property controls() As Control()
        Get
            Return m_controls
        End Get
        Set
            m_controls = Value
        End Set
    End Property
    Private m_controls As Control()


End Class

2

Answers


  1. Chosen as BEST ANSWER

    The "POCO" method SSS mentioned along with utilizing the native JSON namespace did the trick for me.


  2. The simplest way to serialize an object is first to make your classes into "POCO"s such as:

    Public Class DemoClass
      Public Property Property1 As String
      Public Property Property2 As Decimal
      Public Property Property3 As Integer
      Public Property Property4 As Boolean
      Public Property Children As List(Of DemoClassChild)
    End Class
    
    Public Class DemoClassChild
      Public Property Property5 As String
      Public Property Property6 As Decimal
    End Class
    

    Then import the Json namespace:

    Imports System.Text.Json
    

    Finally use JsonSerializer to serialize:

    Dim c1 As New DemoClass With {.Property1 = "Property1Value", .Property2 = 12.34D, .Property3 = 5, .Property4 = True}
    c1.Children = New List(Of DemoClassChild)
    c1.Children.Add(New DemoClassChild With {.Property5 = "Child1", .Property6 = 1})
    c1.Children.Add(New DemoClassChild With {.Property5 = "Child2", .Property6 = 2})
    Dim s = JsonSerializer.Serialize(c1)
    System.IO.File.WriteAllText("C:JunkJunk.json", s)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search