-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
Control serialization of a certain property across many classes in a DRY manner #40
Comments
If you want to override the serialization of something like option, you could shadow the option reader and writer with your own. Would that work? That would override the serialization of all options, but it sounds like maybe that's what you want |
That might work in this particular situation, but a more precise control would have been better. For example, I would have liked to have properties with only the In any case, I am not sure how to get this working where I preserve the behavior of setting the deserialize object's property to the default value ( package com.example
import upickle._
object Hello {
trait Entity {
def id: Option[Long]
}
case class Person(fName: String, lName: String, id: Option[Long] = None) extends Entity
case class Organization(name: String, id: Option[Long] = None) extends Entity
implicit val option2Writer = upickle.Writer[Option[Long]]{
case None => Js.Null
case Some(value) => Js.Num(value)
}
implicit val option2Reader = upickle.Reader[Option[Long]]{
case Js.Null => None
case Js.Num(num) => Some(num.toLong)
}
def main(args: Array[String]): Unit = {
val p1 = Person("F", "L")
val p2 = Person("F", "L", Some(1234))
val p1written = write(p1)
val p2written= write(p2)
val p1read = read[Person](p1written)
val p2read = read[Person](p2written)
println(s"$p1 -> $p1written -> $p1read")
println(s"$p2 -> $p2written -> $p2read")
}
} Running which, I get:
Here, the first object's |
I'm not sure what that would look like. I guess you could extend For the second problem, it looks like you're being bitten by 689ebcd#diff-5c20a012db0ef74e915344d710882fe5R43 Which was meant to fix It should be possible to refactor the library to make it work the other way, i.e. by flipping the order to make the user-defined |
+ Fixes com-lihaoyi#40 + Introduces Js.None, since Js.Null has semantics of Javascript null. This allows custom writers to return Js.None to specify that the value should be writen to the output.
Just made a PR to fix this problem. A part of the issue was |
+ Fixes com-lihaoyi#40 + Introduces Js.None, since Js.Null has semantics of Javascript null. This allows custom writers to return Js.None to specify that the value should not be writen to the output.
I have some work on this in my branch here. It depends on #65, so I'm not submitting it yet. It is based on a suggestion by @lihaoyi in #43: a new implicit configuration called It required some rewrite of macros, and now most of |
"Serialization of properties across all classes" can now be coarsely controlled at the Let me know if that doesn't work for you or if you want something more fine-grained |
I am wondering what would be a good (=DRY) way to modify serialization and deserialization of a certain property in many classes.
I have a bunch of model classes of the following structure (somewhat simplified). I am persisting them using Slick, where such a structure is fairly common:
Currently, uPickle serializes the
id
property as an array:I am looking for a way to map the
id
property as follows:id
isNone
, omit the property. uPickle already does this, since theid
property's default isNone
.id
isSome(value)
, just serialize thevalue
:What would be a good way, short of writing either a custom
apply()
andunapply()
and the attendant parallel set of classes or writing aReader
andWriter
for each model class?The text was updated successfully, but these errors were encountered: