Skip to content

Commit f27677a

Browse files
committed
rename interface to JSONSerializer
1 parent ebe6dbe commit f27677a

File tree

5 files changed

+22
-22
lines changed

5 files changed

+22
-22
lines changed

bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
6565
ctype := req.Header.Get(HeaderContentType)
6666
switch {
6767
case strings.HasPrefix(ctype, MIMEApplicationJSON):
68-
if err = c.Echo().JSONCodec.Decode(c, i); err != nil {
68+
if err = c.Echo().JSONSerializer.Deserialize(c, i); err != nil {
6969
switch err.(type) {
7070
case *HTTPError:
7171
return err

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func (c *context) jsonPBlob(code int, callback string, i interface{}) (err error
465465
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
466466
return
467467
}
468-
if err = c.echo.JSONCodec.Encode(c, i, indent); err != nil {
468+
if err = c.echo.JSONSerializer.Serialize(c, i, indent); err != nil {
469469
return
470470
}
471471
if _, err = c.response.Write([]byte(");")); err != nil {
@@ -477,7 +477,7 @@ func (c *context) jsonPBlob(code int, callback string, i interface{}) (err error
477477
func (c *context) json(code int, i interface{}, indent string) error {
478478
c.writeContentType(MIMEApplicationJSONCharsetUTF8)
479479
c.response.Status = code
480-
return c.echo.JSONCodec.Encode(c, i, indent)
480+
return c.echo.JSONSerializer.Serialize(c, i, indent)
481481
}
482482

483483
func (c *context) JSON(code int, i interface{}) (err error) {

echo.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ type (
9090
HidePort bool
9191
HTTPErrorHandler HTTPErrorHandler
9292
Binder Binder
93-
JSONCodec JSONCodec
93+
JSONSerializer JSONSerializer
9494
Validator Validator
9595
Renderer Renderer
9696
Logger Logger
@@ -126,10 +126,10 @@ type (
126126
Validate(i interface{}) error
127127
}
128128

129-
// JSONCodec is the interface that encodes and decodes JSON to and from interfaces.
130-
JSONCodec interface {
131-
Encode(c Context, i interface{}, indent string) error
132-
Decode(c Context, i interface{}) error
129+
// JSONSerializer is the interface that encodes and decodes JSON to and from interfaces.
130+
JSONSerializer interface {
131+
Serialize(c Context, i interface{}, indent string) error
132+
Deserialize(c Context, i interface{}) error
133133
}
134134

135135
// Renderer is the interface that wraps the Render function.
@@ -322,7 +322,7 @@ func New() (e *Echo) {
322322
e.TLSServer.Handler = e
323323
e.HTTPErrorHandler = e.DefaultHTTPErrorHandler
324324
e.Binder = &DefaultBinder{}
325-
e.JSONCodec = &DefaultJSONCodec{}
325+
e.JSONSerializer = &DefaultJSONSerializer{}
326326
e.Logger.SetLevel(log.ERROR)
327327
e.StdLogger = stdLog.New(e.Logger.Output(), e.Logger.Prefix()+": ", 0)
328328
e.pool.New = func() interface{} {

json.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ import (
66
"net/http"
77
)
88

9-
// DefaultJSONCodec implements JSON encoding using encoding/json.
10-
type DefaultJSONCodec struct{}
9+
// DefaultJSONSerializer implements JSON encoding using encoding/json.
10+
type DefaultJSONSerializer struct{}
1111

12-
// Encode converts an interface into a json and writes it to the response.
12+
// Serialize converts an interface into a json and writes it to the response.
1313
// You can optionally use the indent parameter to produce pretty JSONs.
14-
func (d DefaultJSONCodec) Encode(c Context, i interface{}, indent string) error {
14+
func (d DefaultJSONSerializer) Serialize(c Context, i interface{}, indent string) error {
1515
enc := json.NewEncoder(c.Response())
1616
if indent != "" {
1717
enc.SetIndent("", indent)
1818
}
1919
return enc.Encode(i)
2020
}
2121

22-
// Decode reads a JSON from a request body and converts it into an interface.
23-
func (d DefaultJSONCodec) Decode(c Context, i interface{}) error {
22+
// Deserialize reads a JSON from a request body and converts it into an interface.
23+
func (d DefaultJSONSerializer) Deserialize(c Context, i interface{}) error {
2424
err := json.NewDecoder(c.Request().Body).Decode(i)
2525
if ute, ok := err.(*json.UnmarshalTypeError); ok {
2626
return NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset)).SetInternal(err)

json_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ func TestDefaultJSONCodec_Encode(t *testing.T) {
3131
// Default JSON encoder
3232
//--------
3333

34-
enc := new(DefaultJSONCodec)
34+
enc := new(DefaultJSONSerializer)
3535

36-
err := enc.Encode(c, user{1, "Jon Snow"}, "")
36+
err := enc.Serialize(c, user{1, "Jon Snow"}, "")
3737
if assert.NoError(err) {
3838
assert.Equal(userJSON+"\n", rec.Body.String())
3939
}
4040

4141
req = httptest.NewRequest(http.MethodPost, "/", nil)
4242
rec = httptest.NewRecorder()
4343
c = e.NewContext(req, rec).(*context)
44-
err = enc.Encode(c, user{1, "Jon Snow"}, " ")
44+
err = enc.Serialize(c, user{1, "Jon Snow"}, " ")
4545
if assert.NoError(err) {
4646
assert.Equal(userJSONPretty+"\n", rec.Body.String())
4747
}
@@ -70,10 +70,10 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
7070
// Default JSON encoder
7171
//--------
7272

73-
enc := new(DefaultJSONCodec)
73+
enc := new(DefaultJSONSerializer)
7474

7575
var u = user{}
76-
err := enc.Decode(c, &u)
76+
err := enc.Deserialize(c, &u)
7777
if assert.NoError(err) {
7878
assert.Equal(u, user{ID: 1, Name: "Jon Snow"})
7979
}
@@ -82,7 +82,7 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
8282
req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(invalidContent))
8383
rec = httptest.NewRecorder()
8484
c = e.NewContext(req, rec).(*context)
85-
err = enc.Decode(c, &userUnmarshalSyntaxError)
85+
err = enc.Deserialize(c, &userUnmarshalSyntaxError)
8686
assert.IsType(&HTTPError{}, err)
8787
assert.EqualError(err, "code=400, message=Syntax error: offset=1, error=invalid character 'i' looking for beginning of value, internal=invalid character 'i' looking for beginning of value")
8888

@@ -94,7 +94,7 @@ func TestDefaultJSONCodec_Decode(t *testing.T) {
9494
req = httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON))
9595
rec = httptest.NewRecorder()
9696
c = e.NewContext(req, rec).(*context)
97-
err = enc.Decode(c, &userUnmarshalTypeError)
97+
err = enc.Deserialize(c, &userUnmarshalTypeError)
9898
assert.IsType(&HTTPError{}, err)
9999
assert.EqualError(err, "code=400, message=Unmarshal type error: expected=string, got=number, field=id, offset=7, internal=json: cannot unmarshal number into Go struct field .id of type string")
100100

0 commit comments

Comments
 (0)