-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_v2.go
More file actions
32 lines (27 loc) · 812 Bytes
/
json_v2.go
File metadata and controls
32 lines (27 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
//go:build goexperiment.jsonv2
package chix
import (
"encoding/json/jsontext"
"encoding/json/v2"
"net/http"
"strconv"
)
func DefaultJSONDecoder(opts ...json.Options) JSONDecoder {
return func(r *http.Request, v any) error {
return json.UnmarshalRead(r.Body, v, opts...)
}
}
func DefaultJSONEncoder(opts ...json.Options) JSONEncoder {
return func(w http.ResponseWriter, r *http.Request, v any) error {
var err error
if pretty, _ := strconv.ParseBool(r.FormValue("pretty")); pretty {
err = json.MarshalWrite(w, v, append(opts, jsontext.WithIndent(" "))...)
} else {
err = json.MarshalWrite(w, v)
}
return err
}
}