Skip to content

Instantly share code, notes, and snippets.

@deepankar14693
Created May 9, 2018 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepankar14693/41044ec6062d4e696dc2ee7b7dcbeb87 to your computer and use it in GitHub Desktop.
Save deepankar14693/41044ec6062d4e696dc2ee7b7dcbeb87 to your computer and use it in GitHub Desktop.
import org.json4s.{DefaultFormats, Formats, JNothing, JValue}
import org.json4s.native.JsonMethods.{parse => jParser}
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{write => jWrite}
trait JsonHelper extends {
val EMPTY_STRING = ""
implicit val serialization: Serialization.type = Serialization
implicit val formats: Formats = DefaultFormats
/*
takes any type of value and serializes it to string
eg: val student = Student(1,"deepankar")
write(student) will serialize it to {"rollNum":1,"name":"deepankar"}
*/
def write[T <: AnyRef](value: T): String = jWrite(value)
/*
Any valid json can be parsed into internal AST(Abstract Syntax Tree) format.
eg : If this is our json string coming from a post request
val jsonString = {"rollNum":1,"name":"deepankar"}
parse(jsonString) will parse it to something like
JObject(List((rollNum,JInt(1)), (name,JString(deepankar))))
*/
protected def parse(value: String): JValue = jParser(value)
/*
extract method is used after parsing the json string, as parse method returns JValue so we
can map this JValue to our target scala data model
eg : val jsonString = {"rollNum":1,"name":"deepankar"}
val parseString = parse(jsonString)
extract(parseString) will return Student(?1,"deepankar")
*/
implicit protected def extractOrEmptyString(json: JValue): String = {
json match {
case JNothing => EMPTY_STRING
case data => data.extract[String]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment