-
Notifications
You must be signed in to change notification settings - Fork 62
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
Added custom json serializer support #40
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package render | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
var jsonMarshaller Marshaller = jsonDefaultMarshaller{} | ||
|
||
type Marshaller interface { | ||
Marshal(v interface{}) ([]byte, error) | ||
Unmarshall(data []byte, v interface{}) error | ||
NewEncoder(w io.Writer) Encoder | ||
Decode(r io.Reader, v interface{}) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having
I think 2. is a bit nicer since configuring the |
||
} | ||
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, thanks for your contribution. Few comments:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, can we please confirm that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's continue this discussion in the respective issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright after #42 (comment) I'd personally prefer sticking with Encoder/Decoder checking for What are your opinions on that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I'd be at type Encoder interface {
Encode(w io.Writer, r *http.Request, ctx context.Context, v interface{}) (error)
}
type Decoder interface {
Decode(w io.Reader, r *http.Request, ctx context.Context, v interface{}) (error)
} if really needed one could pass options with the help of the How do you suggest that we proceed in this subject. Ask @josearomeroj to make the modifications (if there is a willingness to do so) or make a new PR (or something different)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could reuse the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a new PR would make sense if you don't mind There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oh I've missed this one.
Alright. I'd wait until tomorrow if @josearomeroj responds. If not I'd be willing to work on this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you see it clearly, go ahead! I'm going to be busy this week and I won't be able to work on it. |
||
|
||
type Encoder interface { | ||
SetEscapeHTML(on bool) | ||
Encode(v interface{}) error | ||
} | ||
|
||
type jsonDefaultMarshaller struct{} | ||
|
||
func (j jsonDefaultMarshaller) NewEncoder(w io.Writer) Encoder { | ||
return json.NewEncoder(w) | ||
} | ||
|
||
func (j jsonDefaultMarshaller) Decode(r io.Reader, v interface{}) error { | ||
return json.NewDecoder(r).Decode(v) | ||
} | ||
|
||
func (j jsonDefaultMarshaller) Marshal(v interface{}) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
func (j jsonDefaultMarshaller) Unmarshall(data []byte, v interface{}) error { | ||
return json.Unmarshal(data, v) | ||
} | ||
|
||
func SetJsonMarshaller(m Marshaller) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if m == nil { | ||
return | ||
} | ||
|
||
jsonMarshaller = m | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add a marshaller for XML as well so that this can be configured as well.