File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -51,17 +51,45 @@ func (dst *JSON) Set(src interface{}) error {
5151
5252 switch value := src .(type ) {
5353 case string :
54+ if value == "" {
55+ * dst = JSON {Bytes : []byte ("" ), Status : Null }
56+ return nil
57+ }
58+ // validate this is a valid json string
59+ err := json .Unmarshal ([]byte (value ), & struct {}{})
60+ if err != nil {
61+ return err
62+ }
5463 * dst = JSON {Bytes : []byte (value ), Status : Present }
5564 case * string :
5665 if value == nil {
5766 * dst = JSON {Status : Null }
5867 } else {
68+ if * value == "" {
69+ * dst = JSON {Bytes : []byte ("" ), Status : Null }
70+ return nil
71+ }
72+ // validate this is a valid json
73+ err := json .Unmarshal ([]byte (* value ), & struct {}{})
74+ if err != nil {
75+ return err
76+ }
5977 * dst = JSON {Bytes : []byte (* value ), Status : Present }
6078 }
6179 case []byte :
6280 if value == nil {
6381 * dst = JSON {Status : Null }
6482 } else {
83+ if string (value ) == "" {
84+ * dst = JSON {Bytes : []byte ("" ), Status : Null }
85+ return nil
86+ }
87+
88+ // validate this is a valid json
89+ err := json .Unmarshal (value , & struct {}{})
90+ if err != nil {
91+ return err
92+ }
6593 * dst = JSON {Bytes : value , Status : Present }
6694 }
6795
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ func TestJSONSet(t *testing.T) {
1313 source interface {}
1414 result JSON
1515 }{
16+ {source : "" , result : JSON {Bytes : []byte ("" ), Status : Null }},
1617 {source : "{}" , result : JSON {Bytes : []byte ("{}" ), Status : Present }},
1718 {source : []byte ("{}" ), result : JSON {Bytes : []byte ("{}" ), Status : Present }},
1819 {source : ([]byte )(nil ), result : JSON {Status : Null }},
You can’t perform that action at this time.
0 commit comments