Format JSON string in scala

前端 未结 2 799
盖世英雄少女心
盖世英雄少女心 2020-12-19 11:57

Is there a straight forward way to format a JSON string in scala?

I have a JSON String like this:

val json = {\"foo\": {\"bar\": {\"baz\": T}}}
         


        
2条回答
  •  青春惊慌失措
    2020-12-19 12:42

    I thought I read somewhere that Typesafe was considering separating their JSON processing out of Play, so look into that to apply @senia's solution first.

    Otherwise, look at Jackson--or more precisely, the Scala wrapper for Jackson:

    val mapper = new ObjectMapper()
    mapper.registerModule(DefaultScalaModule)
    val writer = mapper.writerWithDefaultPrettyPrinter
    val json = writer.writeValueAsString(Object value)
    

    I've also heard that the kids are really into Scala Pickling, which apparently has pretty printing as well.

提交回复
热议问题