1 / 18

Serializing ProDataSets to JSON

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)

declan
Download Presentation

Serializing ProDataSets to JSON

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Session 126 Serializing ProDataSetsto JSON AJAX Data Transfer Michael Resnick Principal Software EngineerOpenEdge Client Development

  2. 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"}] }};

  3. 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”]

  4. {"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

  5. A WebSpeed Application ABL Browser WebSpeed Agent Web Server WebSpeed Messenger Database

  6. JSON Syntax: Simple Values "identifier" :value value

  7. 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}]

  8. JSON Syntax: Non-Standard Data Types • Not defined by JSON syntax • Multiple formats in use • OpenEdge formats

  9. 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])

  10. ABL Attributes and Options

  11. 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

  12. {“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

  13. 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

  14. ProDataSet with Nested Temp-tables as JSON {"dsCustOrd": { “ttCust": [ { “CustNum": 1, “Balance”: 55000, “ttOrder": [ { “OrderNum": 100, “CustNum”: 1 }, { “OrderNum": 102, “CustNum”: 1 }, … ] }, { “CustNum”: … }, … ] }} 14

  15. 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

  16. 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

  17. Summary • JSON is easy in JavaScript • JSON is trivial in ABL

  18. Session 126 Serializing ProDataSetsto JSON AJAX Data Transfer Michael Resnick Principal Software EngineerOpenEdge Client Development

More Related