marshalling.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. import { VSBuffer } from './buffer.js';
  6. import { URI } from './uri.js';
  7. export function parse(text) {
  8. let data = JSON.parse(text);
  9. data = revive(data);
  10. return data;
  11. }
  12. export function revive(obj, depth = 0) {
  13. if (!obj || depth > 200) {
  14. return obj;
  15. }
  16. if (typeof obj === 'object') {
  17. switch (obj.$mid) {
  18. case 1 /* Uri */: return URI.revive(obj);
  19. case 2 /* Regexp */: return new RegExp(obj.source, obj.flags);
  20. }
  21. if (obj instanceof VSBuffer
  22. || obj instanceof Uint8Array) {
  23. return obj;
  24. }
  25. if (Array.isArray(obj)) {
  26. for (let i = 0; i < obj.length; ++i) {
  27. obj[i] = revive(obj[i], depth + 1);
  28. }
  29. }
  30. else {
  31. // walk object
  32. for (const key in obj) {
  33. if (Object.hasOwnProperty.call(obj, key)) {
  34. obj[key] = revive(obj[key], depth + 1);
  35. }
  36. }
  37. }
  38. }
  39. return obj;
  40. }