Dynamic change of object properties and VB series. NET



  • Background:

    There is a program that I've been developing and using everyday for several years, it's written under .NEW framework 3.5, and to move on to another more new version as long as there is no possibility. There's a bunch of lines in it that are stored in a single configuration, collected through SOAP, a XML XML X-M. In turn, masses, rows, handbooks, hexes and JSON converted objects are recorded. Because the programme is constantly being refined, there is a time when an object design, new properties and, after being added to the programme, project files stop converting at reading because they do not see new features in the disk-held configuration.

    There's thoughts about the decision, a little demo.

    Imports System.IO
    Imports System.Runtime.Serialization.Json
    Imports System.Text
    

    Module MD

    Private storeObject As DataConfig
    Private loadObject As DataConfig
    
    Private tmpJson As String
    
    Sub Main()
    
        '   Создаем тестовый объект
        storeObject = New DataConfig
    
        '   Присвоим пару значений
        With storeObject
            .PropertyOne = "значение 1"
            .PropertyTwo = "значение 2"
        End With
    
        '   Конвертнем в Json
        tmpJson = ToJSON(storeObject)
    
        '   Пытаемся восстановить объект
        loadObject = ToDataConfig(tmpJson)
    
    
    End Sub
    
    Function ToJSON(ByRef obj As DataConfig) As String
    
        Dim stream1 As New MemoryStream()
        Dim ser As New DataContractJsonSerializer(GetType(DataConfig))
        ser.WriteObject(stream1, obj)
        stream1.Position = 0
        Dim sr As New StreamReader(stream1)
        Dim jsonOut As String = sr.ReadToEnd
    
        Return jsonOut
    
    End Function
    
    Function ToDataConfig(ByVal json As String) As DataConfig
    
        Dim js As New DataContractJsonSerializer(GetType(DataConfig))
        Dim ms As New IO.MemoryStream(UTF8Encoding.UTF8.GetBytes(json))
    
        Return js.ReadObject(ms)
    
    End Function
    

    End Module

    Class code DataConfig

    Public Class DataConfig

    Private _property_one As String
    Public Property PropertyOne() As String
        Get
            Return _property_one
        End Get
        Set(ByVal value As String)
            _property_one = value
        End Set
    End Property
    
    
    Private _property_two As String
    Public Property PropertyTwo() As String
        Get
            Return _property_two
        End Get
        Set(ByVal value As String)
            _property_two = value
        End Set
    End Property
    

    End Class

    Question:

    Heard what's in the NET, dynamic objects, and the ability to dynamize the object during the programme.

    1. How to finish the class DataConfig for this
      use?
    2. As an example, between an envelope in json and
      Reinstatement?

    введите сюда описание изображения

    update

    Used the library Newtonsoft.Json When attempting to dissert errors, the object also fails to complete the properties it has invested, only fills the properties of the upper level.
    Tested JSON:

    {
    "ExProperty": {
    "_disabled": false,
    "_el_class": null,
    "_enum_values": [],
    "_id": "124",
    "_inline_script": null,
    "_inline_style": null,
    "_innerHTML": null,
    "_input_max": 0,
    "_input_min": 0,
    "_input_min_zero": false,
    "_input_subtype": null,
    "_nolabel": true,
    "_onclick": null,
    "_order": 0,
    "_parent_wrapp_class": "rrrrrrr",
    "_placeholder": null,
    "_value": null,
    "_width": 0,
    "_wrapp_class": null,
    "_wrapp_col_md": 55,
    "_wrapp_label_class": "eeee",
    "_dt_append": null,
    "_dt_append_name": null,
    "_dt_name": null,
    "_fieldSet": [
    "qqqqqqqqqqqqq",
    "aaaaaaaaaaa",
    "ssssssssssssss",
    "dddddddddddddd",
    "fgfffffffffffffffffff"
    ],
    "_fieldset_name": "aaaaaaaaaaa",
    "_isfiltr": false,
    "_lbl": null,
    "_lbl_short": null,
    "_lkp_filter": null,
    "_lkp_idas": null,
    "_lkp_nameas": null,
    "_lkp_table": null,
    "_req": false,
    "_sh_in_add": false,
    "_sh_in_edit": false,
    "_sh_in_list": false,
    "_type": null
    },
    "PropertyOne": "значение 1",
    "PropertyThree": null,
    "PropertyTwo": "значение 2"
    }

    The code that's trying to do dessertization.

    tmpJson = File.ReadAllText("test.json")
    loadObject = JsonConvert.DeserializeObject(Of DataConfig)(tmpJson)

    The reverse tests revealed that the formats did not match, i.e. by converging the object by a method

    tmpJson = File.ReadAllText("test.json")
    loadObject = ToDataConfig(tmpJson)
    tmpJson = JsonConvert.SerializeObject(loadObject)

    We'll get a similar JSON on the way out differently.

    {
    "PropertyOne": "значение 1",
    "PropertyTwo": "значение 2",
    "PropertyThree": null,
    "ExProperty": {
    "fieldSets": [
    "qqqqqqqqqqqqq",
    "aaaaaaaaaaa",
    "ssssssssssssss",
    "dddddddddddddd",
    "fgfffffffffffffffffff"
    ],
    "fieldset_name": "aaaaaaaaaaa",
    "lbl": null,
    "lbl_short": null,
    "type": 0,
    "req": false,
    "lkp_table": null,
    "lkp_nameas": null,
    "lkp_idas": null,
    "lkp_filter": null,
    "dt_name": null,
    "dt_append_name": null,
    "dt_append": null,
    "sh_in_edit": false,
    "sh_in_add": false,
    "sh_in_list": false,
    "isfiltr": false,
    "order": 0,
    "wrapp_col_md": 55,
    "parent_wrapp_class": "rrrrrrr",
    "wrapp_class": null,
    "wrapp_label_class": "eeee",
    "el_class": null,
    "value": null,
    "innerHTML": null,
    "placeholder": null,
    "nolabel": true,
    "width": 0,
    "onClick": null,
    "inlineStyle": null,
    "Disabled": false,
    "Id": "124",
    "inlineScript": null,
    "inputSubtype": null,
    "InputMinZero": false,
    "inputMin": 0,
    "inputMax": 0,
    "EnumValues": []
    }
    }

    The task is almost complete, but needs to be refined.

    update

    Added. https://yadi.sk/d/J3BP42ivm9LEa Demonstration of the facility ExProperty



  • There is a time when an object is to be added to a JSON configuration, new properties and, when added to the programme, project files stop converting at reading

    For deserization used DataContractJsonSerializer(GetType(DataConfig))
    If the names change in JSON, there will be no deserization.
    That is, it is necessary to amend DataConfig.

    But it's easier to use, for example, json.net. https://www.nuget.org/packages/Newtonsoft.Json/7.0.1 )
    Example of use in vb.net - https://stackoverflow.com/questions/21676708/simple-working-example-of-json-net-in-vb-net ♪
    In json.net, there's support for requests similar to xpath. That is, even if the JSON structure is changing, it is possible to find the necessary objects by name or location. Example of use of SelectToken - http://www.newtonsoft.com/json/help/html/SelectToken.htm ♪

    UPDATE:

    How do you download the bag from nuget.org and remove the assembly from it?
    Response https://ru.stackoverflow.com/a/475908/196972

    UPDATE:

    Newtonsoft.Json has the opportunity to work with private properties/fields.
    Example https://stackoverflow.com/a/24107081/5673266 ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2