180 likes | 405 Views
Session 126. Serializing ProDataSets to JSON. AJAX Data Transfer. Michael Resnick Principal Software Engineer OpenEdge Client Development. What Is JSON?. Serialization format for exchanging data with browsers Subset of JavaScript ( J ava S cript O bject N otation)
E N D
Session 126 Serializing ProDataSetsto JSON AJAX Data Transfer Michael Resnick Principal Software EngineerOpenEdge Client Development
What Is JSON? • Serialization format for exchanging data with browsers • Subset of JavaScript (JavaScript Object Notation) var dataset = {"dsCustOrd": { "ttCustomer": [{"CustNum": 1, "Name": "Lift Tours", "Balance": 55000.0}],“ttOrder":[ {"OrderNum": 100, "CustNum": 1, "OrderDate": "2009―01―12"}] }};
Why JSON? Why Not XML? • Smaller than XML • Syntax indicates data type • Data binding done by definition • Parsing: var dataset = eval(jsonString); • Dereferencing: • dataset.dsCustOrd.ttCustomer[0].name • dataset[“dsCustOrd”][“ttCustomer”][0][“name”]
{"dsCustOrd": {"ttCustomer": [{"CustNum":1,"Name":"Lift Tours","Balance":55000.0}, {"CustNum":3,"Name":"Hoops","Balance":23500.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}],"ttOrder": [{"OrderNum":100,"CustNum":1,"OrderDate":"2009―01―12"}, {"OrderNum":101,"CustNum":1,"OrderDate":"2009―01―12"}, {"OrderNum":250,"CustNum":3,"OrderDate":"2009―01―07"}, {"OrderNum":251,"CustNum":3,"OrderDate":"2009―01―07"}, {"OrderNum":500,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}]}} {"dsCustOrd": {"ttCustomer": [{"CustNum":1,"Name":"Lift Tours","Balance":55000.0}, {"CustNum":3,"Name":"Hoops","Balance":23500.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}, {"CustNum":17,"Name":"Acme Sports Widgets","Balance":10.0}],"ttOrder": [{"OrderNum":100,"CustNum":1,"OrderDate":"2009―01―12"}, {"OrderNum":101,"CustNum":1,"OrderDate":"2009―01―12"}, {"OrderNum":250,"CustNum":3,"OrderDate":"2009―01―07"}, {"OrderNum":251,"CustNum":3,"OrderDate":"2009―01―07"}, {"OrderNum":500,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}, {"OrderNum":501,"CustNum":17,"OrderDate":"2009―06―23"}]}} <dsCustOrd><ttCustomer><Row><CustNum>1</CustNum><Name>Lift Tours</Name><Balance>55000.0</Balance></Row><Row><CustNum>3</CustNum><Name>Hoops</Name><Balance>23500.0</Balance></Row><Row><CustNum>17</CustNum><Name>Acme Sports Widgets</Name><Balance>10.0</Balance></Row><Row><CustNum>17</CustNum><Name>Acme Sports Widgets</Name><Balance>10.0</Balance></Row><Row><CustNum>17</CustNum><Name>Acme Sports Widgets</Name><Balance>10.0</Balance></Row></ttCustomer><ttOrder><Row><OrderNum>100</OrderNum><CustNum>1</CustNum><OrderDate>2009―01―12</OrderDate></Row><Row><OrderNum>101</OrderNum><CustNum>1</CustNum><OrderDate>2009―01―12</OrderDate></Row><Row><OrderNum>250</OrderNum><CustNum>3</CustNum><OrderDate>2009―01―07</OrderDate></Row><Row><OrderNum>251</OrderNum><CustNum>3</CustNum><OrderDate>2009―01―07</OrderDate></Row><Row><OrderNum>500</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row><Row><OrderNum>501</OrderNum><CustNum>17</CustNum><OrderDate>2009―06―23</OrderDate></Row></ttOrder></dsCustOrd> Data – XML vs. JSON
A WebSpeed Application ABL Browser WebSpeed Agent Web Server WebSpeed Messenger Database
JSON Syntax: Simple Values "identifier" :value value
JSON Syntax: Complex Values • Object: list of name-value pairs surrounded by braces("{" and "}")"person": {"name": “Dorothy Gale","age": 8,"atHome": false} • Arrays: list of values surrounded by square brackets("[" and "]") "name": ["Alfred", "E", "Newman"] “people”: [{"name": "Dorothy", "age": 8},{"name": "Alfred", "age": 60},{"name": "Oscar", "age": 1}]
JSON Syntax: Non-Standard Data Types • Not defined by JSON syntax • Multiple formats in use • OpenEdge formats
ABL Methods WRITE-JSON ( mode,{file|stream|stream-handle|memptr|longchar}[, formatted[, encoding[, omit-initial-values]]]) READ-JSON (source-type,{file|memptr|handle|longchar}[, read-mode])
ProDataSet Definition DEFINE TEMP-TABLE ttCust FIELD CustNum as INTEGER FIELD Balance as INTEGER …. DEFINE TEMP-TABLE ttOrder FIELD OrderNum as INTEGER FIELD CustNum as INTEGER …. DEFINE DATASET dsCustOrd FOR ttCust, ttOrder DATA-RELATION FOR ttCust, ttOrder RELATION-FIELD (CustNum, CustNum). 11
{“ttCust": [ { “CustNum": 1, “Balance": 55000, … }, { “CustNum": 2, “Balance": 23500, … }, { “CustNum": 3, “Balance": 0, … } ]} {"dsCustOrd": { “ttCust": [ { “CustNum": 1, “Balance”: 55000, … }, … ], “ttOrder": [ { “OrderNum": 100, “CustNum”: 1, … }, … ], … } Temp-table and ProDataSet As JSON 12
ProDataSet with Nested Tables DEFINE TEMP-TABLE ttCust FIELD CustNum as INTEGER FIELD Balance as INTEGER …. DEFINE TEMP-TABLE ttOrder FIELD OrderNum as INTEGER FIELD CustNum as INTEGER …. DEFINE DATASET dsCustOrd FOR ttCust, ttOrder DATA-RELATION FOR ttCust, ttOrder RELATION-FIELD (CustNum, CustNum) NESTED. 13
ProDataSet with Nested Temp-tables as JSON {"dsCustOrd": { “ttCust": [ { “CustNum": 1, “Balance”: 55000, “ttOrder": [ { “OrderNum": 100, “CustNum”: 1 }, { “OrderNum": 102, “CustNum”: 1 }, … ] }, { “CustNum”: … }, … ] }} 14
Inference • Can infer • Datasets andtemp-tables if JSON looks like previous two slides • Numbers, booleans and string fields • Relationships if JSON contains nested tables • Cannot infer • Date and binary fields => string • Meta data such as keys
PROCEDURE process-web-request: ... hds = DATASET dsCustOrd:HANDLE. IF NOTWEB-CONTEXT:IS-JSON THEN DO: output-content-type("application/json":U). hds:WRITE-JSON("STREAM", "WebStream"). END. ELSE DO: hds:READ-JSON("HANDLE", WEB-CONTEXT). END. ... END PROCEDURE. WebSpeed Code 1 2 3
Summary • JSON is easy in JavaScript • JSON is trivial in ABL
Session 126 Serializing ProDataSetsto JSON AJAX Data Transfer Michael Resnick Principal Software EngineerOpenEdge Client Development