decode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import ParseACL from './ParseACL'; // eslint-disable-line no-unused-vars
  12. import ParseFile from './ParseFile';
  13. import ParseGeoPoint from './ParseGeoPoint';
  14. import ParsePolygon from './ParsePolygon';
  15. import ParseObject from './ParseObject';
  16. import { opFromJSON } from './ParseOp';
  17. import ParseRelation from './ParseRelation';
  18. export default function decode(value
  19. /*: any*/
  20. )
  21. /*: any*/
  22. {
  23. if (value === null || typeof value !== 'object') {
  24. return value;
  25. }
  26. if (Array.isArray(value)) {
  27. const dup = [];
  28. value.forEach((v, i) => {
  29. dup[i] = decode(v);
  30. });
  31. return dup;
  32. }
  33. if (typeof value.__op === 'string') {
  34. return opFromJSON(value);
  35. }
  36. if (value.__type === 'Pointer' && value.className) {
  37. return ParseObject.fromJSON(value);
  38. }
  39. if (value.__type === 'Object' && value.className) {
  40. return ParseObject.fromJSON(value);
  41. }
  42. if (value.__type === 'Relation') {
  43. // The parent and key fields will be populated by the parent
  44. const relation = new ParseRelation(null, null);
  45. relation.targetClassName = value.className;
  46. return relation;
  47. }
  48. if (value.__type === 'Date') {
  49. return new Date(value.iso);
  50. }
  51. if (value.__type === 'File') {
  52. return ParseFile.fromJSON(value);
  53. }
  54. if (value.__type === 'GeoPoint') {
  55. return new ParseGeoPoint({
  56. latitude: value.latitude,
  57. longitude: value.longitude
  58. });
  59. }
  60. if (value.__type === 'Polygon') {
  61. return new ParsePolygon(value.coordinates);
  62. }
  63. const copy = {};
  64. for (const k in value) {
  65. copy[k] = decode(value[k]);
  66. }
  67. return copy;
  68. }