![Page 1: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/1.jpg)
1
Introduction to JSON in DataFlex
Harm Wibier
Data Access Worldwide
![Page 2: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/2.jpg)
2
JavaScript Object Notation
• JSON is a lightweight data-interchange format.
• JSON is a text format.
• JSON is completely language independent.
• JSON is based on a subset of JavaScript.
• JSON is easy for humans tor read and write.
• JSON is easy for machines parse and generate.
• JSON is familiar to programmers of C-family languages like C++, Java, Phyton, JavaScript.
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 3: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/3.jpg)
3
The JSON Format
• Objects
• Arrays
• Strings
• Booleans
• Numbers
{"name" : "John","details" : {
"age" : 31,"male" : true
},"ratings" : [ 8, 7.5, 8, 5.5 ]
}
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 4: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/4.jpg)
4
JSON XML<?xml version="1.0" encoding="UTF-8" ?><student>
<name>John</name><details>
<age>31</age><male>true</male>
</details><ratings>
<rate>8</rate><rate>7.5</rate><rate>8</rate><rate>5.5</rate>
</ratings></student>
{"name" : "John","details" : {
"age" : 31,"male" : true,
},"ratings" : [
8,7.5,8,5.5
]}
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 5: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/5.jpg)
5
JSON XML
• Human readable
• Hierachical
• Quicker
– Shorter
• No end tags
– Easier to parse
• Can be evaluated in some languges
• Has arrays
• Lighter and native to JavaScript
• Human readable
• Hierachical
• Better standardized
• More extensive
– Attributes
– Namespaces
– XML Schema
– XSL
– XPath
• Heavier but wider supported
![Page 6: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/6.jpg)
6
Usage
• Interchange data
– REST JSON API’s
– JavaScript WebApps
• Like the WebApp Framework
• Store data
– Serialize data from memory
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 7: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/7.jpg)
7
JSON in DataFlex
• Web Services
– Built in JSON support
– Every DataFlex Web-Service can be called using JSON
– JSON parsing & generation happens in ISAPI handler
• cJsonObject
– Manually parse & generate JSON
– Serialize / deserialize structs and arrays
• cJsonHttpTransfer
– Communicate with JSON Services
http://localhost/WebOrder_19/CustomerAndOrderInfo.wso
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 8: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/8.jpg)
8
cJsonObject
• Parse JSON
• Generate JSON
• Manipulate JSON like a DOM structure
• Convert DataFlex structs and arrays into JSON
• Convert JSON into DataFlex structs and arrays
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 9: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/9.jpg)
9
Parsing Examples
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 10: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/10.jpg)
10
Handle parsing errors
• ParseString returns false indicating error
• ReportParseError
– Generates a DataFlex error
• psParseError
– Provides the error text
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
Get ParseString of hoJson sData to bSuccessIf (bSuccess) Begin
// Do your thing! EndElse Begin
// Handle errors how you preferSend ReportParseError of hoJson
End
Send Destroy of hoJson
![Page 11: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/11.jpg)
11
JSON <> StructStruct tStudentDetail
Integer ageBoolean male
End_Struct
Struct tStudentString nametStudentDetail detailsNumber[] ratings
End_Struct
{"name": "Troy Umstead","details": {"age": 20,"male": true},"ratings": [2.3,5.2,4.0,9.4]}
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 12: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/12.jpg)
12
Structs with JSON
• Parse JSON into Struct
– Parse JSON using cJsonObject
– Use JsonToDataType function
• Fills a struct using JSON data
– pbRequireAllMembers• If set to false missing members will just be ignored and are left empty
• Serialize Struct to JSON
– Use DataTypeToJson procedure
• Generates JSON objects based on the struct data
– Stringify into a JSON string
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 13: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/13.jpg)
13
Struct Examples
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 14: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/14.jpg)
14
API Overview
• cJsonObject
– ParseString, ParseUtf8
• Parses a JSON string into the JSON DOM
– Stringify, StringifyUtf8
• Generates the JSON string from the JSON DOM
– DataTypeToJson, JsonToDataType
• Convert JSON DOM to structs / arrays
– Member, MemberByIndex, MemberCount, MemberNameByIndex, HasMember, JsonType
• Traverse JSON DOM
– AddMember, SetMember, SetMemberValue, InitializeJsonType
• Manipulate JSON
2017/04/05Working with JSON by John Tuohy and Harm Wibier
![Page 15: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/15.jpg)
15
ParseUtf8 and StringifyUtf8
• UChar array as parameter / argument
– No argument-size limitations
– Supported by Read_Block, Write_Block, Set_Field_Value, Get_Field_Value,
Field_Current_UCAValue
• Expected encoding is UTF-8
– Default format when transmitting JSON object the web
• Convert manually if needed
– ConvertUCharArray
of cChartTranslate
UChar[] uData
Direct_Input "FileWithJsonData.json"Read_Block uData -1Close_Input
Get Create (RefClass(cJsonObject)) to hoJsonDomGet ParseUtf8 of hoJsonDom uData to bParsed
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 16: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/16.jpg)
16
peWhitespace
• jpWhitespace_Plain
• jpWhitespace_Spaced
• jpWhitespace_Pretty
2017/04/05Working with JSON by John Tuohy and Harm Wibier
{ "name": "John", "details": { "age": 31, "male": true }, "ratings": [ 8, 7.5, 8 ] }
{"name":"John","details":{"age":31,"male":true},"ratings":[8,7.5,8]}
{"name": "John","details": {
"age": 31,"male": true},"ratings": [
8,7.5,8]}
![Page 17: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/17.jpg)
17
cJsonObject Instances
• Multiple instances of the same JSON object are possible
• Internally reference counting is used
– Once a handle is obtained it will point to that piece of JSON until it is destroyed or reinitialized
• Obtaining as in Create, Member, MemberByIndex
• Destroying as in Destroy, RemoveMember
• Reinitializing means calling Parse, ParseUtf8, DataTypeToJson or InitializeJsonType
• One piece of JSON could part of two structures
– AddMember and SetMember work by reference (they do not clone the objects)
• Every JSON object needs to be destroyed individually
2017/04/05Working with JSON by John Tuohy and Harm Wibier
![Page 18: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/18.jpg)
18
cJsonHttpTransfer
• Easily communicate with Restfull JSON Services
• HTTP Verbs:
– POST, GET, DELETE, PUT, PATCH
• Directly parses into cJsonObject
John Tuohy will discuss this tomorrow in depth…
3/9/2018Introduction to JSON in DataFlex by Harm Wibier
![Page 19: Introduction to JSON in DataFlex · Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 13 Struct Examples Introduction to JSON in DataFlex by Harm Wibier 3/9/2018. 14 API Overview](https://reader033.vdocument.in/reader033/viewer/2022051604/6002cae4845bf07f7c1b3ea4/html5/thumbnails/19.jpg)
19
Have a nice day!
Thank you for your time
3/9/2018Introduction to JSON in DataFlex by Harm Wibier