gitlab.com/evatix-go/core@v1.3.55/coredata/corejson/New.go (about)

     1  package corejson
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"gitlab.com/evatix-go/core/errcore"
     7  	"gitlab.com/evatix-go/core/internal/reflectinternal"
     8  )
     9  
    10  func New(anyItem interface{}) Result {
    11  	jsonBytes, err := json.Marshal(anyItem)
    12  	typeName := reflectinternal.TypeName(anyItem)
    13  
    14  	if err != nil {
    15  		nextErr := errcore.
    16  			MarshallingFailedType.
    17  			Error(
    18  				err.Error(),
    19  				typeName)
    20  
    21  		return Result{
    22  			Bytes:    jsonBytes,
    23  			Error:    nextErr,
    24  			TypeName: typeName,
    25  		}
    26  	}
    27  
    28  	return Result{
    29  		Bytes:    jsonBytes,
    30  		Error:    err,
    31  		TypeName: typeName,
    32  	}
    33  }