SCA

SDO JSON Helper API examples

test_sdoJSONLoad.c
Loading JSON
test_sdoJSONLoad_invalidjson.c
Loading invalid JSON
test_sdoJSONSave.c
Saving JSON
test_sdoJSONLoad_withspacesinjsonkey.c
Shows that JSON support will happily load data objects nodes that can't be serialized into valid XML (eg. using spaces in json hash keys)
test_sdoJSONLoad_jsonarraytest.c
Shows that using a JSON array as value to a top-level JSON object can't be mapped by the "mapped" JSON convention even though it's valid JSON
test_sdoJSONLoad_jsonarraytest2.c
Wrapping the array into an additional JSON object OTOH works just fine; when the implementation's behaviour to add soap:encoding array attribute to the element is enabled, however, this will crash on shudown (but we disable/remove it for pmsca anyway)
test_sdoJSONLoad_jsonarrayroundtrip.c
Like before, but serializing back into JSON
test_sdoJSONLoad_empty.c
(Invalid) empty JSON
test_sdoJSONLoad_emptyobject.c
(Invalid) empty JSON dictionary literal
test_sdoJSONLoad_emptyobjectconst.c
(Unsupported) empty-dictionary constant
test_sdoJSONLoad_emptyarray.c
(Invalid) empty array literal
test_sdoJSONLoad_emptyarrayconst.c
(Unsupported) empty array constant
test_sdoJSONLoad_nestedemptyarray.c
(Unsupported) empty array literal as sub-item
test_sdoJSONLoad_nestedemptyarrayconst.c
(Unsupported) empty array constant as sub-item
test_sdoJSONLoad_toplevelarray.c
(Unsupported) top-level array literals (only dictionaries are supported at top leve)

test_sdoJSONLoad.c

#include "SDO.h"
#include "assert.h"


// Loading JSON
void test_sdoJSONLoad() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	sdoJSONLoad(doc, "{ \"test\": { \"a\" : \"hello\" } }");
	char buffer[100];
	sdoSave(doc, buffer, 0, 0);
	//fprintf(stderr, "testsdoJSONLoad: buffer=%s\n", buffer);
	assert(!strcmp("<test><a>hello</a></test>", buffer));
}

test_sdoJSONLoad_invalidjson.c

#include "SDO.h"
#include "assert.h"


// Loading invalid JSON
void test_sdoJSONLoad_invalidjson() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{ \"test\": { \"a\" : \"hello\" }"));
}

test_sdoJSONSave.c

#include "SDO.h"
#include "assert.h"


// Saving JSON
void test_sdoJSONSave() {
	SDOFACTORY df = getDataFactory();
	SDOXML xmlhlp = getXMLHelper(df);
	SDOXMLDOC doc = XMLDocAlloc(xmlhlp, 0, 0, "tmp");
	sdoXMLLoad(doc, "<test><a>hello</a></test>");
	SDOJSON jsonhlp = getJSONHelper(df);
	char *buf;
	size_t len;
	sdoJSONSave(getXMLRootDataObject(doc), &buf, &len);
	//fprintf(stderr, "test_sdoJSONSave: buf=%s", buf);
	assert(!strcmp("{\"test\":{\"a\":\"hello\"}}", buf));
}

test_sdoJSONLoad_withspacesinjsonkey.c

#include "SDO.h"
#include "assert.h"


// Shows that JSON support will happily load data objects
// nodes that can't be serialized into valid XML
// (eg. using spaces in json hash keys)
void test_sdoJSONLoad_withspacesinjsonkey() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	sdoJSONLoad(doc, "{ \"test\": { \"a key\" : \"hello\" } }");
	char buffer[100];
	sdoSave(doc, buffer, 0, 0);
	//fprintf(stderr, "testsdoJSONLoad_withspacesinjsonkey: buffer=%s\n", buffer);
	// note this contains/will output invalid XML
	assert(!strcmp("<test><a key>hello</a key></test>", buffer));
}

test_sdoJSONLoad_jsonarraytest.c

#include "SDO.h"
#include "assert.h"


// Shows that using a JSON array as value to a top-level
// JSON object can't be mapped by the "mapped" JSON convention
// even though it's valid JSON
void test_sdoJSONLoad_jsonarraytest() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{ \"test\": [ \"a\", \"b\", \"c\" ] }"));
}

test_sdoJSONLoad_jsonarraytest2.c

#include "SDO.h"
#include "assert.h"


// Wrapping the array into an additional JSON object OTOH
// works just fine; when the implementation's behaviour to
// add soap:encoding array attribute to the element is enabled,
// however, this will crash on shudown (but we
// disable/remove it for pmsca anyway)
void test_sdoJSONLoad_jsonarraytest2() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(sdoJSONLoad(doc, "{ \"test\": { \"wrapper\": [ \"a\", \"b\", \"c\" ] } }"));
	char buffer[100];
	sdoSave(doc, buffer, 0, 0);
	//fprintf(stderr, "testsdoJSONLoad_jsonarraytest2: buffer=%s\n", buffer);
	assert(!strcmp("<test><wrapper><item>a</item><item>b</item><item>c</item></wrapper></test>", buffer));
}

test_sdoJSONLoad_jsonarrayroundtrip.c

#include "SDO.h"
#include "assert.h"


// Like before, but serializing back into JSON 
void test_sdoJSONLoad_jsonarrayroundtrip() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(sdoJSONLoad(doc, "{ \"test\": { \"wrapper\": [ \"a\", \"b\", \"c\" ] } }"));
	char *buf;
	size_t len;
	sdoJSONSave(getXMLRootDataObject(doc), &buf, &len);
	//fprintf(stderr, "testsdoJSONLoad_jsonarrayroundtrip: buf=%s\n", buf);
	assert(!strcmp("{\"test\":{\"wrapper\":{\"item\":[\"a\",\"b\",\"c\"]}}}",
buf));
}

test_sdoJSONLoad_empty.c

#include "SDO.h"
#include "assert.h"


// (Invalid) empty JSON
void test_sdoJSONLoad_empty() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, ""));
}

test_sdoJSONLoad_emptyobject.c

#include "SDO.h"
#include "assert.h"


// (Invalid) empty JSON dictionary literal
void test_sdoJSONLoad_emptyobject() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{ }"));
}

test_sdoJSONLoad_emptyobjectconst.c

#include "SDO.h"
#include "assert.h"


// (Unsupported) empty-dictionary constant
void test_sdoJSONLoad_emptyobjectconst() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{}"));
}

test_sdoJSONLoad_emptyarray.c

#include "SDO.h"
#include "assert.h"


// (Invalid) empty array literal
void test_sdoJSONLoad_emptyarray() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "[ ]"));
}

test_sdoJSONLoad_emptyarrayconst.c

#include "SDO.h"
#include "assert.h"


// (Unsupported) empty array constant
void test_sdoJSONLoad_emptyarrayconst() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "[]"));
}

test_sdoJSONLoad_nestedemptyarray.c

#include "SDO.h"
#include "assert.h"


// (Unsupported) empty array literal as sub-item
void test_sdoJSONLoad_nestedemptyarray() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{ \"a\": [ ] }"));
}

test_sdoJSONLoad_nestedemptyarrayconst.c

#include "SDO.h"
#include "assert.h"


// (Unsupported) empty array constant as sub-item
void test_sdoJSONLoad_nestedemptyarrayconst() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "{ \"a\": [] }"));
	/* axis2c-unofficial won't handle empty json arrays eg.
	assert(sdoJSONLoad(doc, "{ \"a\": [] }"));
	char buffer[100];
	sdoSave(doc, buffer, 0, 0);
	fprintf(stderr, "test_sdoJSONLoad_nestedempyarrayconst: buffer=%s\n", buffer);
	*/
}

test_sdoJSONLoad_toplevelarray.c

#include "SDO.h"
#include "assert.h"


// (Unsupported) top-level array literals (only dictionaries are
// supported at top leve)
void test_sdoJSONLoad_toplevelarray() {
	SDOFACTORY df = getDataFactory();
	SDOJSON jsonhlp = getJSONHelper(df);
	SDOJSONDOC doc = JSONDocAlloc(jsonhlp, 0, 0, "tmp");
	assert(!sdoJSONLoad(doc, "[ \"a\", \"b\", \"c\" ]"));
}