JsonApiStruct Usage – Arma Reforger
Lou Montana (talk | contribs) (Page creation) |
Lou Montana (talk | contribs) m (Some wiki formatting) |
||
Line 11: | Line 11: | ||
{{Feature|informative| | {{Feature|informative| | ||
JsonApiStruct is primarily meant for automatic data "conversion" from JSON to script - but it is also possible to assemble JSON using object's API: | JsonApiStruct is primarily meant for automatic data "conversion" from JSON to script - but it is also possible to assemble JSON using object's API: | ||
# by using the variable registration system - based upon additional variable registering ''via'' the <enforce inline>RegV()</enforce> method | # by using the variable registration system - based upon additional variable registering ''via'' the <enforce inline>RegV()</enforce> (Register Variable) method | ||
# as general stream assembly - by calling function to add values, object or starting arrays | # as general stream assembly - by calling function to add values, object or starting arrays | ||
}} | }} | ||
Line 132: | Line 132: | ||
Packing is fairly easy, when object is either asked by thread/callback or invoked manually to pack its data, the <enforce inline>OnPack()</enforce> event method is called. | Packing is fairly easy, when object is either asked by thread/callback or invoked manually to pack its data, the <enforce inline>OnPack()</enforce> event method is called. | ||
{{Feature|See the {{Link|#JSON Validation}} chapter below for a simple way to check JSON structure.}} | {{Feature|informative|See the {{Link|#JSON Validation}} chapter below for a simple way to check JSON structure.}} | ||
=== Automated Packing/Expanding === | === Automated Packing/Expanding === | ||
Line 145: | Line 145: | ||
* If JSON is expanded onto given object or hierarchy and variable name && format match, variables are filled with appropriate data | * If JSON is expanded onto given object or hierarchy and variable name && format match, variables are filled with appropriate data | ||
* If JSON is assembled from given object or hierarchy, variable content is used to create JSON structures | * If JSON is assembled from given object or hierarchy, variable content is used to create JSON structures | ||
* float, int, boolean, string, arrays, object and arrays of objects are supported | |||
* float, int, boolean, string | |||
* automatic allocation of object | * automatic allocation of object | ||
* getting JSON as string | * getting JSON as string | ||
| | | | ||
* Even JSON format does support it - we cannot deal with multi-type arrays, like combined strings and integers | * Even though JSON format does support it - we cannot deal with multi-type arrays, like combined strings and integers (Enforce script does not support arrays of various types) | ||
* Declaration of variables or objects - if there is no adequate variable present on during expand, it is ignored - if an object type is missing, it cannot be expanded | * Declaration of variables or objects - if there is no adequate variable present on during expand, it is ignored - if an object type is missing, it cannot be expanded | ||
|} | |} | ||
Line 304: | Line 302: | ||
! Description | ! Description | ||
|- | |- | ||
| ETJSON_UNKNOWN | | {{hl|ETJSON_UNKNOWN}} | ||
| General error - not implemented | | General error - not implemented | ||
|- | |- | ||
| ETJSON_OK | | {{hl|ETJSON_OK}} | ||
| This is generated upon sucessfull processing at onSuccess() event | | This is generated upon sucessfull processing at onSuccess() event | ||
|- | |- | ||
| ETJSON_COMMSEND | | {{hl|ETJSON_COMMSEND}} | ||
| Sending of object failed (callback got error code) | | Sending of object failed (callback got error code) | ||
|- | |- | ||
| ETJSON_COMMRECV | | {{hl|ETJSON_COMMRECV}} | ||
| Receiving of object failed (callback got error code) | | Receiving of object failed (callback got error code) | ||
|- | |- | ||
| ETJSON_PARSERERROR | | {{hl|ETJSON_PARSERERROR}} | ||
| Parsing process failed - invalid data or corrupted format? | | Parsing process failed - invalid data or corrupted format? | ||
|- | |- | ||
| ETJSON_PACKNOSTART | | {{hl|ETJSON_PACKNOSTART}} | ||
| Packing process cannot start - invalid state or other problems | | Packing process cannot start - invalid state or other problems | ||
|- | |- | ||
| ETJSON_TIMEOUT | | {{hl|ETJSON_TIMEOUT}} | ||
| Failed to send data due to timeout (when JsonStruct data is sent via RestApi or BackendApi | | Failed to send data due to timeout (when JsonStruct data is sent via RestApi or BackendApi | ||
|- | |- | ||
| ETJSON_NOBUFFERS | | {{hl|ETJSON_NOBUFFERS}} | ||
| Too many objects processed at once! | | Too many objects processed at once! | ||
|- | |- | ||
| ETJSON_FAILFILELOAD | | {{hl|ETJSON_FAILFILELOAD}} | ||
| Could not load file with JSON data | | Could not load file with JSON data | ||
|- | |- | ||
| ETJSON_FAILFILESAVE | | {{hl|ETJSON_FAILFILESAVE}} | ||
| Could not save file with JSON data | | Could not save file with JSON data | ||
|} | |} | ||
Line 360: | Line 358: | ||
string data2 = dummy.AsString(); // get packed JSON #2 | string data2 = dummy.AsString(); // get packed JSON #2 | ||
// now | // now both can be visualised | ||
</enforce> | </enforce> | ||
Line 395: | Line 393: | ||
} | } | ||
} | } | ||
</syntaxhighlight></spoiler> | </syntaxhighlight> | ||
</spoiler> | |||
}} | }} | ||
{{GameCategory|armaR|Modding|Tutorials|Scripting}} | {{GameCategory|armaR|Modding|Tutorials|Scripting}} |
Revision as of 16:36, 12 January 2023
JsonApiStruct is scripted object with support to:
- Encode script object/ variable to JSON format
- Decode data from JSON format onto script object/ variable
- Import/export from/to file
- Import/export from/to string
It can also be used as callback object when handling responses from Backend API (script) where incoming data are automatically expanded.
How To
- Create your own class by inheriting JsonApiStruct
- Register all variables you require in constructor of object
- Create hierarchy from objects if you need more complex structures
Simple Declaration
Hierarchy Declaration
File Operations
You can store you object into file (ie. invoke Pack + Save to file) or you can just save already packed JSON (Save to File).
File Import
File Export
Packing
Packing is fairly easy, when object is either asked by thread/callback or invoked manually to pack its data, the OnPack() event method is called.
Automated Packing/Expanding
To make things easier, it is possible to work with a JSON object just by registering variables and object into JsonApiStruct.
What Works | What Doesn't |
---|---|
|
|
Variables
Objects and Variables
If the OnPack() event method is declared upon children objects, it is called upon them hierarchically as well.
Error Handling
If you use object as a callback and an error happen during JSON processing, it generates events upon that very object:
Error Codes
As defined in EJsonApiError:
Code Enum | Description |
---|---|
ETJSON_UNKNOWN | General error - not implemented |
ETJSON_OK | This is generated upon sucessfull processing at onSuccess() event |
ETJSON_COMMSEND | Sending of object failed (callback got error code) |
ETJSON_COMMRECV | Receiving of object failed (callback got error code) |
ETJSON_PARSERERROR | Parsing process failed - invalid data or corrupted format? |
ETJSON_PACKNOSTART | Packing process cannot start - invalid state or other problems |
ETJSON_TIMEOUT | Failed to send data due to timeout (when JsonStruct data is sent via RestApi or BackendApi |
ETJSON_NOBUFFERS | Too many objects processed at once! |
ETJSON_FAILFILELOAD | Could not load file with JSON data |
ETJSON_FAILFILESAVE | Could not save file with JSON data |
JSON Validation
JSON validation allows to test the input/output values.
Result structure can be used as input as well and it should match in the end, similar process can be used for verifying already existing structures.
Packing object will produce a long, non-formatted string like:
{"m_bool":false,"m_int":1024,"m_double":1.2345678806304932,"m_string":"Hello I am your new string!","m_objects":{"obj1":{"name":"Van Ceulen","year":"1706","val":3.1415927410125734},"obj2":{"name":"Bernoulli","year":"1683","val":0.5772156715393066},"obj3":{"name":"Feidius","year":"430BC","val":1.6180340051651}}}
Use suitable online/other tool for readable structure, result can be stored to textfile and compared by binary/text compare tool if neccessary.