Skip to main content
The 2024 Developer Survey results are live! See the results
deleted 5 characters in body
Source Link
Mr. Polywhirl
  • 47k
  • 12
  • 90
  • 139

You cannot call JSON.stringify on most Map or Set.

You cannot call JSON.stringify on most Map or Set.

You cannot call JSON.stringify on Map or Set.

added 1035 characters in body
Source Link
Mr. Polywhirl
  • 47k
  • 12
  • 90
  • 139
class Cat {
  constructor(options = {}) {
    this.name = options.name ?? '';
    this.age = options.age ?? 0;
  }
  toString() {
    return `[Cat name="${this.name}", age="${this.age}"]`
  }
  toJSON() {
    constreturn { age, name } = this;
    return {: this.name, age: this.age };
  }
  static fromObject(obj) {
    const { name, age } = obj ?? {};
    return new Cat({ name, age });
  }
}

/*
 * JSON Set adds the missing methods:
 * - toJSON
 * - toString
 */
class JSONSet extends Set {
  constructor(values) {
    super(values)
  }
  toString() {
    return super
      .toString()
      .replace(']', ` ${[...this].map(v => v.toString())
      .join(', ')}]`);
  }
  toJSON() {
    return [...this];
  }
}

const cats = new JSONSet([
  Cat.fromObject({ name: 'Furball', age: 2 }),
  Cat.fromObject({ name: 'Artemis', age: 5 })
];]);

console.log(cats.toString());
console.log(JSON.stringify(cats, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
class Cat {
  constructor(options = {}) {
    this.name = options.name ?? '';
    this.age = options.age ?? 0;
  }
  toString() {
    return `[Cat name="${this.name}", age="${this.age}"]`
  }
  toJSON() {
    const { age, name } = this;
    return { name, age };
  }
  static fromObject(obj) {
    const { name, age } = obj ?? {};
    return new Cat({ name, age });
  }
}

const cats = [
  Cat.fromObject({ name: 'Furball', age: 2 }),
  Cat.fromObject({ name: 'Artemis', age: 5 })
];

console.log(cats.toString());
console.log(JSON.stringify(cats, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
class Cat {
  constructor(options = {}) {
    this.name = options.name ?? '';
    this.age = options.age ?? 0;
  }
  toString() {
    return `[Cat name="${this.name}", age="${this.age}"]`
  }
  toJSON() {
    return { name: this.name, age: this.age };
  }
  static fromObject(obj) {
    const { name, age } = obj ?? {};
    return new Cat({ name, age });
  }
}

/*
 * JSON Set adds the missing methods:
 * - toJSON
 * - toString
 */
class JSONSet extends Set {
  constructor(values) {
    super(values)
  }
  toString() {
    return super
      .toString()
      .replace(']', ` ${[...this].map(v => v.toString())
      .join(', ')}]`);
  }
  toJSON() {
    return [...this];
  }
}

const cats = new JSONSet([
  Cat.fromObject({ name: 'Furball', age: 2 }),
  Cat.fromObject({ name: 'Artemis', age: 5 })
]);

console.log(cats.toString());
console.log(JSON.stringify(cats, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }
added 1035 characters in body
Source Link
Mr. Polywhirl
  • 47k
  • 12
  • 90
  • 139

toJSON method

If you want to call JSON.stringify on a class object, you will need to override the toJSON method to return your instance data.

class Cat {
  constructor(options = {}) {
    this.name = options.name ?? '';
    this.age = options.age ?? 0;
  }
  toString() {
    return `[Cat name="${this.name}", age="${this.age}"]`
  }
  toJSON() {
    const { age, name } = this;
    return { name, age };
  }
  static fromObject(obj) {
    const { name, age } = obj ?? {};
    return new Cat({ name, age });
  }
}

const cats = [
  Cat.fromObject({ name: 'Furball', age: 2 }),
  Cat.fromObject({ name: 'Artemis', age: 5 })
];

console.log(cats.toString());
console.log(JSON.stringify(cats, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }

toJSON method

If you want to call JSON.stringify on a class object, you will need to override the toJSON method to return your instance data.

class Cat {
  constructor(options = {}) {
    this.name = options.name ?? '';
    this.age = options.age ?? 0;
  }
  toString() {
    return `[Cat name="${this.name}", age="${this.age}"]`
  }
  toJSON() {
    const { age, name } = this;
    return { name, age };
  }
  static fromObject(obj) {
    const { name, age } = obj ?? {};
    return new Cat({ name, age });
  }
}

const cats = [
  Cat.fromObject({ name: 'Furball', age: 2 }),
  Cat.fromObject({ name: 'Artemis', age: 5 })
];

console.log(cats.toString());
console.log(JSON.stringify(cats, null, 2));
.as-console-wrapper { top: 0; max-height: 100% !important; }

Source Link
Mr. Polywhirl
  • 47k
  • 12
  • 90
  • 139
Loading