├── .gitignore ├── JSONListModel ├── JSONListModel.qmlproject ├── jsonData.txt ├── JSONListModel.qml ├── jsonpath.js └── example.qml ├── README └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | *.user 2 | -------------------------------------------------------------------------------- /JSONListModel/JSONListModel.qmlproject: -------------------------------------------------------------------------------- 1 | import QmlProject 1.1 2 | 3 | Project { 4 | mainFile: "example.qml" 5 | 6 | /* Include .qml, .js, and image files from current directory and subdirectories */ 7 | QmlFiles { 8 | directory: "." 9 | } 10 | JavaScriptFiles { 11 | directory: "." 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | A collection of utilities for QML 2 | 3 | Contents: 4 | 5 | *** JSONListModel 6 | 7 | A QML ListModel for JSON data. 8 | Features: 9 | - API matching with XMLListModel, for ease of use 10 | - file-based and string-based data 11 | - Automatic model roles from JSON element properties 12 | - Complex JSON document handling with JSONPath, XPath for JSON (http://goessner.net/articles/JsonPath/) 13 | - Advanced querying and filtering expressions 14 | 15 | -------------------------------------------------------------------------------- /JSONListModel/jsonData.txt: -------------------------------------------------------------------------------- 1 | { "store": { 2 | "book": [ 3 | { "category": "reference", 4 | "author": "Nigel Rees", 5 | "title": "Sayings of the Century", 6 | "price": 8.95 7 | }, 8 | { "category": "fiction", 9 | "author": "Evelyn Waugh", 10 | "title": "Sword of Honour", 11 | "price": 12.99 12 | }, 13 | { "category": "fiction", 14 | "author": "Herman Melville", 15 | "title": "Moby Dick", 16 | "isbn": "0-553-21311-3", 17 | "price": 8.99 18 | }, 19 | { "category": "fiction", 20 | "author": "J. R. R. Tolkien", 21 | "title": "The Lord of the Rings", 22 | "isbn": "0-395-19395-8", 23 | "price": 22.99 24 | } 25 | ], 26 | "bicycle": { 27 | "color": "red", 28 | "price": 19.95 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2007 Stefan Goessner (goessner.net) 2 | Copyright (c) 2012 Romain Pokrzywka (KDAB) (romain@kdab.com) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /JSONListModel/JSONListModel.qml: -------------------------------------------------------------------------------- 1 | /* JSONListModel - a QML ListModel with JSON and JSONPath support 2 | * 3 | * Copyright (c) 2012 Romain Pokrzywka (KDAB) (romain@kdab.com) 4 | * Licensed under the MIT licence (http://opensource.org/licenses/mit-license.php) 5 | */ 6 | 7 | import QtQuick 8 | import "jsonpath.js" as JSONPath 9 | 10 | Item { 11 | property string source: "" 12 | property string json: "" 13 | property string query: "" 14 | 15 | property ListModel model : ListModel { id: jsonModel } 16 | property alias count: jsonModel.count 17 | 18 | onSourceChanged: { 19 | var xhr = new XMLHttpRequest; 20 | xhr.open("GET", source); 21 | xhr.onreadystatechange = function() { 22 | if (xhr.readyState == XMLHttpRequest.DONE) 23 | json = xhr.responseText; 24 | } 25 | xhr.send(); 26 | } 27 | 28 | onJsonChanged: updateJSONModel() 29 | onQueryChanged: updateJSONModel() 30 | 31 | function updateJSONModel() { 32 | jsonModel.clear(); 33 | 34 | if ( json === "" ) 35 | return; 36 | 37 | var objectArray = parseJSONString(json, query); 38 | for ( var key in objectArray ) { 39 | var jo = objectArray[key]; 40 | jsonModel.append( jo ); 41 | } 42 | } 43 | 44 | function parseJSONString(jsonString, jsonPathQuery) { 45 | var objectArray = JSON.parse(jsonString); 46 | if ( jsonPathQuery !== "" ) 47 | objectArray = JSONPath.jsonPath(objectArray, jsonPathQuery); 48 | 49 | return objectArray; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /JSONListModel/jsonpath.js: -------------------------------------------------------------------------------- 1 | /* JSONPath 0.8.5 - XPath for JSON 2 | * 3 | * Copyright (c) 2007 Stefan Goessner (goessner.net) 4 | * Licensed under the MIT (MIT-LICENSE.txt) licence. 5 | * 6 | */ 7 | function jsonPath(obj, expr, arg) { 8 | var P = { 9 | resultType: arg && arg.resultType || "VALUE", 10 | result: [], 11 | normalize: function(expr) { 12 | var subx = []; 13 | return expr.replace(/[\['](\??\(.*?\))[\]']|\['(.*?)'\]/g, function($0,$1,$2){return "[#"+(subx.push($1||$2)-1)+"]";}) /* http://code.google.com/p/jsonpath/issues/detail?id=4 */ 14 | .replace(/'?\.'?|\['?/g, ";") 15 | .replace(/;;;|;;/g, ";..;") 16 | .replace(/;$|'?\]|'$/g, "") 17 | .replace(/#([0-9]+)/g, function($0,$1){return subx[$1];}); 18 | }, 19 | asPath: function(path) { 20 | var x = path.split(";"), p = "$"; 21 | for (var i=1,n=x.length; i