Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1359

Constructing Akka HTTP requests for JSON Lines

$
0
0

@huntc wrote:

The doco is fabulous when explaining how to unmarshal a stream of JSON Lines object from an HTTP response using Spray JSON, but I couldn’t find anything on marshaling when constructing a request. I discovered the following approach, but wonder if it is the “blessed” way:

  /*
   * A JsonPrinter that produces compact JSON source without any superfluous whitespace.
   */
  private object JsonLinesCompactPrinter extends CompactPrinter {

    import java.lang.StringBuilder

    override protected def printArray(elements: Seq[JsValue],
                                      sb: StringBuilder): Unit =
      if (sb.length() > 0) {
        sb.append('[')
        printSeq(elements, sb.append(','))(print(_, sb))
        sb.append(']')
      } else {
        printSeq(elements, sb.append('\n'))(print(_, sb))
      }
  }

…and then to use:

    implicit val jsonLinesPrinter: JsonPrinter = JsonLinesCompactPrinter

    Marshal(events).to[RequestEntity]...

Thoughts? Is there a built-in way to do what I’m looking for?

Thanks.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 1359

Trending Articles