json-mobx
json-mobx copied to clipboard
json.mapOf()?
Is there a equivalent for json.arrayOf but for observable maps instead of arrays?
I'm using this
const jsonObservableMap = (mapData) => {
// extends observable map, so json-mobx know how to serialize it
const map = observable.map(mapData)
Object.defineProperties(map, {
json: {
get: () => map.toJS(),
set: data => map.replace(data)
}
})
return map
}
Like this
class SomeClass {
@json @observable attrs = jsonObservableMap()
}
That looks just about perfect to me.
@danielearwicker would love to have this in the json-mobx so npm install just work.
I could look at this or accept PRs. I would need it to be typescript aware though. Also note that the built-in array support allows for:
json.arrayOf(MyClass)
where MyClass is the type of the array elements, so on deserialisation we can construct the right type of objects.
So it would be good to have:
json.mapOf(MyClass)
which would define a map of string-to-MyClass.