zer interface  
    func (JSONSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {  
      return json.Marshal(fieldValue)  
    }  
      
  
---  
  
And registered with the following code:

    
    
    schema.RegisterSerializer("json", JSONSerializer{})  
      
  
---  
  
After registering a serializer, you can use it with the ` serializer ` tag,
for example:

    
    
    type User struct {  
      Name []byte `gorm:"serializer:json"`  
    }  
      
  
---  
  
##  Customized Serializer Type

You can use a registered serializer with tags, you are also allowed to create
a customized struct that implements the above ` SerializerInterface ` and use
it as a field type directly, for example:

    
    
    type EncryptedString string  
      
    // ctx: contains request-scoped values  
    // field: the field using the serializer, contains GORM settings, struct tags  
    // dst: current model value, `user` in the below example  
    // dbValue: current field's value in database  
    func (es *EncryptedString) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) (err error) {  
      switch value := dbValue.(type) {  
      case []byte:  
        *es = EncryptedString(bytes.TrimPrefix(value, []byte("hello")))  
      case string:  
        *es = EncryptedString(strings.TrimPrefix(value, "hello"))  
      default:  
        return fmt.Errorf("unsupported data %#v", dbValue)  
      }  
      return nil  
    }  
      
    // ctx: contains request-scoped values  
    // field: the field using the serializer, contains GORM settings, struct tags  
    // dst: current model value, `user` in the below example  
    // fieldValue: current field's value of the dst  
    func (es EncryptedString) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {  
      return "hello" + string(es), nil  
    }  
      
    type User struct {  
      gorm.Model  
      Password EncryptedString  
    }  
      
    data := User{  
      Password: EncryptedString("pass"),  
    }  
      
    DB.Create(&data)  
    // INSERT INTO `serializer_structs` (`password`) VALUES ("hellopass")  
      
    var result User  
    DB.First(&result, "id = ?", data.ID)  
    // result => User{  
    //   Password: EncryptedString("pass"),  
    // }  
      
    DB.Where(User{Password: EncryptedString("pass")}).Take(&result)  
    // SELECT * FROM `users` WHERE `users`.`password` = "hellopass"  
      
  
---  
  
[ ![GitHub tag \(latest SemVer\)](https://img.shields.io/github/v/tag/go-
gorm/gorm?label=Latest GORM Release&color=red&&style=for-the-
badge&logo=go&logoColor=red) ](v2_release_note.html)

Last updated: 2024-04-13  [ __ Prev  ](sharding.html "Sharding") [ Next  __
](prometheus.html "Prometheus")

#  Platinum Sponsors __

[ Become a Sponsor! ](/contribute.html#Donations)

#  Gold Sponsors __

[ ![Encore](/sponsors-imgs/encore.png)
](https://www.encore.dev/?utm_source=github&utm_medium=github-gorm-sponsor-
logo&utm_campaign=gorm-20231215 "Encore")

[ ![Incident.io](/sponsors-imgs/incident-io.svg) ](https://incident.io
"Incident.io")

[ Become a Sponsor! ](/contribute.html#Donations)

#  Platinum Sponsors

[ Become a Sponsor! ](/contribute.html#Donations)

#  Gold Sponsors

[ ![Encore](/sponsors-imgs/encore.png)
](https://www.encore.dev/?utm_source=github&utm_medium=github-gorm-sponsor-
logo&utm_campaign=gorm-20231215 "Encore")

[ ![Incident.io](/sponsors-imgs/incident-io.svg) ](https://incident.io
"Incident.io")

[ Become a Sponsor! ](/contribute.html#Donations)

**Contents**

  1. Register Serializer 
  2. Customized Serializer Type 

[ Improve this page ](https://github.com/go-
gorm/gorm.io/edit/master/pages/docs/serializer.md) Back to Top

**Getting Started** [ Overview ](index.html) [ Declaring Models ](models.html)
[ Connecting to Database ](connecting_to_the_database.html) **CRUD Inter