版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.stringify = exports.parse = void 0;
  4. /**
  5. * @since 2.10.0
  6. */
  7. var Either_1 = require("./Either");
  8. var function_1 = require("./function");
  9. /**
  10. * Converts a JavaScript Object Notation (JSON) string into a `Json` type.
  11. *
  12. * @example
  13. * import * as J from 'fp-ts/Json'
  14. * import * as E from 'fp-ts/Either'
  15. * import { pipe } from 'fp-ts/function'
  16. *
  17. * assert.deepStrictEqual(pipe('{"a":1}', J.parse), E.right({ a: 1 }))
  18. * assert.deepStrictEqual(pipe('{"a":}', J.parse), E.left(new SyntaxError('Unexpected token } in JSON at position 5')))
  19. *
  20. * @since 2.10.0
  21. */
  22. var parse = function (s) { return (0, Either_1.tryCatch)(function () { return JSON.parse(s); }, function_1.identity); };
  23. exports.parse = parse;
  24. /**
  25. * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
  26. *
  27. * @example
  28. * import * as E from 'fp-ts/Either'
  29. * import * as J from 'fp-ts/Json'
  30. * import { pipe } from 'fp-ts/function'
  31. *
  32. * assert.deepStrictEqual(J.stringify({ a: 1 }), E.right('{"a":1}'))
  33. * const circular: any = { ref: null }
  34. * circular.ref = circular
  35. * assert.deepStrictEqual(
  36. * pipe(
  37. * J.stringify(circular),
  38. * E.mapLeft(e => e instanceof Error && e.message.includes('Converting circular structure to JSON'))
  39. * ),
  40. * E.left(true)
  41. * )
  42. *
  43. * @since 2.10.0
  44. */
  45. var stringify = function (a) {
  46. return (0, Either_1.tryCatch)(function () {
  47. var s = JSON.stringify(a);
  48. if (typeof s !== 'string') {
  49. throw new Error('Converting unsupported structure to JSON');
  50. }
  51. return s;
  52. }, function_1.identity);
  53. };
  54. exports.stringify = stringify;