github.com/mailru/activerecord@v1.12.2/pkg/serializer/json.go (about)

     1  package serializer
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/mailru/activerecord/pkg/serializer/errs"
     8  )
     9  
    10  func JSONUnmarshal(data string, v any) error {
    11  	err := json.Unmarshal([]byte(data), v)
    12  	if err != nil {
    13  		return fmt.Errorf("%w: %v", errs.ErrUnmarshalJSON, err)
    14  	}
    15  
    16  	return nil
    17  }
    18  
    19  func JSONMarshal(v any) (string, error) {
    20  	ret, err := json.Marshal(v)
    21  	if err != nil {
    22  		return "", fmt.Errorf("%w: %v", errs.ErrMarshalJSON, err)
    23  	}
    24  
    25  	return string(ret), nil
    26  }