json.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. (function (factory) {
  6. if (typeof module === "object" && typeof module.exports === "object") {
  7. var v = factory(require, exports);
  8. if (v !== undefined) module.exports = v;
  9. }
  10. else if (typeof define === "function" && define.amd) {
  11. define(["require", "exports"], factory);
  12. }
  13. })(function (require, exports) {
  14. "use strict";
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.stringifyObject = void 0;
  17. function stringifyObject(obj, indent, stringifyLiteral) {
  18. if (obj !== null && typeof obj === 'object') {
  19. var newIndent = indent + '\t';
  20. if (Array.isArray(obj)) {
  21. if (obj.length === 0) {
  22. return '[]';
  23. }
  24. var result = '[\n';
  25. for (var i = 0; i < obj.length; i++) {
  26. result += newIndent + stringifyObject(obj[i], newIndent, stringifyLiteral);
  27. if (i < obj.length - 1) {
  28. result += ',';
  29. }
  30. result += '\n';
  31. }
  32. result += indent + ']';
  33. return result;
  34. }
  35. else {
  36. var keys = Object.keys(obj);
  37. if (keys.length === 0) {
  38. return '{}';
  39. }
  40. var result = '{\n';
  41. for (var i = 0; i < keys.length; i++) {
  42. var key = keys[i];
  43. result += newIndent + JSON.stringify(key) + ': ' + stringifyObject(obj[key], newIndent, stringifyLiteral);
  44. if (i < keys.length - 1) {
  45. result += ',';
  46. }
  47. result += '\n';
  48. }
  49. result += indent + '}';
  50. return result;
  51. }
  52. }
  53. return stringifyLiteral(obj);
  54. }
  55. exports.stringifyObject = stringifyObject;
  56. });