How to emit JSON for a class? #32

Closed
mslinn opened this Issue Nov 8, 2011 · 1 comment

Projects

None yet

2 participants

@mslinn
mslinn commented Nov 8, 2011

I'd like to be able to convert this class into JSON so it could be assigned to a JValue:

case class TextMatch(val workCellRef:ActorRef, val length:Int = 0, val startPos:Int = 0, val endPos:Int = 0)

The JSON would look like this:

{
  "TextMatch": {
    "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
  }
}

How can I do this?

I also need to turn a list of TextMatch objects into JSON, perhaps something like the following. Again, how would I do that so the list could be assigned to a JValue?

{ 
  [
    "TextMatch": {
      "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
    },
    "TextMatch": {
      "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
    },
    "TextMatch": {
      "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
    },
  ]
}

Thanks,

Mike

@jdegoes
Owner
jdegoes commented Nov 8, 2011

There's no great way to do this at present (although a very nice method is coming shortly).

For now, you can use the package blueeyes.json.xschema, which contains a notion of serialization, expressed in terms of decomposition and extraction, then write your own decomposer / extractor. Take a look at the examples in that package for more information.

Basically, it will end up looking something like:

implicit val TextMatchDecomposer = new Decomposer[TextMatch] {
def decompose(v: TextMatch) = JObject(
JField("workCellRef", v.workCellRef.serialize) ::
JField("length", v.length.serialize) ::
JField("startPos", v.startPos.serialize) ::
JField("endPos", v.endPos.serialize) :: Nil
)

Assuming appropriate implicit decomposers/extractors are in scope for ActorRef and Int types (the latter you get for free with xschema by importing DefaultSerialization._).

On Nov 7, 2011, at 5:44 PM, Mike Slinn wrote:

I'd like to be able to convert this class into JSON so it could be assigned to a JValue:

case class TextMatch(val workCellRef:ActorRef, val length:Int = 0, val startPos:Int = 0, val endPos:Int = 0)

The JSON would look like this:

{
 "TextMatch": {
   "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
 }
}

How can I do this?

I also need to turn a list of TextMatch objects into JSON, perhaps something like the following. Again, how would I do that so the list could be assigned to a JValue?

{ 
 [
   "TextMatch": {
     "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
   },
   "TextMatch": {
     "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
   },
   "TextMatch": {
     "workCellRef":"opaqueStuff", "length":123, "startPos":234, "endPos":456
   },
 ]
}

Thanks,

Mike


Reply to this email directly or view it on GitHub:
#32

@mslinn mslinn closed this Nov 25, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment