play-json

Play Json serialize/deserialize mutual recursive

佐手、 提交于 2019-12-13 22:25:02
问题 How to serialize and deserialize these structures using Play Json combinators? final case class WriteGroupEntity(label: String, items: Map[String, WriteEntity]) final case class WriteEntity(label: String, propertyType: String, groups: Option[Map[String, WriteGroupEntity]]) 回答1: Two days are gone and I've finally found a solution. import play.api.libs.json._ import play.api.libs.functional.syntax._ final case class GroupEntity(label: String, items: Map[String, Entity]) final case class Entity

What's the difference between Json.fromJson, as, asOpt and validate?

烈酒焚心 提交于 2019-12-13 17:22:24
问题 If I have a JsValue , which method shall I use to get values from the JsValue : Json.fromJson , as , asOpt and validate ? 回答1: It depends on the level of error handling you want. What as , asOpt , and validate all have in common is that they will attempt to de-serialize a JsValue into the type T you specify, using whatever implicit Reads[T] can be resolved. Where they differ is how they behave, and what types they return. Let's say we're working with a class Foo where we have an implicit

Ordering of Fileds on the Generated JSON using Play JSON Derived Codecs

冷暖自知 提交于 2019-12-11 15:10:07
问题 To solve one of my complex case classes to JSON mapping, I used the play json dereived codecs library. It works and produces the JSON structure that I want, but the order in which the elements are present in the JSON is nos something that I would want! For example., here is my case class: case class DataSetMetaData( dataSetWriterId: Int, dataTypeSchemaHeader: DataTypeSchemaHeader, name: String, description: LocalizedText, fields: Seq[FieldMetaData], dataSetClassId: UUID, configVersion:

compiler failure to resolve Implicit types with bounds (sometimes)

空扰寡人 提交于 2019-12-11 13:27:17
问题 Edit : Last revision was deemed unhelpful as it did not contain necessary information that help narrow down my issue. hence the need to also include the AST. Below is a library in its entirety that allows parsing and writing of play-json 's json based on user defined schema; Similar to what Scala's slick offers for database columns to some extent: import scala.language.higherKinds import play.api.libs.functional.syntax._ import play.api.libs.json._ import scala.language.{higherKinds,

Scala / Play 2.4 JSON Format issue

末鹿安然 提交于 2019-12-11 10:55:46
问题 I have following class (simplified a bit there) that would extend the JSON-format for certain objects that represent the Database-level with the ID-field: import play.api.libs.json._ import play.api.libs.functional.syntax._ class EntityFormat[T <: Entity](entityFormatter: Format[T]) extends Format[T] { val extendedFormat: Format[T] = ( __.format[T](entityFormatter) ~ (__ \ "id").format[Option[Long]] )(tupleToEntity, entityToTuple) private def tupleToEntity(e: T, id: Option[Long]) = { e.id =

make play-json read the empty string as None for a type of Option[T]

你。 提交于 2019-12-11 04:24:53
问题 I'm attempting to parse json from the GitHub API with play-json, and encountering a problem with the merge_commit_sha field on Pull Requests (incidentally, I know this field is deprecated, but don't want to discuss that in this parsing problem!). Unfortunately merge_commit_sha field comes back as the empty string in some cases: "merge_commit_sha": "" This is how the field is declared in my case class: merge_commit_sha: Option[ObjectId], I have an implicit Format[ObjectId], which does not

How to write implicit Writes for case class having more than 22 fields

扶醉桌前 提交于 2019-12-11 03:48:42
问题 case class Foo( _1:Int,_2:Int,_3:Int,_4:Int,_5:Int, _21:Int,_22:Int,_23:Int,_24:Int,_25:Int, _31:Int,_32:Int,_33:Int,_34:Int,_35:Int, _41:Int,_42:Int,_43:Int,_44:Int,_45:Int, _51:Int,_52:Int,_53:Int,_54:Int,_55:Int ) For a case class like this, I need to write implicit json de- / serializer. I tried splitting the fields, and had a JSONFormat. But still I need implicit OWrited for using Json.obj(). I tried with play-json-extensions also. Any ideas? 回答1: There's really 3 avenues for you to

Scala + Play Framework + Slick - Json as Model Field

一曲冷凌霜 提交于 2019-12-10 20:55:40
问题 I need to save a Json Field as a column of my Play Framework Model. My table parser in DAO is class Table(tag: Tag) extends Table[Model](tag, "tablename") { implicit val configFormat = Json.format[Config] // Fields ... def config = column[Config]("config", O.SqlType("JSON")) // Fields ... } Config is defined as a case class in Model in Play Model folder and has his companion object. Field of this object are Int, Double or String case class Config ( // fields ) object Config { implicit val

Play Framework JSON Fails With Ordering

老子叫甜甜 提交于 2019-12-08 13:43:09
问题 I have a huge set of case classes where below is just a subset of these case classes: sealed trait BuiltInType { def id: Int } case class ZombieType (a: String, id: Int = 0) extends BuiltInType case class BooleanType (a: Boolean, id: Int = 1) extends BuiltInType case class ByteType (a: Byte, id: Int = 2) extends BuiltInType case class UByteType (a: Byte, id: Int = 3) extends BuiltInType case class Int16Type (a: Int, id: Int = 4) extends BuiltInType case class UInt16Type (a: Int, id: Int = 5)

Play JSON Error With Many Nested Case Classes

Deadly 提交于 2019-12-08 11:31:49
问题 I have a huge set of nested case classes for which I have added implicit formatters. Initially, I was hitting some ordering problems when I declared those formatters, but as soon as I was able to sort all the ordering out of the way, I get some other errors like: Error:(57, 56) forward reference extends over definition of value zombType implicit val varFmt: OFormat[Variant] = Json.format[Variant] Here is my set of case classes which are defined in numerous different files: In a package called