Play-Json | Serialization of Case classes(23 + fields)

Reading Time: 2 minutes

Overview

In this blog we discuss about how to Deserialise and Serialise case classes having greater than 22 fields using play-json-extensions.

Library Dependencies

Add the given dependencies to your build.sbt

libraryDependencies += "ai.x" %% "play-json-extensions" % "0.42.0"

Introduction

As we use play-json for deserialise and serialise case classes in different scala framework like play,akka etc.

Sometimes we encounter the problem for formating these case classes which have more then 22 fields fields.As normal play-json support only maximum 22 fields.

For getting this kind of support for bigger case class we use play-json-extensions.As it provide same suitable support as play-json but it works for bigger case class also(>22 fields).

After that formatting we can use the same procedure as done in play-json for converting json data and case classes.

Examples

We check out this feature with the help of the below examples.

import play.api.libs.json.Format
import ai.x.play.json.Jsonx

import ai.x.play.json.Encoders._

object Main extends App {

case class SampleBiggerClass(      
a1:String,a2:String,a3:String,a4:String,a5:String,a6:String,a7:String,a8:String,a9:String,a10:String,a11:String,a12:String,a13:String,a14:String,a15:String,a16:String,a17:String,a18:String,a19:String,a20:String,a21:String,a22:String,a23:String,a24:String,a25:String,a26:String)

 val sampleBiggerClass = SampleBiggerClass("a1","a2","a3","a4","a5","a6","a7","a8","a9","a10","a11","a12","a13","a14","a15","a16","a17","a18","a19","a20","a21","a22","a23","a24","a25","a26")


 //Create implicit formatter
 implicit val sampleBiggerClassFormatter : Format[SampleBiggerClass] = Jsonx.formatCaseClass[SampleBiggerClass]
}
 

In the given example we define the case class SampleBiggerClass which have 26 fields for using case class we need to define the implicit formatter which uses the Jsonx for formatting the case class.

For this formatting we also the implict NameEncoder so for this we import it from ai.x.play.json.

Now we can use this case class for converting into json or use for differnt purposes.

 val sampleJsonData = Json.toJson(sampleBiggerClass)
 assert(sampleBiggerClass == sampleJsonData.as[SampleBiggerClass] )

We can also use this library for different purposes like-De-/Serialize tuples,De-/Serialize single value classes,also below given example shows how to deserialize case classes which have default value fields.

deserialization uses default values:-

case class SampleClass(s: String, i: Int = 9)
implicit  valformat:OFormat[SampleClass]=Jsonx.formatCaseClassUseDefaults[SampleClass]
println(assert(SampleClass("akaak",9) == Json.parse("""{"s":"akaak"}""").validate[SampleClass].get))

Consclusion

This blog gives the practical implementation of how to format case class having more than 22 fields.

This librarary is very helpful if you were using play-json and encounter the problem of lage case classes.

References

Scaladex –https://index.scala-lang.org/bizzabo/play-json-extensions/play-json-extensions/0.42.1?binaryVersion=_2.12

knoldus

Written by 

Akash Kumar is a Software Consultant at Knoldus Software LLP. He has done B.tech from Abdul Kalam Technical University. He is majorly focused on Scala and Angular. On the personnel side, he loves to play Cricket and online video games.