Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Unified Diff: src/d8.js

Issue 283943002: Remove custom JSON implementation from d8. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/d8.js
diff --git a/src/d8.js b/src/d8.js
index 0c1f32dc8d281c4336a09a8675a62a48812e83ff..3f7832dd00b19e5ff0113aff9054faf43b6e4fc5 100644
--- a/src/d8.js
+++ b/src/d8.js
@@ -503,7 +503,7 @@ RequestPacket.prototype.toJSONProtocol = function() {
json += '"seq":' + this.seq;
json += ',"type":"' + this.type + '"';
if (this.command) {
- json += ',"command":' + StringToJSON_(this.command);
+ json += ',"command":' + JSON.stringify(this.command);
}
if (this.arguments) {
json += ',"arguments":';
@@ -511,7 +511,7 @@ RequestPacket.prototype.toJSONProtocol = function() {
if (this.arguments.toJSONProtocol) {
json += this.arguments.toJSONProtocol();
} else {
- json += SimpleObjectToJSON_(this.arguments);
+ json += JSON.stringify(this.arguments);
}
}
json += '}';
@@ -1965,214 +1965,6 @@ ProtocolReference.prototype.handle = function() {
};
-function MakeJSONPair_(name, value) {
- return '"' + name + '":' + value;
-}
-
-
-function ArrayToJSONObject_(content) {
- return '{' + content.join(',') + '}';
-}
-
-
-function ArrayToJSONArray_(content) {
- return '[' + content.join(',') + ']';
-}
-
-
-function BooleanToJSON_(value) {
- return String(value);
-}
-
-
-function NumberToJSON_(value) {
- return String(value);
-}
-
-
-// Mapping of some control characters to avoid the \uXXXX syntax for most
-// commonly used control cahracters.
-var ctrlCharMap_ = {
- '\b': '\\b',
- '\t': '\\t',
- '\n': '\\n',
- '\f': '\\f',
- '\r': '\\r',
- '"' : '\\"',
- '\\': '\\\\'
-};
-
-
-// Regular expression testing for ", \ and control characters (0x00 - 0x1F).
-var ctrlCharTest_ = new RegExp('["\\\\\x00-\x1F]');
-
-
-// Regular expression matching ", \ and control characters (0x00 - 0x1F)
-// globally.
-var ctrlCharMatch_ = new RegExp('["\\\\\x00-\x1F]', 'g');
-
-
-/**
- * Convert a String to its JSON representation (see http://www.json.org/). To
- * avoid depending on the String object this method calls the functions in
- * string.js directly and not through the value.
- * @param {String} value The String value to format as JSON
- * @return {string} JSON formatted String value
- */
-function StringToJSON_(value) {
- // Check for" , \ and control characters (0x00 - 0x1F). No need to call
- // RegExpTest as ctrlchar is constructed using RegExp.
- if (ctrlCharTest_.test(value)) {
- // Replace ", \ and control characters (0x00 - 0x1F).
- return '"' +
- value.replace(ctrlCharMatch_, function (char) {
- // Use charmap if possible.
- var mapped = ctrlCharMap_[char];
- if (mapped) return mapped;
- mapped = char.charCodeAt();
- // Convert control character to unicode escape sequence.
- return '\\u00' +
- '0' + // TODO %NumberToRadixString(Math.floor(mapped / 16), 16) +
- '0'; // TODO %NumberToRadixString(mapped % 16, 16)
- })
- + '"';
- }
-
- // Simple string with no special characters.
- return '"' + value + '"';
-}
-
-
-/**
- * Convert a Date to ISO 8601 format. To avoid depending on the Date object
- * this method calls the functions in date.js directly and not through the
- * value.
- * @param {Date} value The Date value to format as JSON
- * @return {string} JSON formatted Date value
- */
-function DateToISO8601_(value) {
- var f = function(n) {
- return n < 10 ? '0' + n : n;
- };
- var g = function(n) {
- return n < 10 ? '00' + n : n < 100 ? '0' + n : n;
- };
- return builtins.GetUTCFullYearFrom(value) + '-' +
- f(builtins.GetUTCMonthFrom(value) + 1) + '-' +
- f(builtins.GetUTCDateFrom(value)) + 'T' +
- f(builtins.GetUTCHoursFrom(value)) + ':' +
- f(builtins.GetUTCMinutesFrom(value)) + ':' +
- f(builtins.GetUTCSecondsFrom(value)) + '.' +
- g(builtins.GetUTCMillisecondsFrom(value)) + 'Z';
-}
-
-
-/**
- * Convert a Date to ISO 8601 format. To avoid depending on the Date object
- * this method calls the functions in date.js directly and not through the
- * value.
- * @param {Date} value The Date value to format as JSON
- * @return {string} JSON formatted Date value
- */
-function DateToJSON_(value) {
- return '"' + DateToISO8601_(value) + '"';
-}
-
-
-/**
- * Convert an Object to its JSON representation (see http://www.json.org/).
- * This implementation simply runs through all string property names and adds
- * each property to the JSON representation for some predefined types. For type
- * "object" the function calls itself recursively unless the object has the
- * function property "toJSONProtocol" in which case that is used. This is not
- * a general implementation but sufficient for the debugger. Note that circular
- * structures will cause infinite recursion.
- * @param {Object} object The object to format as JSON
- * @return {string} JSON formatted object value
- */
-function SimpleObjectToJSON_(object) {
- var content = [];
- for (var key in object) {
- // Only consider string keys.
- if (typeof key == 'string') {
- var property_value = object[key];
-
- // Format the value based on its type.
- var property_value_json;
- switch (typeof property_value) {
- case 'object':
- if (IS_NULL(property_value)) {
- property_value_json = 'null';
- } else if (typeof property_value.toJSONProtocol == 'function') {
- property_value_json = property_value.toJSONProtocol(true);
- } else if (property_value.constructor.name == 'Array'){
- property_value_json = SimpleArrayToJSON_(property_value);
- } else {
- property_value_json = SimpleObjectToJSON_(property_value);
- }
- break;
-
- case 'boolean':
- property_value_json = BooleanToJSON_(property_value);
- break;
-
- case 'number':
- property_value_json = NumberToJSON_(property_value);
- break;
-
- case 'string':
- property_value_json = StringToJSON_(property_value);
- break;
-
- default:
- property_value_json = null;
- }
-
- // Add the property if relevant.
- if (property_value_json) {
- content.push(StringToJSON_(key) + ':' + property_value_json);
- }
- }
- }
-
- // Make JSON object representation.
- return '{' + content.join(',') + '}';
-}
-
-
-/**
- * Convert an array to its JSON representation. This is a VERY simple
- * implementation just to support what is needed for the debugger.
- * @param {Array} arrya The array to format as JSON
- * @return {string} JSON formatted array value
- */
-function SimpleArrayToJSON_(array) {
- // Make JSON array representation.
- var json = '[';
- for (var i = 0; i < array.length; i++) {
- if (i != 0) {
- json += ',';
- }
- var elem = array[i];
- if (elem.toJSONProtocol) {
- json += elem.toJSONProtocol(true);
- } else if (typeof(elem) === 'object') {
- json += SimpleObjectToJSON_(elem);
- } else if (typeof(elem) === 'boolean') {
- json += BooleanToJSON_(elem);
- } else if (typeof(elem) === 'number') {
- json += NumberToJSON_(elem);
- } else if (typeof(elem) === 'string') {
- json += StringToJSON_(elem);
- } else {
- json += elem;
- }
- }
- json += ']';
- return json;
-}
-
-
// A more universal stringify that supports more types than JSON.
// Used by the d8 shell to output results.
var stringifyDepthLimit = 4; // To avoid crashing on cyclic objects
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698