json-mobx icon indicating copy to clipboard operation
json-mobx copied to clipboard

json.mapOf()?

Open prabirshrestha opened this issue 8 years ago • 4 comments

Is there a equivalent for json.arrayOf but for observable maps instead of arrays?

prabirshrestha avatar Jul 21 '17 20:07 prabirshrestha

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()
  }

Roger avatar Aug 28 '17 11:08 Roger

That looks just about perfect to me.

danielearwicker avatar Aug 28 '17 21:08 danielearwicker

@danielearwicker would love to have this in the json-mobx so npm install just work.

prabirshrestha avatar Aug 29 '17 04:08 prabirshrestha

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.

danielearwicker avatar Aug 29 '17 21:08 danielearwicker