Coder Social home page Coder Social logo

Comments (3)

keyno63 avatar keyno63 commented on September 2, 2024

@dr-jerry

Maybe, we can use jsonFormatN for class which contains primitive values only.
If a class contains original class, should write/read function ourself.
e.g. see Providing JsonFormats for unboxed types

case class Foo(i: Int, foo: Foo)

object PlayerJsonProtocol extends DefaultJsonProtocol {
  import OthersProtocols.FooProtocol
  implicit val fooFormat: JsonFormat[Foo] = lazyFormat(jsonFormat(Foo, "i", "foo"))
}

object OthersProtocols {
  implicit object FooProtocol extends JsonFormat[Foo] {
    def write(f: Foo): JsValue = JsObject(
      "i" -> JsNumber(f.i),
      "foo" -> write(f.foo)
    )
    def read(value: JsValue): Foo =
      value match {
        case JsObject(fields) =>
          (for {
            i <- fields.get("i").map(_.convertTo[Int])
            foo <- fields.get("foo").map(read)
          } yield Foo(i, foo)).get
        case JsNull => None.orNull
        case _ => deserializationError(s"unexpected json type for Foo, json: ${value}")
      }
  }
}

check by test

class PlayerJsonProtocolTest extends FunSuite {
  val jsonString = """
    |{
    |  "i": 1,
    |  "foo": {
    |    "i": 2,
    |    "foo": null
    |  }
    |}
    |""".stripMargin

  test("something test"){
    println(jsonString.parseJson.convertTo[Foo])
    assert(
      jsonString.parseJson.convertTo[Foo] ==
        Foo(1, Foo(2, None.orNull))
    )
  }
}

from spray-json.

jrudolph avatar jrudolph commented on September 2, 2024

No. the above code works as intended. Maybe the issue is that using just Foo as a constructor isn't working all the time?

Try jsonFormat2(Foo.apply _) instead.

from spray-json.

jrudolph avatar jrudolph commented on September 2, 2024

Maybe the issue is that using just Foo as a constructor isn't working all the time?

I.e. when Foo has a companion object. Without a companion object it works just as state above.

from spray-json.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.