Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • 1
    I'm not sure that many will be satisfied with extending the core map class just to serialize it to a json...
    – vasia
    Commented Apr 5, 2020 at 21:36
  • 4
    They don't have to be, but it's a more SOLID way of doing it. Specifically, this aligns with the LSP and OCP principles of SOLID. That is, the native Map is being extended, not modified, and one can still use Liskov Substitution (LSP) with a native Map. Granted, it's more OOP than a lot of novices or staunch Functional Programming people would prefer, but at least it's beset upon a tried & true baseline of fundamental software design principles. If you wanted to implement Interface Segregation Principle (ISP) of SOLID, you can have a small IJSONAble interface (using TypeScript, of course).
    – Cody
    Commented Apr 5, 2020 at 22:47
  • 1
    Yes, this is a good solution since it can produce specific serialisation variations. You can do new MyMap1(someMap) or new MyMap2(someMap) to clone and extend exiting maps easily. And then MyMap1 and MyMap2 can have different serialisation. For example, one might just serialise to a list of tuples, the other to an object. Furthermore, there can be special handling for serialising keys (since those could be arbitrary objects) or similar. The replacer option in JSON.stringify works OK but if it has to handle multiple types, it becomes cumbersome.
    – VLAZ
    Commented Feb 19 at 9:03