aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/json/nativejsonprocessor.js
blob: 2e919fa4a2a5648df1fcc70cb7bbf861e2c15828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview Defines a class for parsing JSON using the browser's built in
 * JSON library.
 */

goog.provide('goog.json.NativeJsonProcessor');

goog.require('goog.asserts');
goog.require('goog.json');
goog.require('goog.json.Processor');



/**
 * A class that parses and stringifies JSON using the browser's built-in JSON
 * library, if it is avaliable.
 *
 * Note that the native JSON api has subtle differences across browsers, so
 * use this implementation with care.  See json_test#assertSerialize
 * for details on the differences from goog.json.
 *
 * This implementation is signficantly faster than goog.json, at least on
 * Chrome.  See json_perf.html for a perf test showing the difference.
 *
 * @param {?goog.json.Replacer=} opt_replacer An optional replacer to use during
 *     serialization.
 * @param {?goog.json.Reviver=} opt_reviver An optional reviver to use during
 *     parsing.
 * @constructor
 * @implements {goog.json.Processor}
 */
goog.json.NativeJsonProcessor = function(opt_replacer, opt_reviver) {
  goog.asserts.assert(goog.isDef(goog.global['JSON']), 'JSON not defined');

  /**
   * @type {goog.json.Replacer|null|undefined}
   * @private
   */
  this.replacer_ = opt_replacer;

  /**
   * @type {goog.json.Reviver|null|undefined}
   * @private
   */
  this.reviver_ = opt_reviver;
};


/** @override */
goog.json.NativeJsonProcessor.prototype.stringify = function(object) {
  return goog.global['JSON'].stringify(object, this.replacer_);
};


/** @override */
goog.json.NativeJsonProcessor.prototype.parse = function(s) {
  return goog.global['JSON'].parse(s, this.reviver_);
};