File
Blob: waf/json.go
| 1 | package waf |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | |
| 6 | "google.golang.org/protobuf/encoding/protojson" |
| 7 | "google.golang.org/protobuf/proto" |
| 8 | ) |
| 9 | |
| 10 | // MarshalJSON emits the WAF API's field names and explicit default values. |
| 11 | func MarshalJSON(message proto.Message) ([]byte, error) { |
| 12 | return (protojson.MarshalOptions{UseProtoNames: true, EmitUnpopulated: true}).Marshal(message) |
| 13 | } |
| 14 | |
| 15 | // DecodeJSON reads one complete message and rejects unknown fields. |
| 16 | // HTTP callers must bound the reader before decoding. |
| 17 | func DecodeJSON(input io.Reader, message proto.Message) error { |
| 18 | data, err := io.ReadAll(input) |
| 19 | if err != nil { |
| 20 | return err |
| 21 | } |
| 22 | return protojson.Unmarshal(data, message) |
| 23 | } |