Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print json string in one line using circe in scala

Tags:

scala

circe

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"}}**

like image 834
Avenger Avatar asked Mar 26 '20 09:03

Avenger


1 Answers

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"}}
like image 50
Mateusz Kubuszok Avatar answered Oct 12 '22 02:10

Mateusz Kubuszok