Commits

Kirill Simonov committed 632800a

Added support for new JSON output format.

Comments (0)

Files changed (1)

src/jquery.htraf.js

     }
     :
     function(data) {
-        return {
-            headers: data.meta,
-            data: data.data
-        };
+        if (data.meta instanceof Array) {
+            return {
+                headers: data.meta,
+                data: data.data
+            };
+        }
+        else {
+            function _toMeta(domain, title) {
+                if (domain.type == 'list') {
+                    return _toMeta(domain.item.domain, title)
+                }
+                else if (domain.type == 'record') {
+                    var headers = [];
+                    for (var k = 0; k < domain.fields.length; k ++) {
+                        var field = domain.fields[k];
+                        headers = headers.concat(_toMeta(field.domain, field.header));
+                    }
+                    return headers;
+                }
+                else {
+                    title = title || "";
+                    domain = domain.type;
+                    if (domain == 'integer' || domain == 'float' || domain == 'decimal') {
+                        domain = 'number';
+                    }
+                    return [{ title: title, domain: domain }];
+                }
+            }
+
+            function _toRecord(domain, data) {
+                if (domain.type == 'list') {
+                    data = (data && data.length > 0) ? data[0] : null;
+                    return _toRecord(domain.item.domain, data[0]);
+                }
+                else if (domain.type == 'record') {
+                    var record = [];
+                    for (var k = 0; k < domain.fields.length; k ++) {
+                        var field = domain.fields[k];
+                        record = record.concat(_toRecord(field.domain, data ? data[k] : null));
+                    }
+                    return record;
+                }
+                else {
+                    return [data];
+                }
+            }
+
+            function _toList(domain, data) {
+                if (domain.type == 'list') {
+                    var list = [];
+                    if (data) {
+                        for (var k = 0; k < data.length; k ++) {
+                            list.push(_toRecord(domain.item.domain, data[k]));
+                        }
+                    }
+                    return list;
+                }
+                else {
+                    return [_toRecord(domain.item.domain, data)];
+                }
+            }
+
+            return {
+                headers: _toMeta(data.meta.domain, data.meta.header),
+                data: _toList(data.meta.domain, data.data)
+            };
+        }
     };
 
 HTRAF.htsqlFormatter = HTRAF.htsqlVersion == 1 ? 'jsonex':':json';