0

Consider the following TypeScript class:

export class Spot {

    public flight:number;

    public dateAndTimes:Map<string,string>

    public unitCost:number;

    constructor(){
        this.flight=0;
        this.dateAndTimes= new Map<string,string>();
        this.unitCost=0;
    }
}

How can I convert the Spot object to JSON?

I try JSON.stringify(spot), but the attribute dateAndTimes it's serialized as empty... for example:

"spot": {
 'flight':2,
  {},
 'unitCost':3500
}

Thank you very much!

AlejoDev
  • 4,345
  • 9
  • 36
  • 67
  • 2
    I guess your real problem is "How to convert Map to JSON". In that case it's already answered - https://stackoverflow.com/questions/29085197/how-do-you-json-stringify-an-es6-map (which will provide you a link to https://stackoverflow.com/questions/28918232/how-do-i-persist-a-es6-map-in-localstorage-or-elsewhere) – mykhailo.romaniuk Sep 27 '18 at 20:23
  • @mykhailo.romaniuk Thank you very much for your comment, but my real problem is that I need the whole object including the map attribute serialized in json once – AlejoDev Sep 27 '18 at 20:28
  • 1
    Don't use a map for that it's not helping you. – Aluan Haddad Sep 27 '18 at 20:33
  • I agree with @AluanHaddad you can use a normal js object (typescript `any`). You will save this problem for sure. – The.Bear Sep 27 '18 at 20:37

1 Answers1

1

Quoting from the MDN documentation for JSON.stringify:

  • All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized.

As a Map can have any type for keys and values then even if you were able to stringify it the JSON spec does not account for any types outside of "objects, arrays, numbers, strings, booleans, and null". Therefore if any of the keys or values of the Map are not of those types then it would not be allowed by the specification.

That last point is extremely important as even if a Map was somehow serialized to JSON but included any type of key or value not of the aforementioned allowed types then it is not determinable how a parser would process the resultant JSON back into an object. If it is not necessary to use a Map then I would recommend using a plain object for this to avoid this issue completely.

Note that it is also not possible to roll your own replacer function for the JSON.stringify method to handle that functionality.

Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • Thank you very much, but I would like to see an example in code .. how could the replacer create – AlejoDev Sep 27 '18 at 20:33
  • Not all keys of a `Map` are symbols. The keys are ignored because they are on the prototype – Aluan Haddad Sep 27 '18 at 20:33
  • @AlejoDev Apologies, I meant to say it *is not possible* to use a replacer function to do so as included in the original quote. Updated to clarify that. – Jason Cust Sep 27 '18 at 20:46
  • then...it is impossible to serialize a javascript object that contains a map as an attribute?... I believe there must be a way – AlejoDev Sep 27 '18 at 20:54
  • @AlejoDev I updated the answer to clarify the issue. If possible I would recommend using a simple object rather than a Map to avoid it. It *might* be possible to use [`toJSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#toJSON()_behavior) in this case but I am uncertain of that. It also does not resolve the greater issue outlined above. – Jason Cust Sep 27 '18 at 21:35