| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* @fileoverview Defines a class for parsing JSON using eval. |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.json.EvalJsonProcessor'); |
| 21 |
|
| 22 |
goog.require('goog.json'); |
| 23 |
goog.require('goog.json.Processor'); |
| 24 |
goog.require('goog.json.Serializer'); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* A class that parses and stringifies JSON using eval (as implemented in |
| 30 |
* goog.json). |
| 31 |
* Adapts {@code goog.json} to the {@code goog.json.Processor} interface. |
| 32 |
* |
| 33 |
* @param {?goog.json.Replacer=} opt_replacer An optional replacer to use during |
| 34 |
* serialization. |
| 35 |
* @param {?boolean=} opt_useUnsafeParsing Whether to skip validation before |
| 36 |
* evaluating. Safe parsing is very slow on large strings. On the other |
| 37 |
* hand, unsafe parsing uses eval() without checking whether the string is |
| 38 |
* valid, so it should only be used if you trust the source of the string. |
| 39 |
* @constructor |
| 40 |
* @implements {goog.json.Processor} |
| 41 |
* @final |
| 42 |
* @deprecated Use goog.json.NativeJsonProcessor. |
| 43 |
*/ |
| 44 |
goog.json.EvalJsonProcessor = function(opt_replacer, opt_useUnsafeParsing) { |
| 45 |
/** |
| 46 |
* @type {goog.json.Serializer} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
this.serializer_ = new goog.json.Serializer(opt_replacer); |
| 50 |
|
| 51 |
/** @private {boolean} */ |
| 52 |
this.useUnsafeParsing_ = opt_useUnsafeParsing || false; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
/** @override */ |
| 57 |
goog.json.EvalJsonProcessor.prototype.stringify = function(object) { |
| 58 |
return this.serializer_.serialize(object); |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
/** @override */ |
| 63 |
goog.json.EvalJsonProcessor.prototype.parse = function(s) { |
| 64 |
if (this.useUnsafeParsing_) { |
| 65 |
return eval('(' + s + ')'); |
| 66 |
} |
| 67 |
return goog.json.parse(s); |
| 68 |
}; |