dataschema-json-debug.js revision 002fb6dfb3c0f32b2be7b241465918b1b520a323
/**
* Provides a DataSchema implementation which can be used to work with JSON data.
*
* @module dataschema
* @submodule dataschema-json
*/
/**
* JSON subclass for the DataSchema Utility.
* @class DataSchema.JSON
* @extends DataSchema.Base
* @static
*/
// TODO: I don't think the calls to Base.* need to be done via Base since
// Base is mixed into SchemaJSON. Investigate for later.
SchemaJSON = {
/////////////////////////////////////////////////////////////////////////////
//
// DataSchema.JSON static methods
//
/////////////////////////////////////////////////////////////////////////////
/**
* Utility function converts JSON locator strings into walkable paths
*
* @method DataSchema.JSON.getPath
* @param locator {String} JSON value locator.
* @return {String[]} Walkable path to data value.
* @static
*/
var path = null,
keys = [],
i = 0;
if (locator) {
// Strip the ["string keys"] and [1] array indexes
// TODO: the first two steps can probably be reduced to one with
// /\[\s*(['"])?(.*?)\1\s*\]/g, but the array indices would be
// stored as strings. This is not likely an issue.
replace(/\[\s*(['"])(.*?)\1\s*\]/g,
function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
replace(/\[(\d+)\]/g,
function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
replace(/^\./,''); // remove leading dot
// Validate against problematic characters.
// should be safe. I'm not sure what makes a locator invalid.
//if (!/[^\w\.\$@]/.test(locator)) {
}
}
/*}
else {
Y.log("Invalid locator: " + locator, "error", "dataschema-json");
}
*/
}
return path;
},
/**
* Utility function to walk a path and return the value located there.
*
* @method DataSchema.JSON.getLocationValue
* @param path {String[]} Locator path.
* @param data {String} Data to traverse.
* @return {Object} Data value at location.
* @static
*/
var i = 0,
for (;i<len;i++) {
} else {
break;
}
}
return data;
},
/**
* Applies a given schema to given JSON data.
*
* @method apply
* @param schema {Object} Schema to apply.
* @param data {Object} JSON data.
* @return {Object} Schema-parsed data.
* @static
*/
// Convert incoming JSON strings
try {
}
catch(e) {
return data_out;
}
}
// Parse results data
// Parse meta data
}
}
else {
Y.log("JSON data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-json");
}
return data_out;
},
/**
* Schema-parsed list of results from full data
*
* @method _parseResults
* @param schema {Object} Schema to parse against.
* @param json_in {Object} JSON to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
// Fall back to treat resultListLocator as a simple key
// Or if no resultListLocator is supplied, use the input
// if no result fields are passed in, then just take
// the results array whole-hog Sometimes you're getting
// an array of strings, or want the whole object, so
// resultFields don't make sense.
} else {
}
} else if (schema.resultListLocator) {
}
return data_out;
},
/**
* Get field data values out of list of full results
*
* @method _getFieldValues
* @param fields {Array} Fields to find.
* @param array_in {Array} Results to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Parsed data object.
* @static
* @protected
*/
var results = [],
i, j,
// First collect hashes of simple paths, complex paths, and parsers
for (i=0; i<len; i++) {
// Validate and store locators for later
if (path) {
});
} else {
});
}
} else {
}
// Validate and store parsers for later
//TODO: use Y.DataSchema.parse?
if (parser) {
});
}
}
// Traverse list of array_in, creating records of simple fields,
// complex fields, and applying parsers as necessary
record = {};
if(result) {
// Cycle through complexLocators
path = complexPaths[j];
// Fail over keys like "foo.bar" from nested parsing
// to single token parsing if a value is found in
// results["foo.bar"]
});
// Don't try to process the path as complex
// for further results
continue;
}
}
}
// Cycle through simpleLocators
path = simplePaths[j];
// Bug 1777850: The result might be an array instead of object
}
// Cycle through fieldParsers
// Safety net
}
}
}
}
return data_out;
},
/**
* Parses results data according to schema
*
* @method _parseMeta
* @param metaFields {Object} Metafields definitions.
* @param json_in {Object} JSON to parse.
* @param data_out {Object} In-progress parsed data to update.
* @return {Object} Schema-parsed meta data.
* @static
* @protected
*/
if (isObject(metaFields)) {
for(key in metaFields) {
}
}
}
}
else {
}
return data_out;
}
};
// TODO: Y.Object + mix() might be better here