question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redux states - do they need to be serializable?

See original GitHub issue

Is there any specific reason why KVision’s redux module should require serializable data classes? I’m running into a problem with a custom class of ObjectID from MongoDB. I’ve written a custom serializer for my class:

@Serializer(forClass = ObjectID::class)
object ObjectIDSerializer: KSerializer<ObjectID> {
    override val descriptor: SerialDescriptor =
            StringDescriptor.withName("ObjectID")

    override fun serialize(encoder: Encoder, obj: ObjectID) {
        encoder.encodeString(obj.toString())
    }

    override fun deserialize(decoder: Decoder): ObjectID {
        return ObjectID(decoder.decodeString())
    }
}

but when I try to run it in the browser, I get Can't locate argument-less serializer for class null even though none of my variables in the redux state are nullable. Is it possible to remove the requirement for it to be serializable? Unless there is a specific reason why it needs to be.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (19 by maintainers)

github_iconTop GitHub Comments

1reaction
rjaroscommented, Jun 21, 2019

The redux module is already fixed in my source tree. I’ve modified some tests to check if lists are working and it seems they do. Thx for this enhancement.

1reaction
rjaroscommented, Jun 21, 2019

First the Redux case. It’s not that simple. The original Redux (redux.js.org) is quite sophisticated piece of software. It’s extendable with addons and middleware and kvision-redux module includes one of the most widely used ones: redux-thunk. It allows to dispatch “Action creators” functions, which is necessary for any asynchronous operations. Your redux implementation is very basic and has no support for this. With original Redux implementation you can use any middleware you want, and the Redux ecosystem gives you very large choice. That’s the reason I’ve integrated KVision with original Redux library. I’m watching some Kotlin projects (https://github.com/freeletics/CoRedux, https://github.com/gumil/Kaskade), but none of them are targeting Kotlin/JS yet.

Regarding the serializable requirement - the kotlin-redux project I’m using has some limitations with Kotlin Lists. I’ve decided that support for Lists is more important, so my kvision-redux module serializes the state to the JSON string before passing it to Redux. This way we can use any classes for the Kotlin state, as long as they are serializable - this is a trade-off.

What can be done:

  1. We can easily create second kvision-redux module without serialization requirement. But you will not be able to use Lists in your state (and should be ready for other incompatibilities).
  2. We can create yet another redux module with your implementantion instead of the original. But you will not be able to dispatch action creators or use any other Redux addons.
Read more comments on GitHub >

github_iconTop Results From Across the Web

The redux best practice "Do Not Put Non-Serializable Values ...
This article explains one of the four react-redux best practices: "Do Not Put Non-Serializable Values in State or Actions"
Read more >
Redux FAQ: Organizing State
It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to ...
Read more >
Is storing Non-Serializable Values into a Redux store a bad ...
Avoid putting non-serializable values such as Promises, Symbols, Maps/Sets, functions, or class instances into the Redux store state or ...
Read more >
About the redux best practice "Do not put non-serializable ...
No, we've always emphasized that a Redux store should only contain plain JS objects, arrays, and primitives, and this article covers some of...
Read more >
Storing a function in the Redux store - prasanna.dev
So, if you are trying to store a inside the Redux state, you need to serialize them before persisting. Storing functions inside redux...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found