hybrid.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2013 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 Utility to attempt native JSON processing, falling back to
  16. * goog.json if not available.
  17. *
  18. * This is intended as a drop-in for current users of goog.json who want
  19. * to take advantage of native JSON if present.
  20. *
  21. * @author nnaze@google.com (Nathan Naze)
  22. */
  23. goog.provide('goog.json.hybrid');
  24. goog.require('goog.asserts');
  25. goog.require('goog.json');
  26. /**
  27. * Attempts to serialize the JSON string natively, falling back to
  28. * {@code goog.json.serialize} if unsuccessful.
  29. * @param {!Object} obj JavaScript object to serialize to JSON.
  30. * @return {string} Resulting JSON string.
  31. */
  32. goog.json.hybrid.stringify =
  33. goog.json.USE_NATIVE_JSON ? goog.global['JSON']['stringify'] : function(
  34. obj) {
  35. if (goog.global.JSON) {
  36. try {
  37. return goog.global.JSON.stringify(obj);
  38. } catch (e) {
  39. // Native serialization failed. Fall through to retry with
  40. // goog.json.serialize.
  41. }
  42. }
  43. return goog.json.serialize(obj);
  44. };
  45. /**
  46. * Attempts to parse the JSON string natively, falling back to
  47. * the supplied {@code fallbackParser} if unsuccessful.
  48. * @param {string} jsonString JSON string to parse.
  49. * @param {function(string):Object} fallbackParser Fallback JSON parser used
  50. * if native
  51. * @return {!Object} Resulting JSON object.
  52. * @private
  53. */
  54. goog.json.hybrid.parse_ = function(jsonString, fallbackParser) {
  55. if (goog.global.JSON) {
  56. try {
  57. var obj = goog.global.JSON.parse(jsonString);
  58. goog.asserts.assertObject(obj);
  59. return obj;
  60. } catch (e) {
  61. // Native parse failed. Fall through to retry with goog.json.unsafeParse.
  62. }
  63. }
  64. var obj = fallbackParser(jsonString);
  65. goog.asserts.assert(obj);
  66. return obj;
  67. };
  68. /**
  69. * Attempts to parse the JSON string natively, falling back to
  70. * {@code goog.json.parse} if unsuccessful.
  71. * @param {string} jsonString JSON string to parse.
  72. * @return {!Object} Resulting JSON object.
  73. */
  74. goog.json.hybrid.parse =
  75. goog.json.USE_NATIVE_JSON ? goog.global['JSON']['parse'] : function(
  76. jsonString) {
  77. return goog.json.hybrid.parse_(jsonString, goog.json.parse);
  78. };