-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Inline Json Codec #1078
Comments
Hi, @saadaamer ! Thanks for trying jsoniter-scala and sending your feedback! Unfortunately, an automatic unwrapping works only for I've created a separated issue for adding that configuration for other classes: #1079 But you can use the following custom codec with an auxiliary case class to serialize nested object ContactDetails {
private case class ContactDetailUnwrapped(email: String, firstName: String, middleName: String, lastName: String)
private object ContactDetailUnwrapped {
def toContactDetail(x: ContactDetailUnwrapped): ContactDetail =
if (x eq null) null
else ContactDetail(x.email, Name(x.firstName, x.middleName, x.lastName))
def fromContactDetail(x: ContactDetail): ContactDetailUnwrapped =
if (x eq null) null
else {
val name = x.name
ContactDetailUnwrapped(x.email, name.firstName, name.middleName, name.lastName)
}
}
implicit val contactDetailCodec: JsonValueCodec[ContactDetail] = new JsonValueCodec[ContactDetail] {
private[this] val codec: JsonValueCodec[ContactDetailUnwrapped] = JsonCodecMaker.make[ContactDetailUnwrapped]
override def decodeValue(in: JsonReader, default: ContactDetail): ContactDetail =
ContactDetailUnwrapped.toContactDetail(codec.decodeValue(in, ContactDetailUnwrapped.fromContactDetail(default)))
override def encodeValue(x: ContactDetail, out: JsonWriter): Unit =
codec.encodeValue(ContactDetailUnwrapped.fromContactDetail(x), out)
override def nullValue: ContactDetail = null
}
} If you would want more efficient codec implementation without instantiation of auxiliary instances then an easier way to write it is printing the codec generated for |
Hello,
We are in a process of migrating from play json to jsoniter-scala and struggling to find solution for the following scenario
Following is the example model and the json generated
In play-json we can inline the above json like following
is there a way/configuration I can use to achieve the above in Jsoniter?
The text was updated successfully, but these errors were encountered: