tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.44k stars 167 forks source link

add option to compact result json #31

Closed luisdavim closed 4 years ago

luisdavim commented 5 years ago

Hi, it would be cool to have a way to get the resulting json compacted.

tidwall commented 5 years ago

sjson is designed to make changes to the json as quickly as possible, which means it doesn't concern itself with reformatting the json.

I recommend the pretty package.

package main

import (
    "github.com/tidwall/pretty"
    "github.com/tidwall/sjson"
)

func main() {
    json := `{
        "hello": "jello"
    }`
    json, _ = sjson.Set(json, "hello", "pudding")
    json = string(pretty.Ugly([]byte(json)))
    println(json)
}

// output: {"hello":"pudding"}

Or, the gjson @ugly modifier.

package main

import (
    "github.com/tidwall/gjson"
    "github.com/tidwall/sjson"
)

func main() {
    json := `{
        "hello": "jello"
    }`
    json, _ = sjson.Set(json, "hello", "pudding")
    json = gjson.Get(json, "@ugly").Raw
    println(json)
}

// output: {"hello":"pudding"}