Skip to content

[Feature] Support UnmarshalJSON of empty strings #65

@shuqingzai

Description

@shuqingzai

Test Cases

func TestDecimalUnmarshalJSON(t *testing.T) {  
    t.Run("number", func(t *testing.T) {  
       type number struct {  
          Name    string  `json:"name"`  
          Decimal Decimal `json:"decimal"`  
       }  
       jsonStr := `[{"name": "testFloat", "decimal": 1.1},{"name": "testInt", "decimal": 10},{"name": "testString", "decimal": "10.11"},{"name": "testEmpty",""},{"name": "testNone"}]`  
       var got []number  
       err := json.Unmarshal([]byte(jsonStr), &got)  
       if err != nil {  
          t.Errorf("UnmarshalJSON(%q) failed: %v", jsonStr, err)  
       }  
       if got[0].Decimal != MustParse("1.1") {  
          t.Errorf("UnmarshalJSON(%q) = %q, want %q", jsonStr, got[0].Decimal, MustParse("1.1"))  
       }  
       if got[1].Decimal != MustParse("10") {  
          t.Errorf("UnmarshalJSON(%q) = %q, want %q", jsonStr, got[1].Decimal, MustParse("10"))  
       }  
       t.Logf("got: %+v", got)  
    })
}

When decimal is an empty string(testEmpty), UnmarshalJSON cannot be performed. Is it possible to support this? The default value is zero? Or can global configuration be added to support this option?

Background Information

When we connect to some third-party libraries, we often encounter empty string values ​​and cannot support UnmarshalJSON

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions