github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/types/example/marshal.go (about)

     1  package example
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/lmorg/murex/lang"
     7  )
     8  
     9  func init() {
    10  	// Register data-type
    11  	lang.Marshallers["json"] = marshal
    12  }
    13  
    14  // Describe marshaller
    15  func marshal(p *lang.Process, v interface{}) ([]byte, error) {
    16  	if p.Stdout.IsTTY() {
    17  		// If STDOUT is a TTY (ie not pipe, text file or other destination other
    18  		// than a terminal) then output JSON in an indented, human readable,
    19  		// format....
    20  		return json.MarshalIndent(v, "", "    ")
    21  	}
    22  
    23  	// ....otherwise we might as well output it in a minified format
    24  	return json.Marshal(v)
    25  }