9

I want to print json to string in one line.


    case class Data(e: Option[String])

    object Data {
      implicit val dEncoder = deriveDecoder[Data]
      implicit val dDecoder = deriveEncoder[Data]
    }

    case class Random(a: String,b: String, c: Int,d: Data)

    object Random {
      implicit val rEncoder = deriveDecoder[Random]
      implicit val rDecoder = deriveEncoder[Random]
    }

val res = Random("a","b", 1, Data("e"))

when i do res.asJson.toString, i get: { "a": "a", "b": "b", "c":1, "d": { "e": "e" } }

but i want it to be printed in one line without \n as using circe.

**{"a": "a","b": "b", "c": 1,d:{"e": "e"}}**

1 Answer 1

15

If you don't mind stripping other unnecessary whitespace characters:

@ res.asJson.noSpaces
res9: String = "{\"a\":\"a\",\"b\":\"b\",\"c\":1,\"d\":{\"e\":\"e\"}}"

@ println(res.asJson.noSpaces)
{"a":"a","b":"b","c":1,"d":{"e":"e"}}
Sign up to request clarification or add additional context in comments.

1 Comment

Then the circe doc is misleading: it refers to noSpaces as "pretty printing" which it obviously is not

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.