Comments (3)
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.
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.
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)
- Publish for Scala 2.13.0 HOT 3
- Provide implicits automatically without extra imports HOT 1
- Provide jsonFormatN method overloads HOT 4
- JsonParserSpecJvm is failing HOT 3
- Documentation review and migration guide for 1.5.0 release
- How to debug "Cannot find JsonWriter or JsonFormatter type class" errors
- Not able to parse the Json file with its relation_tree and split into multiple Json file for each child without any hard coding of Node/Tag (As structure of Json is not always same also very lengthy json). Input and Output is pasted below :
- Publish 1.3.6 release (including Scala.js) HOT 1
- Scaladoc
- DefaultJsonProtocol.viaSeq looses ordering for ordered iterables HOT 10
- Is JsonParser thread safe?
- Default JsonWriter for collections
- What are your plans for spray-json regarding Scala 3? HOT 4
- Generated jsonFormatN has unexpectedly large sizeHint HOT 1
- Cross-compile for scala 3? HOT 1
- Introduce an optional Scala 3 macro module, with full pickling support.
- Is this project still maintained? HOT 2
- No implicit arguments of type: JsonReader[Double] HOT 1
- Cannot parse as Long from String format field
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spray-json.