Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json.mapOf()? #5

Open
prabirshrestha opened this issue Jul 21, 2017 · 4 comments
Open

json.mapOf()? #5

prabirshrestha opened this issue Jul 21, 2017 · 4 comments

Comments

@prabirshrestha
Copy link

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

@Roger
Copy link

Roger commented Aug 28, 2017

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

@danielearwicker
Copy link
Owner

That looks just about perfect to me.

@prabirshrestha
Copy link
Author

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

@danielearwicker
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants