Created
October 24, 2023 03:15
-
-
Save Braden-Preston/ed60f27a8ceecf67ce78d2f90c35206e to your computer and use it in GitHub Desktop.
Custom Go Type with Marshallers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package form | |
import ( | |
"encoding/json" | |
"gopkg.in/guregu/null.v4/zero" | |
) | |
type OptionalText string | |
func (s *OptionalText) MarshalJSON() ([]byte, error) { | |
var z zero.String | |
z.String = string(*s) | |
return json.Marshal(z.String) | |
} | |
func (s *OptionalText) UnmarshalJSON(data []byte) error { | |
text := string(data) | |
*s = OptionalText(text) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment