Sep 18, 2020 Ola M sitting here and thinking if I can work with a "type" object as a "map" object and vise versa and or even "type to json". For example, I can define my stuff as a type variable, serialize it as JSON, save to a db and then get it back and JSON and then convert to Type, update it and save it back etc etc. This is possible because of BANano.FromJSON and BANano.ToJSON 1. Define a type object. Code: Copy Type Order(ProductKey As String, Amount As Double, Comment As String) 2. Initialize an instance and convert to JSON i.e. Serialize Code: Copy Dim myorder As Order myorder.Initialize myorder.Amount = 10.00 myorder.Comment = "This is a test..." myorder.ProductKey = "123456" ' Dim toJSON As String = BANano.ToJson(myorder) Output (NB: note the _ prefix) You can save this serialized object to a db or something. 3. Convert the JSON back to the type i.e. Deserialize Code: Copy Dim fromJSON As Order = BANano.FromJson(toJSON) Log("JSON to Type") Log(fromJSON.Amount) Log(fromJSON.Comment) Log(fromJSON.ProductKey) Log(fromJSON) Q. What if you want to use the type as a MAP object? Convert the JSON string to a map and update the amount (Note the prefix _) Code: Copy 'comvert the serialized JSON to Map Dim orderMap As Map = BANano.FromJson(toJSON) Log("Type to Map") Log(orderMap) orderMap.put("_amount", 1000.00) Convert the MAP back to the Type Code: Copy 'convert a map to a type Dim m2j As String = BANano.ToJson(orderMap) Dim ut As Order = BANano.FromJson(m2j) Log("Map to Type") Log(ut) Log(ut.Amount) The Type to Map functionality is not necessarily needed as you can just update, serialize and de-serialize the type object using ToJSON and FromJSON, but has been added for brevity. Ta! Attachments SerializeObject.zip SerializeObject.zip 2.1 KB · Views: 737 Last edited: Sep 18, 2020 Click to expand...
Ola M sitting here and thinking if I can work with a "type" object as a "map" object and vise versa and or even "type to json". For example, I can define my stuff as a type variable, serialize it as JSON, save to a db and then get it back and JSON and then convert to Type, update it and save it back etc etc. This is possible because of BANano.FromJSON and BANano.ToJSON 1. Define a type object. Code: Copy Type Order(ProductKey As String, Amount As Double, Comment As String) 2. Initialize an instance and convert to JSON i.e. Serialize Code: Copy Dim myorder As Order myorder.Initialize myorder.Amount = 10.00 myorder.Comment = "This is a test..." myorder.ProductKey = "123456" ' Dim toJSON As String = BANano.ToJson(myorder) Output (NB: note the _ prefix) You can save this serialized object to a db or something. 3. Convert the JSON back to the type i.e. Deserialize Code: Copy Dim fromJSON As Order = BANano.FromJson(toJSON) Log("JSON to Type") Log(fromJSON.Amount) Log(fromJSON.Comment) Log(fromJSON.ProductKey) Log(fromJSON) Q. What if you want to use the type as a MAP object? Convert the JSON string to a map and update the amount (Note the prefix _) Code: Copy 'comvert the serialized JSON to Map Dim orderMap As Map = BANano.FromJson(toJSON) Log("Type to Map") Log(orderMap) orderMap.put("_amount", 1000.00) Convert the MAP back to the Type Code: Copy 'convert a map to a type Dim m2j As String = BANano.ToJson(orderMap) Dim ut As Order = BANano.FromJson(m2j) Log("Map to Type") Log(ut) Log(ut.Amount) The Type to Map functionality is not necessarily needed as you can just update, serialize and de-serialize the type object using ToJSON and FromJSON, but has been added for brevity. Ta!