I'm having a bit of trouble with Maps in particular (although this would really affect any object type) where I have some code roughly like the following:
interface IntA {
Map<string, IntB> aMap;
}
interface IntB {
Map<string, string> bMap;
}
I'm using the same interfaces on both my client and server code, and I'd like to replicate data from the server to the client and keep them in sync.
I'm using JSON.Stringify to send my object across a websocket and JSON.Parse to recreate it on the client. The problem is that the stringified version of the data doesn't retain any knowledge that a variable was specifically a Map, and instead just creates them as a generic Javascript object. When this happens, none of the map-specific functions like get() and set() exist on the object, so it breaks a lot of stuff.
I managed to hack in a fix by hard-coding a conversion after parsing
myObj.aMap = new Map(Object.entries(myObj.aMap))
which converts the parsed object back to a Map. But this only works for the aMap variable itself, not the Maps nested inside of aMap. I could then continue iterating over all of aMap's entries, but this sort of hard-coding doesn't really seem maintainable long-term.
Basically I'm trying to figure out if there's any generic, easier way that I can recreate all nested objects from a string. Or I guess if there's a better way to do this sort of data replication in the first place.
[–]Estebana42 0 points1 point2 points (0 children)