pml-json provides functionality to encode PML objects to JSON strings and decode JSON strings to PML objects.
To install this macro, follow these steps:
-
Place
json.pmlobj
in a directory specified by thePMLLIB
environment variable. -
Open an AVEVA product.
-
Enter the following command in the Command Window:
pml rehash all
String method returns the JSON string of the specified object.
-
Example:
!json = object JSON() !result = !json.String( N WRT /* ) $P$!result
-
Result in the console:
{"east":0,"north":1,"origin":"/*","up":0}
Lines method returns an array of the indented JSON strings from the specified object.
-
Example:
!file = object FILE('%temp%\sample.json') !json = object JSON() !obj = object VOLUME('E 100mm N 200mm U 300mm WRT /* TO E 400mm N 500mm U 600mm WRT /*') !file.WriteFile('OVERWRITE', !json.Lines(!obj))
-
Result in
%temp%\sample.json
:{ "from": { "east": 100, "north": 200, "origin": "/*", "up": 300 }, "to": { "east": 400, "north": 500, "origin": "/*", "up": 600 } }
Lines method returns an array of the indented JSON strings from the specified object with the specified indent string.
-
Example:
!file = object FILE('%temp%\sample.json') !json = object JSON() !obj = object VOLUME('E 100mm N 200mm U 300mm WRT /* TO E 400mm N 500mm U 600mm WRT /*') !file.WriteFile('OVERWRITE', !json.Lines(!obj, ' '))
-
Result in
%temp%\sample.json
:{ "from": { "east": 100, "north": 200, "origin": "/*", "up": 300 }, "to": { "east": 400, "north": 500, "origin": "/*", "up": 600 } }
Parse method parses a JSON string and stores the result in the specified object.
-
Example:
!json = object JSON() !obj = object POSITION() !json.Parse('{"east":100,"north":200,"up":300,"origin":"/*"}', !obj) q var !obj
-
Result in the console:
<POSITION> E 100mm N 200mm U 300mm WRT /* EAST <REAL> 100 NORTH <REAL> 200 ORIGIN <DBREF> =****/**** (Refno depends on DB) UP <REAL> 300
Parse method parses an array of JSON strings and stores the result in the specified object.
-
Example:
!json = object JSON() !file = object FILE('%temp%\sample.json') !obj = object DICTIONARY() !json.Parse(!file.ReadFile(), !obj) q var !obj.keys !obj.values q var !obj.values[1].keys !obj.values[1].values q var !obj.values[2].keys !obj.values[2].values
-
Contents of
%temp%\sample.json
:{ "from": { "east": 100, "north": 200, "up": 300, "origin": "/*" }, "to": { "east": 400, "north": 500, "up": 600, "origin": "/*" } }
-
Result in the console:
<ARRAY> [1] <STRING> 'from' [2] <STRING> 'to' <ARRAY> [1] <DICTIONARY> DICTIONARY [2] <DICTIONARY> DICTIONARY <ARRAY> [1] <STRING> 'east' [2] <STRING> 'north' [3] <STRING> 'up' [4] <STRING> 'origin' <ARRAY> [1] <REAL> 100 [2] <REAL> 200 [3] <REAL> 300 [4] <STRING> '/*' <ARRAY> [1] <STRING> 'east' [2] <STRING> 'north' [3] <STRING> 'up' [4] <STRING> 'origin' <ARRAY> [1] <REAL> 400 [2] <REAL> 500 [3] <REAL> 600 [4] <STRING> '/*'
The test cases use PML Unit on Everything3D 2.1.