Javascript 将对象中的Typescript映射到Json

Javascript 将对象中的Typescript映射到Json,javascript,json,typescript,Javascript,Json,Typescript,我有课: export class Widget { public id: any; public name: string; public position: number; public height: number; public width: number; public type: string; public params: Map<string, string>; ... } 现有的解决方案仍然是正确的 您是否希望使用某种类型的插入式替换,

我有课:

export class Widget {
  public id: any;
  public name: string;
  public position: number;
  public height: number;
  public width: number;
  public type: string;
  public params: Map<string, string>;
...
}
现有的解决方案仍然是正确的

您是否希望使用某种类型的插入式替换,并与值一起工作?你需要自己写

这两个函数都使用一个可选参数(
replace
表示
JSON.stringify()
reviver
表示
JSON.parse()
),允许您自定义如何将JavaScript对象转换为JSON和从JSON转换过来。有一种可能性:

const JSONWithMap = (mapIdentifier: string = "SerializedES6Map!!") => ({

  stringify(
    value: any,
    replacer?: ((key: string, value: any) => any) | (number | string)[] | null,
    space?: string | number
  ): string {

    let innerReplacer: (key: string, value: any) => any;
    if ((typeof replacer === 'undefined') || (replacer === null)) {
      innerReplacer = (k, v) => v;
    } else if (Array.isArray(replacer)) {
      innerReplacer = (k, v) => replacer.indexOf(k) >= 0 ? v : void 0;
    } else {
      innerReplacer = replacer;
    }

    let outerReplacer = function(this: any, key: string, value: any) {
      if (value instanceof Map) {        
        value = { [mapIdentifier]: Array.from(value.entries()) };
      }
      return innerReplacer.call(this, key, value);
    }

    return JSON.stringify(value, outerReplacer, space);

  },

  parse(text: string, reviver?: (key: any, value: any) => any): any {
    let innerReviver: (key: string, value: any) => any;
    if (typeof reviver === 'undefined' || reviver === null) {
      innerReviver = (k, v) => v;
    } else {
      innerReviver = reviver;
    }
    let outerReviver = function(this: any, key: string, value: any) {
      if ((typeof value !== 'undefined') && (Array.isArray(value[mapIdentifier]))) {
        return new Map(value[mapIdentifier]);
      }
      return innerReviver.call(this, key, value);
    }
    return JSON.parse(text, outerReviver);
  }
});
函数
JSONWithMap()
在对
Map
进行序列化和反序列化时,使用一个可选的字符串参数进行标记,它默认为
“SerializedES6Map!!”
,希望您不会在其他对象中使用它。它的
stringify()
除了它识别那些序列化的
Map
对象并将它们反序列化为
Map
s之外

您可以这样使用它:

declare const widget: Widget; // some widget instance
const JSONM = JSONWithMap(); // use default identifier
const widgetJSON = JSONM.stringify(widget); // serialize
const deserializedWidget = JSONM.parse(widgetJSON); // deserialize into plain JS object
const rehydratedWidget = Object.assign(new Widget(), deserializedWidget); // actually make instance of class from plain JS object

注意,我说“您需要自己编写”,然后递给您一个实现。除了非常简短的测试之外,这个实现还没有被测试过,所以您需要彻底地测试它,为您的
小部件
类做一些定制的事情,而不是替换
JSON.stringify()
,或者自己编写它


祝你好运。

@jcalz的问题和选择答案已经超过2岁了。“那还相关吗?”ideaboxer问题和choosen答案已经有2年多的历史了。它仍然相关吗?您仍然可以将
映射
转换为数组并序列化它吗?当然
declare const widget: Widget; // some widget instance
const JSONM = JSONWithMap(); // use default identifier
const widgetJSON = JSONM.stringify(widget); // serialize
const deserializedWidget = JSONM.parse(widgetJSON); // deserialize into plain JS object
const rehydratedWidget = Object.assign(new Widget(), deserializedWidget); // actually make instance of class from plain JS object