spray-json
spray-json copied to clipboard
Alternative product formats
The alternative product formats support the following:
- Use default values defined on case-classes when fields are missing from json
- Omit empty arrays and objects when serializing fields with default values
- Allow property renaming
- Override formats per field
- Allow advanced customizations
The formats can be obtained by using formatN instead of jsonFormatN
It supersedes my previous pull request #56
Any of this to be merged into master soon?
Ruud, thanks for this, from the looks of it it looks like a useful addition. However, we currently don't have any capacity for spray-json related work, so this PR might have to sit some time longer. However, we have scheduled a discussion with the other Typesafe teams (Akka / Play) regarding a general joint strategy for JSON support and expect this to get things moving again...
Sorry for not reacting earlier!
@rditerwich I'd like to get that merged and shipped with 1.3.0. However, you have to accept the Typesafe CLA first (just a few clicks). Would that be possible for you?
Also, the patch doesn't merge cleanly anymore. Could you rebase on top of the current master?
This is great news. I will accept the Typesafe CLA and rebase in the next couple of days.
I'd love to see this in!
+1 for getting this in if possible :)
plz pull in master
What is the status? Can this be merged?
I guess it can. We'll merge it when we cut the next minor release, which will hopefully be soon.
Any time plan for this one? Would be great to have.
I would really, really like this functionality too. I have a lot of custom JsonFormat definitions that are 95% boilerplate, but with a small amount of special handling for a minority of properties.
What's the status on this PR? Would be very interested in having this. Anything I can do to help?
+1
I guess this one missed the 1.3.2 boat. :(
Could this be broken out into a tiny separate library, or does it need to live in the spray.json namespace? :)
There is a problem with the way this is implemented:
If the case class contains "additional fields" (i.e. some inner case class, a lazy val etc.), then the automatic field names don't work. Normally that would mean using the "jsonFormat" method and specifying fields names manually but I can't find such a method for this alternative product format.
I've fixed it in our project by getting a TypeTag for P in ProductFormatImpl, getting the names of the constructor parameters from that tag and then filtering the declaredFields by name. This should eliminate one reason for the formatN-methods not working:
val tpe = ru.typeOf[P]
def termsOnly(s:ru.Symbol) = if(s.isTerm) Some(s.asTerm) else None
...
// list all case class constructor parameters
val constructor = tpe.members
.filter(_.isMethod).map(_.asMethod).find(_.isConstructor).toSeq
val constructorParams = constructor
.flatMap(_.paramss.head).flatMap(termsOnly)
// Filter declared fields based on constructor param names
val fields = runtimeClass.getDeclaredFields
.filter(field => constructorParams.exists(_.name.toString == field.getName))
There's probably a nice way of doing even more of the work with just the TypeTag, but this works for me for now.
Nice improvement, +1 for getting this merged.
+1 for sane optional values
+1, would be really helpfull
Any idea if this is still on the table? It's open for more than 2 years now.
+1, this would be helpful
+1, when this will be merged?
Hi there, I recently gained rights to maintain this repository and release etc from @sirthias. Will try to get on top of issues and PRs soon. This one was waiting a long time, but hope we can get it in perhaps now :)
+1 for merging this in...
+1 for merging
please release this 👍
@ktoso I see you started merging in some of the newer PRs. That's great news! Do you have any idea when you will be able to look at this one?
No updates on this?
any progress on this ?