From 89ef515177fd5a0b5d95dcffd5fd0b0669e3625a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Thu, 5 Sep 2013 07:55:49 +0200 Subject: Add JSON parsing support to qmake. Add qjson* implementation files from corelib/json to the qmake build. Add a read-only compile mode, enabled by defining QT_JSON_READONLY. Add qmake built-in function parseJson(file, into) which parses a json file into the given variable. qmake uses a flat key -> value-list implementation for storing variables, which means that some hackery is need to represent arbitrarily nested JSON. Use a special "_KEYS_" variable for arrays and objects: Arrays: ["item1", "item2"] $${array._KEYS_} -> 0 1 2 $${array.0} -> "item1" $${array.1} -> "item2" Objects: { "key1" : "value1", "key2" : "value2" } $${object._KEYS_} -> key1 key2 $${object.key1} -> value1 $${object.key2} -> value2 Change-Id: I0aa2e4e4ae14fa25be8242bc16d3cffce32504d2 Reviewed-by: Lars Knoll --- tests/auto/tools/qmake/testdata/json/json.pro | 26 +++++++++++++++++++++++ tests/auto/tools/qmake/testdata/json/test.json | 9 ++++++++ tests/auto/tools/qmake/tst_qmake.cpp | 29 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/auto/tools/qmake/testdata/json/json.pro create mode 100644 tests/auto/tools/qmake/testdata/json/test.json (limited to 'tests/auto') diff --git a/tests/auto/tools/qmake/testdata/json/json.pro b/tests/auto/tools/qmake/testdata/json/json.pro new file mode 100644 index 0000000000..33440b3209 --- /dev/null +++ b/tests/auto/tools/qmake/testdata/json/json.pro @@ -0,0 +1,26 @@ +jsontext = $$cat($$PWD/test.json) +parseJson(jsontext, json) + +# print all keys +message(json._KEYS_ $${json._KEYS_}) + +# print array +message(json.array._KEYS_ $${json.array._KEYS_}) +for(key, json.array._KEYS_): \ + message(json.array.$${key} $$eval(json.array.$${key})) + +# print object +message(json.object._KEYS_ $${json.object._KEYS_}) +for(key, json.object._KEYS_): \ + message(json.object.$${key} $$eval(json.object.$${key})) + +# print value tyes +message(json.string: $${json.string}) +message(json.number: $${json.number}) +message(json.true: $${json.true}) +message(json.false: $${json.false}) +message(json.null: $${json.null}) + +# check that booleans work +$${json.true}: message(json.true is true) +!$${json.false}: message(json.false is false) diff --git a/tests/auto/tools/qmake/testdata/json/test.json b/tests/auto/tools/qmake/testdata/json/test.json new file mode 100644 index 0000000000..cc82908eba --- /dev/null +++ b/tests/auto/tools/qmake/testdata/json/test.json @@ -0,0 +1,9 @@ +{ + "array" : ["arrayItem1", "arrayItem2", "arrayItem3"], + "object" : { "key1" : "objectValue1", "key2" : "objectValue2" }, + "string" : "test string", + "number" : 999, + "true" : true, + "false" :false, + "null" : null +} diff --git a/tests/auto/tools/qmake/tst_qmake.cpp b/tests/auto/tools/qmake/tst_qmake.cpp index cf5c75a66b..87e86406b8 100644 --- a/tests/auto/tools/qmake/tst_qmake.cpp +++ b/tests/auto/tools/qmake/tst_qmake.cpp @@ -92,6 +92,7 @@ private slots: void substitutes(); void project(); void proFileCache(); + void json(); private: TestCompiler test_compiler; @@ -556,5 +557,33 @@ void tst_qmake::proFileCache() QVERIFY( test_compiler.qmake( workDir, "pro_file_cache" )); } +void tst_qmake::json() +{ + QString workDir = base_path + "/testdata/json"; + QVERIFY( test_compiler.qmake( workDir, "json.pro" )); + QString output = test_compiler.commandOutput(); + + // all keys + QVERIFY(output.contains("json._KEYS_ array false null number object string true")); + // array + QVERIFY(output.contains("json.array._KEYS_ 0 1 2")); + QVERIFY(output.contains("json.array.0 arrayItem1")); + QVERIFY(output.contains("json.array.1 arrayItem2")); + QVERIFY(output.contains("json.array.2 arrayItem3")); + // object + QVERIFY(output.contains("json.object._KEYS_ key1 key2")); + QVERIFY(output.contains("json.object.key1 objectValue1")); + QVERIFY(output.contains("json.object.key1 objectValue1")); + // value types + QVERIFY(output.contains("json.string: test string")); + QVERIFY(output.contains("json.number: 999")); + QVERIFY(output.contains("json.true: true")); + QVERIFY(output.contains("json.false: false")); + QVERIFY(output.contains("json.null:")); + // functional booleans + QVERIFY(output.contains("json.true is true")); + QVERIFY(output.contains("json.false is false")); +} + QTEST_MAIN(tst_qmake) #include "tst_qmake.moc" -- cgit v1.2.3