Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Titan » Support for JSON binary encodings in Titan: CBOR
Support for JSON binary encodings in Titan: CBOR [message #1768463] Wed, 19 July 2017 06:50
Elemer Lelik is currently offline Elemer LelikFriend
Messages: 760
Registered: January 2015
Senior Member
Dear all,

Human-readable text-based data interchange formats are immensely popular, however they come with a caveat:
they are less efficient than binary serializations. CBOR, defined in an Internet Standards Document, RFC 7049,
promises smaller message sizes while retaining JSONs simple yet comprehensive data model.

CBOR is supported in Titan in form of two conversion functions json2cbor and cbor2json ,

see par. 4.13.12, 4.13.13 of the reference guide:


4.13.12 json2cbor
Syntax: json2cbor(in universal charstring us) return octetstring

The function json2cbor(in universal charstring us) return octetstring converts a TITAN encoded json document into the binary
representation of that json document using a binary coding called CBOR.
The encoding follows the recommendations written in the CBOR standard [22] section 4.2.
EXAMPLE: json2cbor("{"a":1,"b":2}") results in 'A2616101616202'O

4.13.13 cbor2json

Syntax: cbor2json(in octetstring os) return universal charstring

The function cbor2json(in octetstring os) return universal charstring converts a CBOR encoded bytestream into a json
document which can be decoded using the built in JSON decoder.
The decoding follows the recommendations written in the CBOR standard [22] section 4.1
except that the indefinite-length items are not made definite before conversion and the decoding of indefinite-length items is not supported.
EXAMPLE: cbor2json('A2616101616202'O) results in "{"a":1,"b":2}"


The attached code exemplifies CBOR encoding/decoding by resuscitating an earlier JSON example
( Using JSON in Titan part III: JSON RPC
https://www.eclipse.org/forums/index.php/t/1070141/ )
with some modifications.

TTCN-3 templates are first encoded in JSON then recoded in CBOR ; length of JSON vs. CBOR representations is recorded.
Similarly, decoding is performed, first from CBOR to JSON then to TTCN-3.
Average JSON to CBOR size ratio was 1.46 , that is the JSON message was 46% longer than the equivalent CBOR.
This of course will vary depending on JSON content and structure.

Finally, the average length of a json2cbor and cbor2json cycle is meaasured(this of course strongly depends on the machine used, JSON size and content etc.)
On my machine an encoding cycle(json --> cbor) was 17 microseconds, while a decoding cycle (cbor --> json) about 20 microseconds.




15:16:11.639819 - TTCN-3 Test Executor started in single mode. Version: CRL 113 200/6 R2A.
15:16:11.639870 - Maximum number of open file descriptors: 8193,   FD_SETSIZE = 1024
15:16:11.642569 - TTCN Logger v2.2 options: TimeStampFormat:=Time; LogEntityName:=No; LogEventTypes:=No; SourceInfoFormat:=Single; *.FileMask:=LOG_ALL; *.ConsoleMask:=ACTION | ERROR | TESTCASE | STATISTICS_VERDICT | STATISTICS_UNQUALIFIED | WARNING; LogFileSize:=0; LogFileNumber:=1; DiskFullAction:=Error
15:16:11.642594 - Initializing module JSON_RPC.
15:16:11.642647 - Initialization of module JSON_RPC finished.
15:16:11.642661 - Initializing module PreGenRecordOf.
15:16:11.642672 - Initialization of module PreGenRecordOf finished.
15:16:11.642683 - Initializing module TitanLoggerApi.
15:16:11.642694 - Initialization of module TitanLoggerApi finished.
15:16:11.642709 JSON_RPC.ttcn:463 Execution of control part in module JSON_RPC started.
15:16:11.642925 JSON_RPC.ttcn:481 JSON/CBOR ratio:  1.386364
15:16:11.642983 JSON_RPC.ttcn:482 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "subtract", params := { recOfInt := { 42, 23 } }, id := { num_id := 1 } } }
15:16:11.643098 JSON_RPC.ttcn:491 JSON/CBOR ratio:  1.440000
15:16:11.643117 JSON_RPC.ttcn:493 full cycle:  { jSON_RPC_Resp := { jsonrpc := "2.0", result := { number := 19 }, error_ := omit, id := { num_id := 1 } } }
15:16:11.643192 JSON_RPC.ttcn:502 JSON/CBOR ratio:  1.386364
15:16:11.643211 JSON_RPC.ttcn:504 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "subtract", params := { recOfInt := { 23, 42 } }, id := { num_id := 2 } } }
15:16:11.643273 JSON_RPC.ttcn:512 JSON/CBOR ratio:  1.480000
15:16:11.643289 JSON_RPC.ttcn:514 full cycle:  { jSON_RPC_Resp := { jsonrpc := "2.0", result := { number := -19 }, error_ := omit, id := { num_id := 2 } } }
15:16:11.643355 JSON_RPC.ttcn:523 JSON/CBOR ratio:  1.333333
15:16:11.643373 JSON_RPC.ttcn:525 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "subtract", params := { namedPars := { subtrahend := 42, minuend := 23 } }, id := { num_id := 3 } } }
15:16:11.643449 JSON_RPC.ttcn:533 JSON/CBOR ratio:  1.511111
15:16:11.643466 JSON_RPC.ttcn:535 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "update", params := { recOfInt := { 1, 2, 3, 4, 5, 6 } }, id := omit } }
15:16:11.643529 JSON_RPC.ttcn:542 JSON/CBOR ratio:  1.451613
15:16:11.643544 JSON_RPC.ttcn:544 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "foobar", params := omit, id := omit } }
15:16:11.643703 JSON_RPC.ttcn:552 JSON/CBOR ratio:  1.354839
15:16:11.643724 JSON_RPC.ttcn:554 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "foobar", params := omit, id := { num_id := 1 } } }
15:16:11.643788 JSON_RPC.ttcn:560 JSON/CBOR ratio:  1.350877
15:16:11.643805 JSON_RPC.ttcn:562 full cycle:  { jSON_RPC_Resp := { jsonrpc := "2.0", result := omit, error_ := { code := -32601, message_ := "Method not found", data := omit }, id := { num_id := 5 } } }
15:16:11.643877 JSON_RPC.ttcn:570 JSON/CBOR ratio:  1.450000
15:16:11.643895 JSON_RPC.ttcn:572 full cycle:  { jSON_RPC_Req := { jsonrpc := "2.0", method := "sum", params := { recOfInt := { 1, 2, 3, 4 } }, id := { num_id := 7 } } }
15:16:11.643962 JSON_RPC.ttcn:581 JSON/CBOR ratio:  1.442308
15:16:11.643982 JSON_RPC.ttcn:583 full cycle:  { jSON_RPC_Resp := { jsonrpc := "2.0", result := omit, error_ := { code := -32700, message_ := "Parse error", data := omit }, id := omit } }
15:16:11.644080 JSON_RPC.ttcn:590 JSON/CBOR ratio:  1.426036
15:16:11.644109 JSON_RPC.ttcn:592 full cycle:  { rec_JSON_RPC_Resp := { { jsonrpc := "2.0", result := omit, error_ := { code := -32600, message_ := "Invalid request", data := omit }, id := omit }, { jsonrpc := "2.0", result := omit, error_ := { code := -32600, message_ := "Invalid request", data := omit }, id := omit }, { jsonrpc := "2.0", result := omit, error_ := { code := -32600, message_ := "Invalid request", data := omit }, id := omit } } }
15:16:11.644270 JSON_RPC.ttcn:601 JSON/CBOR ratio:  1.430000
15:16:11.644289 JSON_RPC.ttcn:603 full cycle:  { rec_JSON_RPC_Resp := { { jsonrpc := "2.0", result := { number := 7 }, error_ := omit, id := { char_id := "1" } }, { jsonrpc := "2.0", result := { number := 19 }, error_ := omit, id := { char_id := "2" } }, { jsonrpc := "2.0", result := omit, error_ := { code := -32600, message_ := "Invalid request", data := omit }, id := omit }, { jsonrpc := "2.0", result := omit, error_ := { code := -32601, message_ := "Method not found", data := omit }, id := { char_id := "5" } }, { jsonrpc := "2.0", result := { hello := { { charfield := "hello" }, { intfield := 5 } } }, error_ := omit, id := { char_id := "9" } } } }
15:16:11.644416 JSON_RPC.ttcn:611 cbor encoded:  'A4676A736F6E72706363322E30666D6574686F6466666F6F62617266706172616D73636261726269646131'O
15:16:11.644476 JSON_RPC.ttcn:613 JSON/CBOR ratio 1.418605
15:16:11.644495 JSON_RPC.ttcn:615 { jSON_RPC_Req := { jsonrpc := "2.0", method := "foobar", params := { xar := "bar" }, id := { char_id := "1" } } }
15:16:11.644521 JSON_RPC.ttcn:620 cbor encoded:  'A3676A736F6E72706363322E30666D6574686F6466666F6F62617266706172616D7363626172'O
15:16:11.644568 JSON_RPC.ttcn:622 JSON/CBOR ratio 1.342105
15:16:11.644582 JSON_RPC.ttcn:625 { jSON_RPC_Req := { jsonrpc := "2.0", method := "foobar", params := { xar := "bar" }, id := omit } }
15:16:11.644604 JSON_RPC.ttcn:632 cbor encoded:  '82A3676A736F6E72706363322E30666D6574686F646A6E6F746966795F73756D66706172616D7383010204A3676A736F6E72706363322E30666D6574686F646C6E6F746966795F68656C6C6F66706172616D738107'O
15:16:11.644692 JSON_RPC.ttcn:634 JSON/CBOR ratio 1.552941
15:16:11.644708 JSON_RPC.ttcn:637 { rec_JSON_RPC_Req := { { jsonrpc := "2.0", method := "notify_sum", params := { recOfInt := { 1, 2, 4 } }, id := omit }, { jsonrpc := "2.0", method := "notify_hello", params := { recOfInt := { 7 } }, id := omit } } }
15:16:11.644748 JSON_RPC.ttcn:648 cbor encoded:  '85A3676A736F6E72706363322E3066726573756C74076269646131A3676A736F6E72706363322E3066726573756C74136269646132A3676A736F6E72706363322E30656572726F72A264636F6465397F57676D6573736167656F496E76616C6964205265717565737462696400A3676A736F6E72706363322E30656572726F72A264636F6465397F58676D657373616765704D6574686F64206E6F7420666F756E646269646135A3676A736F6E72706363322E3066726573756C74826568656C6C6F056269646139'O
15:16:11.644923 JSON_RPC.ttcn:650 JSON/CBOR ratio 1.675000
15:16:11.644940 JSON_RPC.ttcn:653 { rec_JSON_RPC_Resp := { { jsonrpc := "2.0", result := { number := 7 }, error_ := omit, id := { char_id := "1" } }, { jsonrpc := "2.0", result := { number := 19 }, error_ := omit, id := { char_id := "2" } }, { jsonrpc := "2.0", result := omit, error_ := { code := -32600, message_ := "Invalid Request", data := omit }, id := { num_id := 0 } }, { jsonrpc := "2.0", result := omit, error_ := { code := -32601, message_ := "Method not found", data := omit }, id := { char_id := "5" } }, { jsonrpc := "2.0", result := { hello := { { charfield := "hello" }, { intfield := 5 } } }, error_ := omit, id := { char_id := "9" } } } }
15:16:11.645014 JSON_RPC.ttcn:655 -------------------------------------------------------------------------------------------
15:16:11.645029 JSON_RPC.ttcn:665 cbor encoded:  '85A4676A736F6E72706363322E30666D6574686F646373756D66706172616D73830102046269646131A3676A736F6E72706363322E30666D6574686F646C6E6F746966795F68656C6C6F66706172616D738107A4676A736F6E72706363322E3066706172616D7382182A17666D6574686F646873756274726163746269646132A4676A736F6E72706363322E30666D6574686F6467666F6F2E67657466706172616D7382182A176269646135A3676A736F6E72706363322E30666D6574686F64686765745F646174616269646139'O
15:16:11.645211 JSON_RPC.ttcn:667 JSON/CBOR ratio 1.645631
15:16:11.645227 JSON_RPC.ttcn:670 { rec_JSON_RPC_Req := { { jsonrpc := "2.0", method := "sum", params := { recOfInt := { 1, 2, 4 } }, id := { char_id := "1" } }, { jsonrpc := "2.0", method := "notify_hello", params := { recOfInt := { 7 } }, id := omit }, { jsonrpc := "2.0", method := "subtract", params := { recOfInt := { 42, 23 } }, id := { char_id := "2" } }, { jsonrpc := "2.0", method := "foo.get", params := { recOfInt := { 42, 23 } }, id := { char_id := "5" } }, { jsonrpc := "2.0", method := "get_data", params := omit, id := { char_id := "9" } } } }
15:16:11.645297 JSON_RPC.ttcn:675 cbor encoded:  'A3676A736F6E72706363322E30656572726F72A264636F6465397FBB676D6573736167656B5061727365206572726F7262696407'O
15:16:11.645361 JSON_RPC.ttcn:677 JSON/CBOR ratio 1.596154
15:16:11.645376 JSON_RPC.ttcn:680 { jSON_RPC_Resp := { jsonrpc := "2.0", result := omit, error_ := { code := -32700, message_ := "Parse error", data := omit }, id := { num_id := 7 } } }
15:16:11.645404 JSON_RPC.ttcn:686 cbor encoded:  'A3676A736F6E72706363322E30656572726F72A264636F6465397FBB676D6573736167656B5061727365206572726F72626964F6'O
15:16:11.645464 JSON_RPC.ttcn:688 JSON/CBOR ratio 1.653846
15:16:11.645479 JSON_RPC.ttcn:690 { jSON_RPC_Resp := { jsonrpc := "2.0", result := omit, error_ := { code := -32700, message_ := "Parse error", data := omit }, id := omit } }
15:16:11.645504 JSON_RPC.ttcn:693 JSON/CBOR ratio data: { 1.386364, 1.440000, 1.386364, 1.480000, 1.333333, 1.511111, 1.451613, 1.354839, 1.350877, 1.450000, 1.442308, 1.426036, 1.430000, 1.418605, 1.342105, 1.552941, 1.675000, 1.645631, 1.596154, 1.653846 }
15:16:11.645553 JSON_RPC.ttcn:701 ********************************
15:16:11.645566 JSON_RPC.ttcn:702 average JSON/CBOR ratio: 1.466356
15:16:11.645579 JSON_RPC.ttcn:703 ********************************
15:16:11.645591 JSON_RPC.ttcn:717 Start timer t0: 100 s
15:16:13.369904 JSON_RPC.ttcn:726 Read timer t0: 1.72428 s
15:16:13.369898 JSON_RPC.ttcn:726 1.724284
15:16:15.413602 JSON_RPC.ttcn:734 Read timer t0: 3.76798 s
15:16:15.413596 JSON_RPC.ttcn:734 3.767982
15:16:15.413702 JSON_RPC.ttcn:734 Stop timer t0: 100 s
15:16:15.413718 JSON_RPC.ttcn:734 Execution of control part in module JSON_RPC finished.
15:16:15.413773 - Verdict statistics: 0 none, 0 pass, 0 inconc, 0 fail, 0 error.
15:16:15.413805 - Test execution summary: 0 test case was executed. Overall verdict: none
15:16:15.413824 - TTCN-3 Test Executor finished in single mode.



Best regards

Elemer


P.S. This feature is available in Titan post 6.2.0 only; If someone needs it now, will have to compile a fresh Titan from source code.

[Updated on: Wed, 19 July 2017 08:08]

Report message to a moderator

Previous Topic:Regular expression escaping in configuration file
Next Topic:problem in RAW decoder with TAG of different length
Goto Forum:
  


Current Time: Mon Jun 25 04:46:41 GMT 2018

Powered by FUDForum. Page generated in 0.02461 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top