1

I'm having an Array[Array[Byte]] and I would like to serialize it to Json in Playframework:

implicit val matrixWrites = new Writes[matrix] {
    def writes(c: Matrix): JsValue = {
  Json.obj(
        "id" -> c.id,
        "matr" -> c.matr
      )
    }
  }

However I get the error Type mismatch: found (String, Array[Array[Byte]] required (String, Json.JsValueWrapper)

What is the correct way to turn a Array[Array[Byte]] into Json?

4
  • 1
    What would that even look like as JSON? Commented Sep 28, 2014 at 21:38
  • How the JSON should look like? Commented Sep 30, 2014 at 6:00
  • possible duplicate of Handle multidimensional JSON with scala Play framework Commented Sep 30, 2014 at 6:01
  • The problem seems to be that Play can't serialize Byte Arrays to Json, when I change it to an Int Array it works fine. Commented Sep 30, 2014 at 10:46

2 Answers 2

1

Create a Writes[Array[Array[Byte]]] that serializes the array of byte array into some string format ( maybe base64 ).

You may also want to create a Reads for the same type to convert back to Array[Array[Byte]].

Sign up to request clarification or add additional context in comments.

Comments

0

On a related note, I like to wrap my binary data, so the implicit Format has not to be imported explicitly.

import org.apache.commons.codec.binary.Base64
import play.api.libs.json.{Format, Reads, Writes}

case class BinaryData(bytes: Array[Byte])

object BinaryData {
    implicit val MessageFormat: Format[BinaryData] = Format(Reads.of[String].map(s => apply(Base64.decodeBase64(s))), Writes(a => Writes.of[String].writes(Base64.encodeBase64String(a.bytes))))
}

1 Comment

This does not seem like an answer. If you are attempting to comment on a question or answer, please do so as you are able.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.