marshalling.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 /* MarshalledId.Uri */: return URI.revive(obj);
  19. case 2 /* MarshalledId.Regexp */: return new RegExp(obj.source, obj.flags);
  20. case 16 /* MarshalledId.Date */: return new Date(obj.source);
  21. }
  22. if (obj instanceof VSBuffer
  23. || obj instanceof Uint8Array) {
  24. return obj;
  25. }
  26. if (Array.isArray(obj)) {
  27. for (let i = 0; i < obj.length; ++i) {
  28. obj[i] = revive(obj[i], depth + 1);
  29. }
  30. }
  31. else {
  32. // walk object
  33. for (const key in obj) {
  34. if (Object.hasOwnProperty.call(obj, key)) {
  35. obj[key] = revive(obj[key], depth + 1);
  36. }
  37. }
  38. }
  39. }
  40. return obj;
  41. }