json4s / muster

A library for macro based serializers to many different formats
http://muster.json4s.org
MIT License
33 stars 4 forks source link

[question] How to serialize this mix? #13

Closed gaydenko closed 10 years ago

gaydenko commented 10 years ago

Hi! I'd want to serialize this mess:

  case class In(i: Option[String])
  case class Out(boo: Boolean, in: In)

  val mess = Map(
    "s" -> "aString",
    "inSome" -> In(Some("forty two")),
    "inNone" -> In(None),
    "arr" -> Seq(11, 22, 33)
  )

I have tried

    val j = muster.codec.jawn.JawnCodec
    println(j.from(mess))
    val ja = muster.codec.jackson.JacksonCodec
    println(ja.from(mess))

but have got:

{"s":{},"inSome":{},"inNone":{},"arr":{}}
{"s":{},"inSome":{},"inNone":{},"arr":{}}
casualjim commented 10 years ago

yeah Map[String,Any] I don't know how to do yet. That would require runtime reflection I think.

gaydenko commented 10 years ago

Not sure it is for the next issue, so asking here :) In fact, I have tried Map and Seq as far as not found hash-like and array-like case classes in muster.ast (lets omit case classes for now). These containers are needed to craft arbitrary tree. Have I missed something?

I can clarify the intention. There are (far to be rare) use cases a resultant map is dynamic, i.e. a list of Tuple2 (as a base of the map) is creating at runtime. And the aim is to serialize such maps. At this case a model can not be modeled as a case class.

casualjim commented 10 years ago

o they exist: https://github.com/json4s/muster/blob/master/core/src/main/scala/muster/ast/ast.scala#L398-L400

But they are kind of deferred to each implementation so that for arrays for example it doesn't read the entire array but just the element it's trying to deserialize at that moment.

For jawn their implementation is this: https://github.com/json4s/muster/blob/master/codecs/jawn/src/main/scala/muster/codec/jawn/jawn_input.scala#L12-L69

gaydenko commented 10 years ago

Yes, I have found those abstract classes. But they are abstract, and those jawn implementations your are pointing to are private. How to become happy? :)

casualjim commented 10 years ago

Your use case is dynamic json generation right? Why not build a jawn ast and then get muster to serialize it?

gaydenko commented 10 years ago

Yes, it is one of the use cases. Thanks for the tip. I'm new in these areas and need more minutes to try it.

gaydenko commented 10 years ago

Heh... Still hasn't found a way muster can serialize an AstNode[_]...

casualjim commented 10 years ago

O I'm terribly sorry I haven't implemented an integration with the jawn ast integration. there is integration with the json4s ast, play-json ast and argonaut ast. it's missing an implementation like this one: https://github.com/json4s/muster/blob/master/codecs/json4s/src/main/scala/muster/codec/json4s/package.scala

gaydenko commented 10 years ago

Aha! Thanks for the clarification! Now I see I have tried to look for a black cat in a black room while there isn't a cat at all there :) But I can't implement it myself yet..