amnaredo / test

0 stars 0 forks source link

Using OptionPickler with a custom pickler #247

Open amnaredo opened 2 years ago

amnaredo commented 2 years ago

I'm upgrading my project from uPickle 0.4.4 to 0.7.1. Everything went smoothly until I got to a class that needs a custom pickling AND a custom handling for Options so that it interoperates correctly with other systems generating Json. That is, I need to combine the techniques documented under "Customization - Custom Picklers" and "Customization - Custom Configuration".

If I make use of CustomThing2 (from the documentation), which pickles via a String, things work. Unfortunately, I started with the techniques shown in Bar, which pickles via the Json AST. Things go awry with a ujson.Value$InvalidData: Expected ujson.Arr (data: null) error.

I've added some tests to the uPickle test suite that illustrate the error. They make use of code from the documentation. Everything works except the very last test: read[Option[Bar]]("""null""") ==> None

I note that the following code is buried at line 180 of Visitor.scala. It looks to be doing something useful in this case, but I don't know how to make use of it.

    def mapFunction(v: V): Z =
      if(v == null) null.asInstanceOf[Z]
      else mapNonNullsFunction(v)

I believe I can get things to work by pickling via String. But I'm curious why one works but the other does not. I also believe the tests I've added are a useful addition to the test suite. Just wish I knew how to fix the last one.

I'll issue a pull request for the test suite changes.

ID: 261 Original Author: bwbecker

amnaredo commented 2 years ago

Thanks for the report, this should be fixed by a tweak to OptionPickler to make it handle null -> None conversion before the nulls get passed to the underlying pickler in https://github.com/lihaoyi/upickle/commit/ef6e9e8e1a34fd2ffdd23a335c59769c3fc85eea Original Author: lihaoyi