Created
October 12, 2017 21:30
-
-
Save hongwei1/d410365d9ef35482e187c2688c4b44e1 to your computer and use it in GitHub Desktop.
All Liftweb Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.liftweb.json._ | |
import scala.text.Document | |
implicit val formats = net.liftweb.json.DefaultFormats | |
case class InternalCaseClass( | |
text: String | |
) | |
case class OutBoundCaseClass( | |
cbsToken: String, | |
backendMessages: List[InternalCaseClass], | |
owners: List[String] | |
) | |
case class InBoundCaseClass( | |
oneMore: Option[String], | |
cbsToken: Option[String], | |
backendMessages: List[InternalCaseClass], | |
owners: List[String] | |
) | |
var oneCaseClass: OutBoundCaseClass = OutBoundCaseClass("cbsToken", List(InternalCaseClass("text")), List("")) | |
val twoCaseClass: InternalCaseClass = InternalCaseClass("text") | |
//1case class -->JValue | |
val classToJValue: JValue = Extraction.decompose(twoCaseClass) | |
//2 JValue -->String | |
//2.1 JValue -->Document first. | |
val jValueToDocument: Document = render(classToJValue) | |
//2.2 Document-->String | |
val documnetToStringCompact: String = compact(jValueToDocument) | |
val documnetToStringPretty: String = pretty(jValueToDocument) | |
//2.1 and 2.2 one way: | |
val jValueToStringCompact: String = compactRender(classToJValue) | |
//3.0 String --> JValue | |
val stringToJValue: JValue = parse(jValueToStringCompact) | |
//4.0 JValue --> Case Class | |
val jvalueToCaseClass: InBoundCaseClass = Extraction.extract[InBoundCaseClass](stringToJValue) | |
val jvalueToCaseClass2: InBoundCaseClass = Extraction.extract[InBoundCaseClass](parse("{\n \"1\":\"2\"\n}")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment