canBeSerialized.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ParseFile from './ParseFile';
  12. import ParseObject from './ParseObject';
  13. import ParseRelation from './ParseRelation';
  14. export default function canBeSerialized(obj
  15. /*: ParseObject*/
  16. )
  17. /*: boolean*/
  18. {
  19. if (!(obj instanceof ParseObject)) {
  20. return true;
  21. }
  22. const attributes = obj.attributes;
  23. for (const attr in attributes) {
  24. const val = attributes[attr];
  25. if (!canBeSerializedHelper(val)) {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. function canBeSerializedHelper(value
  32. /*: any*/
  33. )
  34. /*: boolean*/
  35. {
  36. if (typeof value !== 'object') {
  37. return true;
  38. }
  39. if (value instanceof ParseRelation) {
  40. return true;
  41. }
  42. if (value instanceof ParseObject) {
  43. return !!value.id;
  44. }
  45. if (value instanceof ParseFile) {
  46. if (value.url()) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. if (Array.isArray(value)) {
  52. for (let i = 0; i < value.length; i++) {
  53. if (!canBeSerializedHelper(value[i])) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. for (const k in value) {
  60. if (!canBeSerializedHelper(value[k])) {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }