22{{- $services := .Services -}}
33{{- $typeMap := .TypeMap -}}
44{{- $typePrefix := .TypePrefix -}}
5+ {{- $json := .Json -}}
56{{- $opts := .Opts -}}
67
78{{- if $services -}}
@@ -109,7 +110,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSON(ctx cont
109110 Arg{{$i}} {{template "field" dict "Name" $input.Name "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $input.Meta "JsonTags" true}}
110111 {{- end}}
111112 }{}
112- if err := json.Unmarshal(reqBody, &reqPayload); err != nil {
113+ if err := {{$ json}} .Unmarshal(reqBody, &reqPayload); err != nil {
113114 s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err))
114115 return
115116 }
@@ -138,9 +139,9 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSON(ctx cont
138139
139140 {{- if $method.Outputs | len}}
140141 {{ if $opts.fixEmptyArrays -}}
141- respBody, err := json.Marshal(initializeNilSlices(respPayload))
142+ respBody, err := {{$ json}} .Marshal(initializeNilSlices(respPayload))
142143 {{ else -}}
143- respBody, err := json.Marshal(respPayload)
144+ respBody, err := {{$ json}} .Marshal(respPayload)
144145 {{ end -}}
145146 if err != nil {
146147 s.sendErrorJSON(w, r, ErrWebrpcBadResponse.WithCausef("failed to marshal json response: %w", err))
@@ -174,7 +175,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct
174175 Arg{{$i}} {{template "field" dict "Name" $input.Name "Type" $input.Type "Optional" $input.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $input.Meta "JsonTags" true}}
175176 {{- end}}
176177 }{}
177- if err := json.Unmarshal(reqBody, &reqPayload); err != nil {
178+ if err := {{$ json}} .Unmarshal(reqBody, &reqPayload); err != nil {
178179 s.sendErrorJSON(w, r, ErrWebrpcBadRequest.WithCausef("failed to unmarshal request data: %w", err))
179180 return
180181 }
@@ -192,7 +193,7 @@ func (s *{{$serviceName}}) serve{{firstLetterToUpper $method.Name}}JSONStream(ct
192193 w.Header().Set("X-Content-Type-Options", "nosniff")
193194 w.WriteHeader(http.StatusOK)
194195
195- streamWriter := &{{firstLetterToLower $method.Name}}StreamWriter{streamWriter{w: w, f: f, e: json.NewEncoder(w), sendError: s.sendErrorJSON}}
196+ streamWriter := &{{firstLetterToLower $method.Name}}StreamWriter{streamWriter{w: w, f: f, e: {{$ json}} .NewEncoder(w), sendError: s.sendErrorJSON}}
196197 if err := streamWriter.ping(); err != nil {
197198 s.sendErrorJSON(w, r, ErrWebrpcStreamLost.WithCausef("failed to establish SSE stream: %w", err))
198199 return
@@ -226,15 +227,15 @@ func (s *{{$serviceName}}) sendErrorJSON(w http.ResponseWriter, r *http.Request,
226227 out := struct {
227228 WebRPCError WebRPCError `json:"webrpcError"`
228229 }{ WebRPCError: rpcErr }
229- json.NewEncoder(w).Encode(out)
230+ {{$ json}} .NewEncoder(w).Encode(out)
230231 return
231232 }
232233 {{- end }}
233234
234235 w.Header().Set("Content-Type", "application/json")
235236 w.WriteHeader(rpcErr.HTTPStatus)
236237
237- respBody, _ := json.Marshal(rpcErr)
238+ respBody, _ := {{$ json}} .Marshal(rpcErr)
238239 w.Write(respBody)
239240}
240241{{- end}}
@@ -248,7 +249,7 @@ func RespondWithError(w http.ResponseWriter, err error) {
248249 w.Header().Set("Content-Type", "application/json")
249250 w.WriteHeader(rpcErr.HTTPStatus)
250251
251- respBody, _ := json.Marshal(rpcErr)
252+ respBody, _ := {{$ json}} .Marshal(rpcErr)
252253 w.Write(respBody)
253254}
254255
0 commit comments