argonaut

Use Argonaut in Play framework with Reads and Writes

我只是一个虾纸丫 提交于 2020-01-07 09:02:07
问题 I know how to do json parsing using play json library for play application. For example I have following code: class PersonController extends Controller { case class Person(age: Int, name: String) implicit val personReads = Json.reads[Person] implicit val personWrites = Json.writes[Person] def create = Action(parse.json) { implicit request => rs.body.validate[Person] match { case s: JsSuccess => ... case e: JsError => ... } } } How should I write the code like Reads and Writes using Argonaut

Decoding Json with Circe when fields are incomplete

僤鯓⒐⒋嵵緔 提交于 2019-12-24 01:29:11
问题 I have a transcript in json format with a bunch of words in it { "words": [{ "duration": 123, "name": "world" "time": 234, "speaker": null }] } I have been using Circe to encode/decode Json. In this particular case: import io.circe.generic.auto._ import io.circe.parser._ val decoded = decode[Transcript](transcriptJson) And my ADT look like: case class Word( duration: Double, name: String, time: Float, para: String, speaker: Option[String], key: Option[String] = None, strike: Option[String] =

Decoding a sealed trait in Argonaut based on JSON structure?

戏子无情 提交于 2019-12-23 03:47:21
问题 Given the following example: sealed trait Id case class NewId(prefix: String, id: String) extends Id case class RevisedId(prefix: String, id: String, rev: String) extends Id case class User(key: Id, name: String) val json = """ { "key": { "prefix": "user", "id": "Rt01", "rev": "0-1" }, "name": "Bob Boberson" } """ implicit val CodecUser: CodecJson[User] = casecodec2(User.apply, User.unapply)("key", "name") implicit val CodecId: CodecJson[Id] = ??? json.decodeOption[User] I need to write a

What are the problems with an ADT encoding that associates types with data constructors? (Such as Scala.)

不羁的心 提交于 2019-12-18 09:59:28
问题 In Scala, algebraic data types are encoded as sealed one-level type hierarchies. Example: -- Haskell data Positioning a = Append | AppendIf (a -> Bool) | Explicit ([a] -> [a]) // Scala sealed trait Positioning[A] case object Append extends Positioning[Nothing] case class AppendIf[A](condition: A => Boolean) extends Positioning[A] case class Explicit[A](f: Seq[A] => Seq[A]) extends Positioning[A] With case class es and case object s, Scala generates a bunch of things like equals , hashCode ,

decode and encode with argonaut

ε祈祈猫儿з 提交于 2019-12-12 03:34:47
问题 I would like to use the public API of guildWars2 and decode and encode some informations . I am searching for the decode and encode withs argonauts but I did not understand well .can you explain to me the fact of using argonaut. thanks 来源: https://stackoverflow.com/questions/35460608/decode-and-encode-with-argonaut

Scalaz validation with Argonaut

一笑奈何 提交于 2019-12-11 04:04:09
问题 I have a case class and companion object: case class Person private(name: String, age: Int) object Person { def validAge(age: Int) = { if (age > 18) age.successNel else "Age is under 18".failureNel } def validName(name: String) = { name.successNel } def create(name: String, age: Int) = (validAge(age) |@| validName(name))(Person.apply) } I want to use Argonaut to parse some JSON and return a Person OR some errors, as a list. So I need to: Read the JSON from a string, and validate the string is

Encoding nested classes using scala argonaut

谁说胖子不能爱 提交于 2019-12-10 02:44:53
问题 I'm trying to encode/decode following case class case class Person(name: String, age: Int, childs: List[Person]) using the following code: object Person { implicit def PersonCodecJson = casecodec3(Person.apply, Person.unapply)("name", "age", "childs") } with argonaut, but I'm getting the following compiler error: could not find implicit value for evidence parameter of type argonaut.EncodeJson[List[Person]] Obviously, the compiler doesn't know how to handle encoding of List[Person], because it

Decoding a sealed trait in Argonaut based on JSON structure?

夙愿已清 提交于 2019-12-07 13:15:27
Given the following example: sealed trait Id case class NewId(prefix: String, id: String) extends Id case class RevisedId(prefix: String, id: String, rev: String) extends Id case class User(key: Id, name: String) val json = """ { "key": { "prefix": "user", "id": "Rt01", "rev": "0-1" }, "name": "Bob Boberson" } """ implicit val CodecUser: CodecJson[User] = casecodec2(User.apply, User.unapply)("key", "name") implicit val CodecId: CodecJson[Id] = ??? json.decodeOption[User] I need to write a CodecJson for Id that will decode an object when it has the proper structure. Adding a discriminator field

Update case class from incomplete JSON with Argonaut or Circe

这一生的挚爱 提交于 2019-12-05 17:43:10
问题 I need to create an updated instance from a case class instance (with any needed DecodeJson s implicitly derived), given an incomplete json (some fields missing). How can this be accomplished with Argonaut (preferably) or Circe (if I have to)? Example: case class Person(name:String, age:Int) val person = Person("mr complete", 42) val incompletePersonJson = """{"name":"mr updated"}""" val updatedPerson = updateCaseClassFromIncompleteJson(person, incompletePersonJson) println(updatedPerson) /