spray-json icon indicating copy to clipboard operation
spray-json copied to clipboard

Alternative product formats

Open rditerwich opened this issue 11 years ago • 38 comments

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

rditerwich avatar Mar 09 '14 18:03 rditerwich

Any of this to be merged into master soon?

jdevelop avatar Apr 22 '14 03:04 jdevelop

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!

sirthias avatar Apr 22 '14 08:04 sirthias

@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?

sirthias avatar Sep 19 '14 13:09 sirthias

Also, the patch doesn't merge cleanly anymore. Could you rebase on top of the current master?

sirthias avatar Sep 19 '14 13:09 sirthias

This is great news. I will accept the Typesafe CLA and rebase in the next couple of days.

rditerwich avatar Sep 27 '14 10:09 rditerwich

I'd love to see this in!

fzakaria avatar Oct 24 '14 22:10 fzakaria

+1 for getting this in if possible :)

jamesmorgan avatar Dec 01 '14 16:12 jamesmorgan

plz pull in master

bvolders avatar Dec 18 '14 08:12 bvolders

What is the status? Can this be merged?

diversit avatar Feb 05 '15 14:02 diversit

I guess it can. We'll merge it when we cut the next minor release, which will hopefully be soon.

sirthias avatar Feb 06 '15 13:02 sirthias

Any time plan for this one? Would be great to have.

mustajarvi avatar Mar 11 '15 14:03 mustajarvi

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.

acruise avatar Apr 27 '15 21:04 acruise

What's the status on this PR? Would be very interested in having this. Anything I can do to help?

j-mueller avatar Apr 28 '15 14:04 j-mueller

+1

raboof avatar May 03 '15 20:05 raboof

I guess this one missed the 1.3.2 boat. :(

acruise avatar May 06 '15 18:05 acruise

Could this be broken out into a tiny separate library, or does it need to live in the spray.json namespace? :)

acruise avatar May 11 '15 18:05 acruise

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.

fbjon avatar May 22 '15 09:05 fbjon

Nice improvement, +1 for getting this merged.

ghost avatar Jun 15 '15 09:06 ghost

+1 for sane optional values

ksilin avatar Jul 21 '15 11:07 ksilin

+1, would be really helpfull

merlijn avatar Feb 10 '16 16:02 merlijn

Any idea if this is still on the table? It's open for more than 2 years now.

aditanase avatar May 18 '16 11:05 aditanase

+1, this would be helpful

mboggan avatar Jun 22 '16 22:06 mboggan

+1, when this will be merged?

shlushchanka avatar Oct 07 '16 13:10 shlushchanka

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 :)

ktoso avatar Oct 07 '16 15:10 ktoso

+1 for merging this in...

zhangcbrian avatar Oct 23 '16 21:10 zhangcbrian

+1 for merging

mtomas avatar Dec 20 '16 13:12 mtomas

please release this 👍

greenhost87 avatar Jan 17 '17 20:01 greenhost87

@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?

poohsen avatar Feb 01 '17 08:02 poohsen

No updates on this?

poohsen avatar Mar 06 '17 09:03 poohsen

any progress on this ?

zzzhy avatar Mar 21 '17 09:03 zzzhy