...

Source file src/github.com/TylerBrock/colorjson/examples/indent.go

Documentation: github.com/TylerBrock/colorjson/examples

     1  package main
     2  
     3  import (
     4      "fmt"
     5      "github.com/TylerBrock/colorjson"
     6      "encoding/json"
     7  )
     8  
     9  func main() {
    10      str := `{
    11        "str": "foo",
    12        "num": 100,
    13        "bool": false,
    14        "null": null,
    15        "array": ["foo", "bar", "baz"],
    16        "obj": { "a": 1, "b": 2 }
    17      }`
    18  
    19      var obj map[string]interface{}
    20      json.Unmarshal([]byte(str), &obj)
    21  
    22      // Make a custom formatter with indent set
    23      f := colorjson.NewFormatter()
    24      f.Indent = 4
    25  
    26      // Marshall the Colorized JSON
    27      s, _ := f.Marshal(obj)
    28      fmt.Println(string(s))
    29  }
    30  
    31  

View as plain text