3

I have the following class structure:

export abstract class PersonBase {
    public toJSON(): string {
        let obj = Object.assign(this);
        let keys = Object.keys(this.constructor.prototype);
        obj.toJSON = undefined;
        return JSON.stringify(obj, keys);
    }
}

export class Person extends PersonBase {

    private readonly _firstName: string;
    private readonly _lastName: string;

    public constructor(firstName: string, lastName: string) {
        this._firstName = firstName;
        this._lastName = lastName;
    }

    public get first_name(): string {
        return this._firstName;
    }

    public get last_name(): string {
        return this._lastName;
    }
}

export class DetailPerson extends Person {

    private _address: string;

    public constructor(firstName: string, lastName: string) {
        super(firstName, lastName);
    }

    public get address(): string {
        return this._address;
    }
     
    public set address(addy: string) {
        this._address = addy;
    }
}

I am trying to get toJSON to output all the getters (excluding private properties) from the full object hierarchy.

So if I have a DetailPerson instance and I call the toJSON method, I want to see the following output:

{
   "address": "Some Address",
   "first_name": "My first name",
   "last_name": "My last name"
}

I used one of the solutions from this Q&A but it doesn't solve my particular use case - I am not getting all the getters in the output.

What do I need to change here to get the result I am looking for?

Daz
  • 147
  • 2
  • 12
  • Have you defined the method `toJsonString` on both the `Person` and `DetailPerson` classes, as shown in the post you linked? I don't see them defined in the code you posted. – gforce301 Jun 01 '17 at 19:21
  • Please provide a [mcve] showing the exact issue you are trying to solve. – Heretic Monkey Jun 01 '17 at 19:27
  • Amendments made. – Daz Jun 01 '17 at 20:03
  • @Daz Maybe you'd like to edit this question again? As far as I can tell, the `toJSON` method added to `PersonBase` doesn't return the getters from `DetailPerson`. Also, the previous phrasing of the question made it more clear that you want to omit "private" properties (i.e. you only want getters). – Frank Modica Jun 02 '17 at 04:06
  • Additional edits made. – Daz Jun 02 '17 at 18:39

1 Answers1

5

The link you provided uses Object.keys which leaves out properties on the prototype.

You could use for...in instead of Object.keys:

public toJSON(): string {
    let obj: any = {};

    for (let key in this) {
        if (key[0] !== '_') {
            obj[key] = this[key];
        }
    }

    return JSON.stringify(obj);
}

Edit: This is my attempt to return only getters, recursively, without assuming that non-getters start with underscores. I'm sure there are gotchas I missed (circular references, issues with certain types), but it's a good start:

abstract class PersonBase {
  public toJSON(): string {
    return JSON.stringify(this._onlyGetters(this));
  }

  private _onlyGetters(obj: any): any {
    // Gotchas: types for which typeof returns "object"
    if (obj === null || obj instanceof Array || obj instanceof Date) {
      return obj;
    }

    let onlyGetters: any = {};

    // Iterate over each property for this object and its prototypes. We'll get each
    // property only once regardless of how many times it exists on parent prototypes.
    for (let key in obj) {
      let proto = obj;

      // Check getOwnPropertyDescriptor to see if the property is a getter. It will only
      // return the descriptor for properties on this object (not prototypes), so we have
      // to walk the prototype chain.
      while (proto) {
        let descriptor = Object.getOwnPropertyDescriptor(proto, key);

        if (descriptor && descriptor.get) {
          // Access the getter on the original object (not proto), because while the getter
          // may be defined on proto, we want the property it gets to be the one from the
          // lowest level
          let val = obj[key];

          if (typeof val === 'object') {
            onlyGetters[key] = this._onlyGetters(val);
          } else {
            onlyGetters[key] = val;
          }

          proto = null;
        } else {
          proto = Object.getPrototypeOf(proto);
        }
      }
    }

    return onlyGetters;
  }
}
Frank Modica
  • 10,238
  • 3
  • 23
  • 39
  • 1
    Thank you... ideally i was hoping for a solution that was more generic, something that didn't make assumptions about how i name private properties vs getters\setters. In the case my private property is named firstName and the getter\setter is First_Name or any other variant this won't work. I should have been clearer with my question, apologies. – Daz Jun 01 '17 at 20:16
  • I edited my response. Let me know if that works for you. – Frank Modica Jun 01 '17 at 21:26
  • What would it take to modify this so that it would correctly serialize a get property that was in-fact another object I had defined? In the example above say the DetailPerson has a Pet object property with its own set of get\set properties? – Daz Aug 31 '17 at 20:27
  • @Daz I updated the answer over time to address that issue - does it not work? – Frank Modica Aug 31 '17 at 20:29
  • Apologies, I did not see those changes, the updates have in fact resolved the issue, however, it is now not serializing Date properties properly, all i see is {} – Daz Aug 31 '17 at 21:07
  • 1
    @Daz I made an update, but can't test at the moment. Can you tell me if it works? – Frank Modica Aug 31 '17 at 21:18
  • That did it, thank you. Appreciate the very quick replies. – Daz Aug 31 '17 at 21:25
  • I think you shouldn't do `JSON.stringify` inside the `toJSON` function, just output the object tree the way you want it to be formatted. – orad Apr 25 '18 at 21:44
  • 1
    Unfortunately, both return just `{ }` in my case, i.e. an empty JSON object. – Manfred Jun 02 '23 at 06:07
  • @Manfred Did you try to debug it and see why it happens in your case? – Frank Modica Jun 02 '23 at 15:02
  • @FrankModica I debugged it and also tried a few other things but couldn't figure it out yet. For noww I put it into the "too hard" basket to revisit again in the future. As a temporary measure, I just implemented a `toJSON` method in which I define a JSON object with the values I need, then use `JSON.stringify(theObj)` to return a string. Not generic, not nice, but does the job at least for the one class where I need it. – Manfred Jun 04 '23 at 21:17