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!

4
  • 2
    I guess your real problem is "How to convert Map to JSON". In that case it's already answered - stackoverflow.com/questions/29085197/… (which will provide you a link to stackoverflow.com/questions/28918232/…) Commented Sep 27, 2018 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 Commented Sep 27, 2018 at 20:28
  • 1
    Don't use a map for that it's not helping you. Commented Sep 27, 2018 at 20:33
  • I agree with @AluanHaddad you can use a normal js object (typescript any). You will save this problem for sure. Commented Sep 27, 2018 at 20:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, but I would like to see an example in code .. how could the replacer create
Not all keys of a Map are symbols. The keys are ignored because they are on the prototype
@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.
then...it is impossible to serialize a javascript object that contains a map as an attribute?... I believe there must be a way
@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 in this case but I am uncertain of that. It also does not resolve the greater issue outlined above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.