github.com/argoproj/argo-events@v1.9.1/common/json.go (about)

     1  package common
     2  
     3  import "encoding/json"
     4  
     5  func MustJSON(in interface{}) string {
     6  	if data, err := json.Marshal(in); err != nil {
     7  		panic(err)
     8  	} else {
     9  		return string(data)
    10  	}
    11  }
    12  
    13  // MustUnJSON unmarshalls JSON or panics.
    14  // v - must be []byte or string
    15  // in - must be a pointer.
    16  func MustUnJSON(v interface{}, in interface{}) {
    17  	switch data := v.(type) {
    18  	case []byte:
    19  		if err := json.Unmarshal(data, in); err != nil {
    20  			panic(err)
    21  		}
    22  	case string:
    23  		MustUnJSON([]byte(data), in)
    24  	default:
    25  		panic("unknown type")
    26  	}
    27  }