|
3 | 3 |
|
4 | 4 | This project is just an attempt to implement a feature to serialize objects (persistent or just registered) with **two main goals in mind** : |
5 | 5 |
|
6 | | -1. Avoid the need of changing the class definition if there are changes in naming of serialized properties or even in the criteria to serialize the object |
| 6 | +1. Avoid the need of changing the class definition if there are changes in the names of serialized properties or even in the criteria to serialize the object |
7 | 7 | 2. Being able to serialize/deserialize to/from multiple formats (JSON, CSV, XML, custom,…), also _without touching the class definition itself_. |
8 | 8 |
|
9 | 9 | ### How to get universal serialization feature in your classes |
@@ -115,6 +115,7 @@ We can have as much mapping definitions for a class as we need. An easy way to s |
115 | 115 | do objJSON.%ToJSON() |
116 | 116 |
|
117 | 117 | ``` |
| 118 | +--- |
118 | 119 |
|
119 | 120 | We also have the possibility of changing a bit the way in which default map `MAP0` is generated : |
120 | 121 | * Change the name of default map. |
@@ -144,9 +145,64 @@ This have benefits over performance but comes at the price of having to use a di |
144 | 145 |
|
145 | 146 | Anyway, the primary class is not affected and doesn't have to be changed no matter how many templates define to handle the serialization of its objects. |
146 | 147 |
|
| 148 | +Using these classes is very easy. Let's see an example: |
147 | 149 |
|
148 | | - |
| 150 | +--- |
149 | 151 | Example : |
150 | 152 |
|
151 | | -<<< define and use different template classes, for different maps in JSON and also for different serialization (CSV) |
152 | | ->>> |
| 153 | +We want to be able to export SampleApps.Serialize.PersistObject to JSON, but just some of the properties: `cod`, `description` and `start`. We want to project those properties, for example, in Spanish, as: `codigo`, `descripcion` and `inicio` respectively. |
| 154 | + |
| 155 | +We design the required map, that we call MAP2 and load it: |
| 156 | + |
| 157 | +```javascript |
| 158 | +do ##class(SampleApps.Serialize.HandleMaps).SetPersistObjectMAP2() |
| 159 | +``` |
| 160 | + |
| 161 | +Then, we create a new class that we can call `SampleApps.Serialize.PersistObject.generatedMAP2` that extends `OPNLib.Serialize.Template` and change the required parameters. This would be the class definition: |
| 162 | +```javascript |
| 163 | +Class SampleApps.Serialize.PersistObject.generatedMAP2 Extends OPNLib.Serialize.Template |
| 164 | +{ |
| 165 | +Parameter EXPTASSOCIATEDCLASS = "SampleApps.Serialize.PersistObject"; |
| 166 | +Parameter EXPTMAP = "MAP2"; |
| 167 | +} |
| 168 | +``` |
| 169 | +We have just set the associated class and the MAP to apply. Then, we will have 2 methods auto-generated: `Export` and `Import` with the code required to export/import from/to JSON format objects of type `PersistObject` following the `MAP2` designed. |
| 170 | +The disadvantage with this approach is that we will have to recompile each time the mapping changes. The advantage is that it will be a bit quicker than the standard approach as all the property sets are static as have been resolved at compile time. |
| 171 | + |
| 172 | +As you can see, the class that stores the `PersistObject` objects, is not aware of these export/import classes and methods and it doesn't require any modification. |
| 173 | + |
| 174 | +This way, once our template class is compiled, we could do: |
| 175 | + |
| 176 | +```javascript |
| 177 | + set mObject = ##class(SampleApps.Serialize.PersistObject).%OpenId(1) |
| 178 | + set json = mObject.Export("SampleApps.Serialize.PersistObject.generatedMAP1","Export") |
| 179 | + do json.%ToJSON() |
| 180 | + { |
| 181 | + "codigo":372732612, |
| 182 | + "descripcion":"Q8845", |
| 183 | + "inicio":"1953-03-04", |
| 184 | + "añofinal":133788319, |
| 185 | + "colores":"B9211\tC8958\tY489\tC5123", |
| 186 | + "MapTesting": |
| 187 | + { |
| 188 | + "codigo":462711925, |
| 189 | + "fecha":"1932-03-30", |
| 190 | + "descripcion":"F1357", |
| 191 | + "numeroDecimal":5303.31, |
| 192 | + "fechaHora":"1956-07-25 04:52:33", |
| 193 | + "hora":"17:47:32", |
| 194 | + "lista":"J9796\tZ2412\tN4278" |
| 195 | + } |
| 196 | + } |
| 197 | +``` |
| 198 | +--- |
| 199 | + |
| 200 | +## Sample Application |
| 201 | +There are some classes that I used to build some of the examples. There are others testing other features. You can take a look at them in the package `SampleApps.Serialize` |
| 202 | + |
| 203 | +## End |
| 204 | + |
| 205 | +I hope this code can help you in any way. |
| 206 | + |
| 207 | +Enjoy! |
| 208 | +_Jose-Tomas Salvador_ |
0 commit comments