Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: marshal result of Format*Fn for all types #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Bool) Format(f fmt.State, c rune) {
}

func (s Bool) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseBool(string(ss.b))
if err != nil {
return nil, err
}
return json.Marshal(v)
}

func (s Bool) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Float32) Format(f fmt.State, c rune) {
}

func (s Float32) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseFloat(string(ss.b), 32)
if err != nil {
return nil, err
}
return json.Marshal(float32(v))
}

func (s Float32) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Float64) Format(f fmt.State, c rune) {
}

func (s Float64) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseFloat(string(ss.b), 64)
if err != nil {
return nil, err
}
return json.Marshal(v)
}

func (s Float64) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Int) Format(f fmt.State, c rune) {
}

func (s Int) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseInt(string(ss.b), 10, 0)
if err != nil {
return nil, err
}
return json.Marshal(int(v))
}

func (s Int) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Int16) Format(f fmt.State, c rune) {
}

func (s Int16) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseInt(string(ss.b), 10, 16)
if err != nil {
return nil, err
}
return json.Marshal(int16(v))
}

func (s Int16) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Int32) Format(f fmt.State, c rune) {
}

func (s Int32) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseInt(string(ss.b), 10, 32)
if err != nil {
return nil, err
}
return json.Marshal(int32(v))
}

func (s Int32) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Int64) Format(f fmt.State, c rune) {
}

func (s Int64) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseInt(string(ss.b), 10, 64)
if err != nil {
return nil, err
}
return json.Marshal(v)
}

func (s Int64) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions int8.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Int8) Format(f fmt.State, c rune) {
}

func (s Int8) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseInt(string(ss.b), 10, 8)
if err != nil {
return nil, err
}
return json.Marshal(int8(v))
}

func (s Int8) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
4 changes: 2 additions & 2 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func (s String) Format(f fmt.State, c rune) {

func (s String) MarshalJSON() ([]byte, error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return json.Marshal(string(ss.b))
}

func (s String) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Uint) Format(f fmt.State, c rune) {
}

func (s Uint) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseUint(string(ss.b), 10, 0)
if err != nil {
return nil, err
}
return json.Marshal(uint(v))
}

func (s Uint) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions uint16.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Uint16) Format(f fmt.State, c rune) {
}

func (s Uint16) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseUint(string(ss.b), 10, 16)
if err != nil {
return nil, err
}
return json.Marshal(uint16(v))
}

func (s Uint16) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Uint32) Format(f fmt.State, c rune) {
}

func (s Uint32) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseUint(string(ss.b), 10, 32)
if err != nil {
return nil, err
}
return json.Marshal(uint32(v))
}

func (s Uint32) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
14 changes: 12 additions & 2 deletions uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding"
"encoding/json"
"fmt"
"strconv"
)

var (
Expand All @@ -20,11 +21,20 @@ func (s Uint64) Format(f fmt.State, c rune) {
}

func (s Uint64) MarshalJSON() ([]byte, error) {
return json.Marshal(nil)
var ss State
s.Format(&ss, 'v')
if len(ss.b) == 0 {
return json.Marshal(nil)
}
v, err := strconv.ParseUint(string(ss.b), 10, 64)
if err != nil {
return nil, err
}
return json.Marshal(v)
}

func (s Uint64) MarshalText() (text []byte, err error) {
var ss State
s.Format(&ss, 's')
s.Format(&ss, 'v')
return ss.b, nil
}
Loading