Home » Eclipse Projects » Eclipse Titan » Schema-less decoding of JSON in TTCN-3/Titan
Schema-less decoding of JSON in TTCN-3/Titan [message #1803904] |
Wed, 13 March 2019 11:57 |
|
As in XML, a JSON Schema supplies a reference that permits validation of documents (or instances) versus the rules expressed in the schema.
The schema-based encoding/decoding is exemplified in:
Using JSON in TTCN-3 and Titan Part I: Testing REST / JSON web services: (Test of JSON -based REST web services)
https://www.eclipse.org/forums/index.php/t/1068704/
and can be illustrated as below:
However, JavaScript or Python do not need a schema to decode or encode JSON (that is, building an equivalent internal representation) and in some cases we might need
this possibility ourselves.
An example of how to achieve this is described in:
Using JSON in TTCN-3 and Titan Part II: Simple REST/JSON test framework(Using a generic JSON representation in TTCN-3)
https://www.eclipse.org/forums/index.php/t/1069269/
where a generic TTCN-3 structure and TEXT encoding/decoding is being used.
Schema-less processing flow is as below:
As mentioned in
Eclipse Titan release 6.5.0, intermediate release 6.5.1 announcements
https://www.eclipse.org/forums/index.php/t/1097206/
new JSON codec attributes permit now building a generic TTCN-3 structure based not on TEXT , but JSON encoding, which presents a more elegant and
lean solution.
The generic structure that can be used is as below (and will be published as a new version of the protocol module
https://github.com/eclipse/titan.ProtocolModules.JSON_v07_2006
type union JSON_PDU {
integer JSON_integer,
float JSON_number,
boolean JSON_bool,
universal charstring JSON_string,
set of JSON_PDU JSON_array,
record of record {
universal charstring key,
JSON_PDU val optional
} JSON_object
} with {
variant "JSON: as value"
variant (JSON_object) "as map"
}
To exemplify its functionality , I will reuse the REST/JSON example in:
Using JSON in TTCN-3 and Titan Part II: Simple REST/JSON test framework(Using a generic JSON representation in TTCN-3)
https://www.eclipse.org/forums/index.php/t/1069269/
where the old JSON_v07_2006 protocol is replaced with its new version (see attached code).
In this example I will disregard the evolution of the test case, the verdict value etc. and focus on the schema-less
decoding.
A sample of the expected JSON response is taken from the config file
:
json_response := "{\"Operated\":4,\"unavailable\":4,\"availavvvvvble\":1,\"happy\":1,\"available\":12067,\"[Tnn96]\":1,\"none\":1,\"whathwat\":4,\"Just Born\":1,\"xyz\":1,\"UNAVAILABLE\":1,\"Available\":1,\"'; INSERT INTO Users(username, password, priv) VALUES ('BobbyTables', 'kl20da$$','admin');\":1,\"1; select 1,2,3\":1,\"scary\":4,\"\\\\''); DROP TABLE users; --\":1,\"availablennnnn\":5,\"Dead\":1,\"ugly\":1,\"raer\":2,\"applicable\":1,\"smrt\":1,\"';UPDATE user SET type = 'admin' WHERE id = 23;--\":1,\"gone\":2,\"exit\":1,\"avadsggfilable\":1,\"I dont know\":2,\"invalid\":1,\";\":1,\"adorable\":2,\"aaa\":1,\"string\":171,\"pending\":173,\"Not-Operated\":10,\"available;pending;sold\":1,\"asdfasdfasd\":1,\"dead\":2,\"fghfgh\":1,\"sold' + (SELECT 0 FROM (SELECT SLEEP(28))qsqli_2222) + '\":1,\"sd\":1,\"not-available\":1,\"freaky\":12,\"not available\":4,\"Inactive\":1,\"sold\":258,\"slobodan\":1,\"" & char(0, 0, 136, 197) & char(0, 0, 144, 60) & char(0, 0, 78, 45) & "\":1,\"x-available\":10,\"manco\":1,\"status2\":1,\"jack\":1,\"availa1111ble\":2,\"101\":1,\"sasaad\":1,\"amet\":1,\"0.9\":1,\"x-sold\":2,\"consectetur\":1}"//omit
},
:
and decoded into an internal structure:
09:04:12.537107 HTTP_Test.ttcn:137 Decoded string {
JSON_object := {
{
key := "id",
val := {
JSON_integer := 1
}
},
{
key := "category",
val := {
JSON_object := {
{
key := "id",
val := {
JSON_integer := 1000
}
},
{
key := "name",
val := {
JSON_string := "adipiscing"
}
}
}
}
},
{
key := "name",
val := {
JSON_string := "sed"
}
},
{
key := "photoUrls",
val := {
JSON_array := {
{
JSON_string := "${photoUrls}"
},
{
JSON_string := "${photoUrls}"
}
}
}
},
{
key := "tags",
val := {
JSON_array := {
{
JSON_object := {
{
key := "id",
val := {
JSON_integer := 1000
}
},
{
key := "name",
val := {
JSON_string := "incididunt"
}
}
}
},
{
JSON_object := {
{
key := "id",
val := {
JSON_integer := 1000
}
},
{
key := "name",
val := {
JSON_string := "incididunt"
}
}
}
}
}
}
},
{
key := "status",
val := {
JSON_string := "labore"
}
}
}
}
Similarly, the JSON response provided by the SUT
:
11:05:30.751988 HTTP_Test.ttcn:423 "{\"id\":1,\"category\":{\"id\":1000,\"name\":\"adipiscing\"},\"name\":\"sed\",\"photoUrls\":[\"${photoUrls}\",\"${photoUrls}\"],\"tags\":[{\"id\":1000,\"name\":\"incididunt\"},{\"id\":1000,\"name\":\"incididunt\"}],\"status\":\"labore\"}"
:
is also decoded using a schema-less decoding
09:04:12.537206 HTTP_Test.ttcn:137 Decoded string {
JSON_object := {
{
key := "id",
val := {
JSON_integer := 1
}
},
{
key := "category",
val := {
JSON_object := {
{
key := "id",
val := {
JSON_integer := 0
}
},
{
key := "name",
val := {
JSON_string := "string"
}
}
}
}
},
{
key := "name",
val := {
JSON_string := "doggie"
}
},
{
key := "photoUrls",
val := {
JSON_array := {
{
JSON_string := "string"
}
}
}
},
{
key := "tags",
val := {
JSON_array := {
{
JSON_object := {
{
key := "id",
val := {
JSON_integer := 0
}
},
{
key := "name",
val := {
JSON_string := "string"
}
}
}
}
}
}
},
{
key := "status",
val := {
JSON_string := "available"
}
}
}
}
and the two matched.
(The actual responses may be different, as the SUT is open to additions/modifications).
This can be illustrated as below:
The point to remember is: when you dont't need to validate a JSON instance, schema-less decoding might come handy.
Best regards
Elemer
[Updated on: Wed, 20 March 2019 14:53] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri Apr 26 03:29:05 GMT 2024
Powered by FUDForum. Page generated in 0.03387 seconds
|