diff --git a/binding/form_mapping.go b/binding/form_mapping.go index f0913ea5..187fdbd0 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -209,7 +209,13 @@ func setWithProperType(val string, value reflect.Value, field reflect.StructFiel case time.Time: return setTimeField(val, field, value) } - return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) + err1 := json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) + if err1 != nil { + if err2 := json.Unmarshal(bytesconv.StringToBytes(strconv.Quote(val)), value.Addr().Interface()); err2 != nil { + return fmt.Errorf("%v;tryping to parse with quoted json string but still failed: %v", err1, err2) + } + } + return nil case reflect.Map: return json.Unmarshal(bytesconv.StringToBytes(val), value.Addr().Interface()) default: diff --git a/binding/form_mapping_test.go b/binding/form_mapping_test.go index 2675d46b..1a527539 100644 --- a/binding/form_mapping_test.go +++ b/binding/form_mapping_test.go @@ -5,6 +5,7 @@ package binding import ( + "encoding/json" "reflect" "testing" "time" @@ -190,6 +191,46 @@ func TestMappingTime(t *testing.T) { assert.Error(t, err) } +type CustomDateTime time.Time + +const CustomDateTimeFormat = "2006/01/02 15:04:05" + +func (t CustomDateTime) String() string { + return time.Time(t).Format(CustomDateTimeFormat) +} + +func (t *CustomDateTime) UnmarshalJSON(data []byte) error { + var value string + err := json.Unmarshal(data, &value) + if err != nil { + return err + } + parseTime, err := time.Parse(CustomDateTimeFormat, value) + if err != nil { + return err + } + *t = CustomDateTime(parseTime) + return nil +} + +func TestMappingCustomStructFromJsonString(t *testing.T) { + var s struct { + Time CustomDateTime + } + value := "2020/09/23 13:20:49" + + // ok + err := mappingByPtr(&s, formSource{"Time": {value}}, "form") + assert.NoError(t, err) + expectedTime, err := time.Parse(CustomDateTimeFormat, value) + assert.NoError(t, err) + assert.Equal(t, CustomDateTime(expectedTime), s.Time) + + // parse failed + err = mappingByPtr(&s, formSource{"Time": {"20200/09/23 13:20:49"}}, "form") + assert.Error(t, err) +} + func TestMappingTimeDuration(t *testing.T) { var s struct { D time.Duration