Created
January 7, 2016 13:40
-
-
Save tyrcho/d270928abbe336169817 to your computer and use it in GitHub Desktop.
Parse and Serialize in JSON using rapture.io
This file contains hidden or 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 rapture.json._ | |
| import rapture.json.jsonBackends.jackson._ | |
| import rapture._ | |
| object JsonDemo extends App { | |
| val p1 = Person("toto", List(Address("du chat", 3, "Lille")), None) | |
| val p2 = Person("titi", List(Address("du chien", 2, "Lille")), Some("maid")) | |
| val jsonStr = Json(List(p1, p2)).toString | |
| println(jsonStr) | |
| val json = json"""[ | |
| {"addresses":[{"city":"Lille","number":3,"street":"du chat"}],"name":"toto"}, | |
| {"addresses":[{"city":"Lille","number":2,"street":"du chien"}],"name":"titi","maidenName":"maid"}] | |
| """ | |
| println(json.as[List[Person]]) | |
| val str="""[ | |
| {"addresses":[{"city":"Lille","number":3,"street":"du chat"}],"name":"tutu"}, | |
| {"addresses":[{"city":"Lille","number":2,"street":"du chien"}],"name":"titi","maidenName":"maid"}] | |
| """ | |
| println(Json.parse(str).as[List[Person]]) | |
| } | |
| case class Person(name: String, addresses: List[Address], maidenName: Option[String]) | |
| case class Address(street: String, number: Int, city: String) |
This file contains hidden or 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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>info.daviot</groupId> | |
| <version>0.1-SNAPSHOT</version> | |
| <artifactId>json-rapture-demo</artifactId> | |
| <packaging>jar</packaging> | |
| <properties> | |
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
| <scala.version>2.11.7</scala.version> | |
| <java.version>1.7</java.version> | |
| </properties> | |
| <dependencies> | |
| <dependency> | |
| <artifactId>scala-library</artifactId> | |
| <groupId>org.scala-lang</groupId> | |
| <version>${scala.version}</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.propensive</groupId> | |
| <artifactId>rapture-json-jackson_2.11</artifactId> | |
| <version>2.0.0-M3</version> | |
| </dependency> | |
| </dependencies> | |
| <build> | |
| <sourceDirectory>src/main/scala</sourceDirectory> | |
| <testSourceDirectory>src/test/scala</testSourceDirectory> | |
| <plugins> | |
| <plugin> | |
| <groupId>net.alchim31.maven</groupId> | |
| <artifactId>scala-maven-plugin</artifactId> | |
| <version>3.1.6</version> | |
| </plugin> | |
| <plugin> | |
| <groupId>org.apache.maven.plugins</groupId> | |
| <artifactId>maven-compiler-plugin</artifactId> | |
| <version>3.3</version> | |
| <configuration> | |
| <source>${java.version}</source> | |
| <target>${java.version}</target> | |
| </configuration> | |
| <executions> | |
| <execution> | |
| <phase>compile</phase> | |
| <goals> | |
| <goal>compile</goal> | |
| </goals> | |
| </execution> | |
| </executions> | |
| </plugin> | |
| </plugins> | |
| </build> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment