...

Source file src/oss.terrastruct.com/util-go/xjson/xjson.go

Documentation: oss.terrastruct.com/util-go/xjson

     1  // Package xjson implements JSON helpers.
     2  package xjson
     3  
     4  import (
     5  	"bytes"
     6  	"encoding/json"
     7  )
     8  
     9  // Marshal is like json.MarshalIndent but does not escape HTML.
    10  // And does not return an error
    11  func Marshal(v interface{}) []byte {
    12  	var b bytes.Buffer
    13  	e := json.NewEncoder(&b)
    14  	// Allows < and > in JSON strings without escaping which we do with SrcArrow and
    15  	// DstArrow. See https://stackoverflow.com/a/28596225
    16  	e.SetEscapeHTML(false)
    17  	e.SetIndent("", "  ")
    18  	err := e.Encode(v)
    19  	if err != nil {
    20  		buf, _ := json.Marshal(err.Error())
    21  		return buf
    22  	}
    23  	buf := b.Bytes()
    24  	// Remove trailing newline.
    25  	return buf[:len(buf)-1]
    26  }
    27  

View as plain text