Last active
July 15, 2017 16:09
-
-
Save kajstrom/27fa9ac6d20af4ea07227326e101446e to your computer and use it in GitHub Desktop.
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
name := """minimal-scala""" | |
version := "1.0" | |
scalaVersion := "2.11.7" | |
libraryDependencies += "com.typesafe.play" %% "play-json" % "2.5.13" |
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
object InventoryItem { | |
implicit val inventoryItemWrites: Writes[InventoryItem] = ( | |
(JsPath \ "name").write[String] and | |
(JsPath \ "serialNumber").write[String] and | |
(JsPath \ "value").write[Float] | |
)(unlift(InventoryItem.unapply)) | |
implicit val inventoryItemReads: Reads[InventoryItem] = ( | |
(JsPath \ "name").read[String] and | |
(JsPath \ "serialNumber").read[String] and | |
(JsPath \ "value").read[Float] | |
)(InventoryItem.apply _) | |
} | |
case class InventoryItem(name: String, serialNumber: String, value: Float) {} |
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
object ReadingJson extends App { | |
var inventory: List[InventoryItem] = List() | |
val itemJsonString = Source.fromFile("D:\\Path\\To\\My.json").mkString | |
val json = Json.parse(itemJsonString) | |
json.validate[List[InventoryItem]] match { | |
case s: JsSuccess[List[InventoryItem]] => inventory = s.get | |
case e: JsError => println("Oh dog, what done") | |
} | |
} |
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
[ | |
{ | |
"name": "Item", | |
"serialNumber": "asdsad", | |
"value": 22 | |
}, | |
{ | |
"name": "Another Item", | |
"serialNumber": "SRS-001", | |
"value": 33 | |
} | |
] |
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
object WritingJson extends App { | |
var inventory = List[InventoryItem]() | |
inventory = inventory :+ InventoryItem("Name", "SR-001", 33.0) | |
val json = Json.toJson(inventory) | |
println(Json.stringify(json)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment