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

Side by Side Diff: Source/devtools/front_end/network/RequestJSONView.js

Issue 654083006: DevTools: NetworkPanel: use optimistic JSON parser for response preview. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 2 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2011 Google Inc. All rights reserved. 2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 24 matching lines...) Expand all
35 * @param {!WebInspector.ParsedJSON} parsedJSON 35 * @param {!WebInspector.ParsedJSON} parsedJSON
36 */ 36 */
37 WebInspector.RequestJSONView = function(request, parsedJSON) 37 WebInspector.RequestJSONView = function(request, parsedJSON)
38 { 38 {
39 WebInspector.RequestView.call(this, request); 39 WebInspector.RequestView.call(this, request);
40 this._parsedJSON = parsedJSON; 40 this._parsedJSON = parsedJSON;
41 this.element.classList.add("json"); 41 this.element.classList.add("json");
42 } 42 }
43 43
44 /** 44 /**
45 * @return {*}
46 */
47 WebInspector.RequestJSONView.buildJSON = function(text)
vsevik 2014/10/16 06:51:35 buildObjectFromJSON
eustas 2014/10/16 14:14:56 Done.
48 {
49 // Based on https://code.google.com/p/json-sans-eval/
50
51 var jsonToken = new RegExp('(?:false|true|null|[,\\{\\}\\[\\]]|(?:-?\\b(?:0| [1-9][0-9]*)(?:\\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\\b)|(?:\"(?:[^\\0-\\x08\\x0a-\\x1 f\"\\\\]|\\\\(?:[\"/\\\\bfnrt]|u[0-9A-Fa-f]{4}))*\"))', 'g');
52 var escapeSequence = new RegExp('\\\\(?:([^u])|u(.{4}))', 'g');
53 var escapes = {'"': '"', '/': '/', '\\': '\\', 'b': '\b', 'f': '\f', 'n': '\ n', 'r': '\r', 't': '\t'};
54
55 function unescapeOne(_, ch, hex)
56 {
57 return ch ? escapes[ch] : String.fromCharCode(parseInt(hex, 16));
58 }
59
60 var emptyString = new String('');
61 var slash = '\\';
62 var toks = text.match(jsonToken);
63 var result = [];
64 var tok = toks[0];
65 var topLevelPrimitive = false;
66 if ('{' === tok)
67 result = {};
68 else if ('[' !== tok)
69 topLevelPrimitive = true;
70
71 var key;
72 var noEntry = true;
73 var stack = [result];
74 for (var i = 1 - topLevelPrimitive, n = toks.length; i < n; ++i) {
75 tok = toks[i];
76 var cont;
77 var wasNoEntry = noEntry;
78 noEntry = false;
79 switch (tok.charCodeAt(0)) {
80 default: // sign or digit
81 cont = stack[0];
82 cont[key || cont.length] = +(tok);
83 key = void 0;
84 break;
85 case 0x22: // "
86 tok = tok.substring(1, tok.length - 1);
87 if (tok.indexOf(slash) !== -1)
88 tok = tok.replace(escapeSequence, unescapeOne);
89 cont = stack[0];
90 if (!key) {
91 if (cont instanceof Array) {
92 key = cont.length;
93 } else {
94 key = tok || emptyString;
95 break;
96 }
97 }
98 cont[key] = tok;
99 key = void 0;
100 break;
101 case 0x2C: // "
102 cont = stack[0];
103 if (wasNoEntry && (cont instanceof Array))
104 cont[key || cont.length] = undefined;
105 noEntry = true;
106 break;
107 case 0x5b: // [
108 cont = stack[0];
109 stack.unshift(cont[key || cont.length] = []);
110 key = void 0;
111 noEntry = true;
112 break;
113 case 0x5d: // ]
114 stack.shift();
115 break;
116 case 0x66: // f
117 cont = stack[0];
118 cont[key || cont.length] = false;
119 key = void 0;
120 break;
121 case 0x6e: // n
122 cont = stack[0];
123 cont[key || cont.length] = null;
124 key = void 0;
125 break;
126 case 0x74: // t
127 cont = stack[0];
128 cont[key || cont.length] = true;
129 key = void 0;
130 break;
131 case 0x7b: // {
132 cont = stack[0];
133 stack.unshift(cont[key || cont.length] = {});
134 key = void 0;
135 break;
136 case 0x7d: // }
137 stack.shift();
138 break;
139 }
140 }
141 if (topLevelPrimitive) {
142 if (stack.length !== 1)
143 throw new Error();
144 result = result[0];
145 } else {
146 if (stack.length)
147 throw new Error();
148 }
149 return result;
150 }
151
152 /**
45 * @param {string} text 153 * @param {string} text
46 * @return {?WebInspector.ParsedJSON} 154 * @return {?WebInspector.ParsedJSON}
47 */ 155 */
48 WebInspector.RequestJSONView.parseJSON = function(text) 156 WebInspector.RequestJSONView.parseJSON = function(text)
49 { 157 {
50 var prefix = ""; 158 var prefix = "";
51 159
52 // Trim while(1), for(;;), weird numbers, etc. We need JSON start. 160 // Trim while(1), for(;;), weird numbers, etc. We need JSON start.
53 var start = /[{[]/.exec(text); 161 var start = /[{[]/.exec(text);
54 if (start && start.index) { 162 if (start && start.index) {
55 prefix = text.substring(0, start.index); 163 prefix = text.substring(0, start.index);
56 text = text.substring(start.index); 164 text = text.substring(start.index);
57 } 165 }
58 166
59 try { 167 try {
60 return new WebInspector.ParsedJSON(JSON.parse(text), prefix, ""); 168 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView.buildJSO N(text), prefix, "");
vsevik 2014/10/16 06:51:34 We should make sure this returns correct suffix
eustas 2014/10/16 14:14:56 Done.
61 } catch (e) { 169 } catch (e) {
62 return null; 170 return null;
63 } 171 }
64 } 172 }
65 173
66 /** 174 /**
67 * @param {string} text 175 * @param {string} text
68 * @return {?WebInspector.ParsedJSON} 176 * @return {?WebInspector.ParsedJSON}
69 */ 177 */
70 WebInspector.RequestJSONView.parseJSONP = function(text) 178 WebInspector.RequestJSONView.parseJSONP = function(text)
vsevik 2014/10/16 06:51:34 So this one is not needed anymore?
eustas 2014/10/16 14:14:56 Yup. Removed.
71 { 179 {
72 // Taking everything between first and last parentheses 180 // Taking everything between first and last parentheses
73 var start = text.indexOf("("); 181 var start = text.indexOf("(");
74 var end = text.lastIndexOf(")"); 182 var end = text.lastIndexOf(")");
75 if (start == -1 || end == -1 || end < start) 183 if (start == -1 || end == -1 || end < start)
76 return null; 184 return null;
77 185
78 var prefix = text.substring(0, start + 1); 186 var prefix = text.substring(0, start + 1);
79 var suffix = text.substring(end); 187 var suffix = text.substring(end);
80 text = text.substring(start + 1, end); 188 text = text.substring(start + 1, end);
81 189
82 try { 190 try {
83 return new WebInspector.ParsedJSON(JSON.parse(text), prefix, suffix); 191 return new WebInspector.ParsedJSON(WebInspector.RequestJSONView.buildJSO N(text), prefix, suffix);
84 } catch (e) { 192 } catch (e) {
85 return null; 193 return null;
86 } 194 }
87 } 195 }
88 196
89 WebInspector.RequestJSONView.prototype = { 197 WebInspector.RequestJSONView.prototype = {
90 wasShown: function() 198 wasShown: function()
91 { 199 {
92 this._initialize(); 200 this._initialize();
93 }, 201 },
(...skipping 17 matching lines...) Expand all
111 219
112 /** 220 /**
113 * @constructor 221 * @constructor
114 */ 222 */
115 WebInspector.ParsedJSON = function(data, prefix, suffix) 223 WebInspector.ParsedJSON = function(data, prefix, suffix)
116 { 224 {
117 this.data = data; 225 this.data = data;
118 this.prefix = prefix; 226 this.prefix = prefix;
119 this.suffix = suffix; 227 this.suffix = suffix;
120 } 228 }
OLDNEW
« 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