如何为具有超过22个字段的案例类编写隐式Writes [英] How to write implicit Writes for case class having more than 22 fields

查看:115
本文介绍了如何为具有超过22个字段的案例类编写隐式Writes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

case class Foo(
  _1:Int,_2:Int,_3:Int,_4:Int,_5:Int,
  _21:Int,_22:Int,_23:Int,_24:Int,_25:Int,
  _31:Int,_32:Int,_33:Int,_34:Int,_35:Int,
  _41:Int,_42:Int,_43:Int,_44:Int,_45:Int,
  _51:Int,_52:Int,_53:Int,_54:Int,_55:Int
)

对于这样的案例类,我需要编写隐式的json de/序列化器. 我尝试拆分字段,并使用JSONFormat.但是我仍然需要使用Json.obj()的隐式OWrited.我也尝试过play-json-extensions.有什么想法吗?

For a case class like this, I need to write implicit json de- / serializer. I tried splitting the fields, and had a JSONFormat. But still I need implicit OWrited for using Json.obj(). I tried with play-json-extensions also. Any ideas?

推荐答案

这里确实有3种途径可供您探索:

There's really 3 avenues for you to explore here:

  1. 自己明确地写出每个班级(是的,大量的样板.)
  2. 使用Macros或 Shapeless 完成.
  3. 使用已经做过#2的某人的图书馆.
  1. Write out each class explicitly yourself (yes, tons of boilerplate.)
  2. Do it with Macros or Shapeless.
  3. Use someone's library who already did #2.

当别人为我工作时,我喜欢它.因此,考虑到这一点,#3似乎是我的首选解决方案...您会知道什么?有人这样做了: play-json-derived-codecs .由于它使用 Shapeless ,它将能够处理任意大小的案例类,而不仅限于那些受大小限制的案例类. ProductN(1-22,具体取决于您的Scala版本.)

I like it when someone else does the work for me. So with that in mind, #3 seems like my preferred solution... And what would you know? Someone else did just that: play-json-derived-codecs. Since it uses Shapeless, it will be able to handle case classses of arbitrary size, not just those constrained by ProductN (1-22, depending on your version of Scala.)

这篇关于如何为具有超过22个字段的案例类编写隐式Writes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆