evaljsonprocessor.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * @fileoverview Defines a class for parsing JSON using eval.
  16. */
  17. goog.provide('goog.json.EvalJsonProcessor');
  18. goog.require('goog.json');
  19. goog.require('goog.json.Processor');
  20. goog.require('goog.json.Serializer');
  21. /**
  22. * A class that parses and stringifies JSON using eval (as implemented in
  23. * goog.json).
  24. * Adapts {@code goog.json} to the {@code goog.json.Processor} interface.
  25. *
  26. * @param {?goog.json.Replacer=} opt_replacer An optional replacer to use during
  27. * serialization.
  28. * @param {?boolean=} opt_useUnsafeParsing Whether to use goog.json.unsafeParse
  29. * for parsing. Safe parsing is very slow on large strings. On the other
  30. * hand, unsafe parsing uses eval() without checking whether the string is
  31. * valid, so it should only be used if you trust the source of the string.
  32. * @constructor
  33. * @implements {goog.json.Processor}
  34. * @final
  35. * @deprecated Use goog.json.NativeJsonProcessor.
  36. */
  37. goog.json.EvalJsonProcessor = function(opt_replacer, opt_useUnsafeParsing) {
  38. /**
  39. * @type {goog.json.Serializer}
  40. * @private
  41. */
  42. this.serializer_ = new goog.json.Serializer(opt_replacer);
  43. /**
  44. * @type {function(string): *}
  45. * @private
  46. */
  47. this.parser_ = opt_useUnsafeParsing ? goog.json.unsafeParse : goog.json.parse;
  48. };
  49. /** @override */
  50. goog.json.EvalJsonProcessor.prototype.stringify = function(object) {
  51. return this.serializer_.serialize(object);
  52. };
  53. /** @override */
  54. goog.json.EvalJsonProcessor.prototype.parse = function(s) {
  55. return this.parser_(s);
  56. };