1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.provide("dojo.data.old.format.Json");
|
|
|
12 |
dojo.require("dojo.lang.assert");
|
|
|
13 |
dojo.data.old.format.Json = new function () {
|
|
|
14 |
this.loadDataProviderFromFileContents = function (dataProvider, jsonFileContents) {
|
|
|
15 |
dojo.lang.assertType(dataProvider, dojo.data.old.provider.Base);
|
|
|
16 |
dojo.lang.assertType(jsonFileContents, String);
|
|
|
17 |
var arrayOfJsonData = eval("(" + jsonFileContents + ")");
|
|
|
18 |
this.loadDataProviderFromArrayOfJsonData(dataProvider, arrayOfJsonData);
|
|
|
19 |
};
|
|
|
20 |
this.loadDataProviderFromArrayOfJsonData = function (dataProvider, arrayOfJsonData) {
|
|
|
21 |
dojo.lang.assertType(arrayOfJsonData, Array, {optional:true});
|
|
|
22 |
if (arrayOfJsonData && (arrayOfJsonData.length > 0)) {
|
|
|
23 |
var firstRow = arrayOfJsonData[0];
|
|
|
24 |
dojo.lang.assertType(firstRow, [Array, "pureobject"]);
|
|
|
25 |
if (dojo.lang.isArray(firstRow)) {
|
|
|
26 |
_loadDataProviderFromArrayOfArrays(dataProvider, arrayOfJsonData);
|
|
|
27 |
} else {
|
|
|
28 |
dojo.lang.assertType(firstRow, "pureobject");
|
|
|
29 |
_loadDataProviderFromArrayOfObjects(dataProvider, arrayOfJsonData);
|
|
|
30 |
}
|
|
|
31 |
}
|
|
|
32 |
};
|
|
|
33 |
this.getJsonStringFromResultSet = function (resultSet) {
|
|
|
34 |
dojo.unimplemented("dojo.data.old.format.Json.getJsonStringFromResultSet");
|
|
|
35 |
var jsonString = null;
|
|
|
36 |
return jsonString;
|
|
|
37 |
};
|
|
|
38 |
function _loadDataProviderFromArrayOfArrays(dataProvider, arrayOfJsonData) {
|
|
|
39 |
var arrayOfKeys = arrayOfJsonData[0];
|
|
|
40 |
for (var i = 1; i < arrayOfJsonData.length; ++i) {
|
|
|
41 |
var row = arrayOfJsonData[i];
|
|
|
42 |
var item = dataProvider.getNewItemToLoad();
|
|
|
43 |
for (var j in row) {
|
|
|
44 |
var value = row[j];
|
|
|
45 |
var key = arrayOfKeys[j];
|
|
|
46 |
item.load(key, value);
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
function _loadDataProviderFromArrayOfObjects(dataProvider, arrayOfJsonData) {
|
|
|
51 |
for (var i in arrayOfJsonData) {
|
|
|
52 |
var row = arrayOfJsonData[i];
|
|
|
53 |
var item = dataProvider.getNewItemToLoad();
|
|
|
54 |
for (var key in row) {
|
|
|
55 |
var value = row[key];
|
|
|
56 |
if (dojo.lang.isArray(value)) {
|
|
|
57 |
var arrayOfValues = value;
|
|
|
58 |
for (var j in arrayOfValues) {
|
|
|
59 |
value = arrayOfValues[j];
|
|
|
60 |
item.load(key, value);
|
|
|
61 |
}
|
|
|
62 |
} else {
|
|
|
63 |
item.load(key, value);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
}();
|
|
|
69 |
|