More Related Content Similar to Native json in the Cache' ObjectScript 2016.* (20) More from Timur Safin (8) Native json in the Cache' ObjectScript 2016.*4. Это – JSON
"employees“ : [
{"firstName” : "John", "lastName“ : "Doe"},
{"firstName“ : "Anna", "lastName“ : "Smith"},
{"firstName“ : "Peter", "lastName“ : "Jones"}
]
5. Это – не совсем JSON
SELECT '[1, 2, "foo", null]'::json;
SELECT '{"bar": "baz", "balance": 7.77, "active": false}'::json;
SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::json;
6. %Object и %Array
USER>set object = ##class(%Object).$new()
USER>set object.name = "Stefan Wittmann"
USER>set object.lastSeriesSeen = "Daredevil"
USER>set object.likes = "Galaxy"
7. %Object и %Array
USER>set array = ##class(%Array).$new()
USER>do array.$push(1)
USER>do array.$push("This is a string")
USER>do array.$push(object)
8. Сериализация в JSON
USER>do object.$toJSON()
{"name":"Stefan Wittmann","lastSeriesSeen":"Daredevil",
"likes":"Galaxy"}
USER>do array.$toJSON()
[1,"This is a string.",{"name":"Stefan Wittmann",
"lastSeriesSeen":"Daredevil","likes":"Galaxy"}]
9. Разбор JSON
USER>set someJSONstring = "{""firstname"":""Stefan"",
""lastname"":""Wittmann""}"
USER>set consumedJSON = ##class(%AbstractObject).
$fromJSON(someJSONstring)
USER>write consumedJSON.$size()
2
USER>write consumedJSON.$toJSON()
{"firstname":"Stefan","lastname":"Wittmann"}
10. Итераторы на %Object
USER>set iter = object.$getIterator()
USER>while iter.$getNext(.key,.value) {
write "key "_key_" : "_value,! }
key name : Stefan Wittmann
key lastSeriesSeen : Daredevil
key likes : Galaxy
11. Итераторы на %Array
USER>set iter = array.$getIterator()
USER>while iter.$getNext(.key,.value) {
write "key "_key_" : "_value,! }
key 0 : 1
key 1 : This is a string.
key 2 : 2@%Library.Object
12. Присваивание элемента в %Array
USER>do array.$set(10,
"This is a string in a sparse array")
USER>write array.$toJSON()
[1,"This is a string.",{"name":"Stefan Wittmann",
"lastSeriesSeen":"Daredevil","likes":"Galaxy"},
null,null,null,null,null,null,null,
"This is a string in a sparse array"]
13. Итератор по разреженному массиву
USER>set iter = array.$getIterator()
USER>while iter.$getNext(.key,.value) {
write "key "_key_" : "_value,! }
key 0 : 1
key 1 : This is a string.
key 2 : 2@%Library.Object
key 10 : This is a string in a sparse array
14. Native JSON Syntax
USER>set object = {"name":"Stefan Wittmann",
"lastMovieSeen":"The Martian",
"likes":"Writing Blogs"}
USER>write object.$toJSON()
{"name":"Stefan Wittmann","lastMovieSeen":
"The Martian","likes":"Writing Blogs"}
USER>set array = [1,2,3,[4,5,6],true,false,null]
USER>write array.$toJSON()
[1,2,3,[4,5,6],true,false,null]
16. Native JSON Syntax
USER>set name = "Stefan"
USER>set subObject = {"nationality":"German",
"favoriteColors":["yellow","blue"]}
USER>set object = {"name":name,"details":subObject,
"lastUpdate":$ZD($H,3)}
USER>write object.$toJSON()
{"name":"Stefan","details":{"nationality":"German",
"favoriteColors":["yellow","blue"]}," lastUpdate ":
"2016-01-31"}
17. Native JSON Syntax
USER>set array = [1,2,3,[4,5,6],true,false,null]
USER>set iter = array.$getIterator()
USER>while iter.$getNext(.key,.value) {
write "key "_key_":"_value,! }
key 0:1
key 1:2
key 2:3
key 3:5@%Library.Array
key 4:1
key 5:0
key 6:
18. Native JSON Syntax
USER>set array = [1,2,3,[4,5,6],true,false,null]
…
USER>w array.$getTypeOf(5)
boolean
USER>w array.$getTypeOf(6)
null
19. Native JSON Syntax
USER>set array = [1,2,3,[4,5,6],true,false,null]
…
USER>do array.$set(7,1)
USER>write array.$toJSON()
[1,2,3,[4,5,6],true,false,null,1]
USER>do array.$set(7,1,"boolean")
USER>write array.$toJSON()
[1,2,3,[4,5,6],true,false,null,true]
21. $compose
SAMPLES>set object = array.$compose("%Object")
SAMPLES>write ["zero","one","two"].
$compose("%Object").$toJSON()
{"0":"zero","1":"one","2":"two"}
22. $compose
SAMPLES>set person = ##class(Sample.Person).%OpenId(10)
SAMPLES>set object = person.$compose("%Object")
SAMPLES>write object.$toJSON()
{"$CLASSNAME":"Sample.Person","$REFERENCE":"10",
"Age":46,"DOB":47058,"FavoriteColors":[],
"Home":{"City":"Washington","State":"HI","Street":
"4358 Franklin Place","Zip":59519},"Name":"Quincy,Neil Z.",
"Office":{"City":"Bensonhurst","State":"WI","Street":
"8620 Clinton Drive","Zip":75074},"SSN":"966-11-9404"}
23. Result Sets
SAMPLES>set result = $system.SQL.Execute("call sample.sp_sample_by_name('N')").
%NextResult()
SAMPLES>write result.$toJSON()
{"content":[{"DOB":"63986","ID":"189","Name":"Nathanson,Natasha T.","SSN":
"439-13-7455"},{"DOB":"58420","ID":"85","Name":"Nelson,Charlotte Y.",
"SSN":"664-42-8486"},{"DOB":"34965","ID":"150","Name":"Noodleman,Charles Y.",
"SSN":"156-64-3875"},{"DOB":"39300","ID":"134","Name":"North,Ted J.",
"SSN":"308-14-4306"}],"metadata":{"columnCount":4,"columns":
[{"ODBCType":4,"clientType":"","colName":"ID","isAliased":1,"isAutoIncrement":1,
"isCaseSensitive":1,"isCurrency":0,"isExpression":0,"isHidden":0,"isIdentity":1,
"isKeyColumn":1,"isNullable":0,"isReadOnly":1,"isRowId":1,"isRowVersion":0,
"isUnique":1,"label":"ID","precision":10,"qualifier":"","scale":0,"schemaName":
"Sample","tableName":"Person"},{"ODBCType":12,"clientType":"","colName":"Name",
"isAliased":1,"isAutoIncrement":0,"isCaseSensitive":0,"isCurrency":0,
"isExpression":0,"isHidden":0,"isIdentity":0,"isKeyColumn":0,"isNullable":0,
"isReadOnly":0,"isRowId":0,"isRowVersion":0,"isUnique":0,"label":"Name",
"precision":50,"qualifier":"","scale":0…
24. И это мы еще не
рассказали про JSON
расширения в SQL…
25. JSON & SQL
LATEST:USER>set response = ##class(%Net.Http).getJSON(
"http://localhost:57772/api/document/v1/SAMPLES/continents",
{"Username":"_SYSTEM","Password":"SYS"})
LATEST:USER>do response.$toJSON()
{"collection":"continents","size":8,"content":[{"documentID":1,
"documentVersion":1,"content":{"code":"NA","name":"North America"}},
{"documentID":2,"documentVersion":2,"content":{"code":"SA","name":
"South America"}},{"documentID":3,"documentVersion":3,"content":{"code":
"AF","name":"Africa"}},{"documentID":4,"documentVersion":4,"content":
{"code":"AS","name":"Asia"}},{"documentID":5,"documentVersion":5,
"content":{"code":"EU","name":"Europe"}},{"documentID":6,"documentVersion":6,
"content":{"code":"OC","name":"Oceana"}},{"documentID":7,"documentVersion":7,
"content":{"code":"AN","name":"Antarctica"}},{"documentID":9,"documentVersion":8,
"content":{}}]}
26. JSON & SQL
select code, name from JSON_TABLE(%Net.getJSON(
'http://localhost/api/document/v1/SAMPLES/continents',
'{"Username":"_SYSTEM","Password":"SYS","Port":57772}'),
'$.content' columns (document varchar(2000) path '$',
code varchar(2) path '$.content.code',
name varchar(50) path '$.content.name')) order by name
28. Производительность разбора JSON
(Each company has an average of 20
employees and 20 customers each with several
addresses)
Caché 2015.1
zenProxyObject
Caché 2016.*
Native JSON
NodeJS
0.12.2
v8: 3.28.73
Load 1000 companies JSON file
(10.8MB)
28000ms 94ms 97ms
Find how many employees called
"Robert“ (126 employees)
55ms 55ms 2ms
Load 10,000 companies JSON file
(108MB)
386700ms 904ms 892ms
Find how many employees called
"Robert" (1346 employees)
554.5ms 567ms 13ms