vitess.io/vitess@v0.16.2/go/jsonutil/json.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package jsonutil contains json-related utility functions 18 package jsonutil 19 20 import ( 21 "bytes" 22 "encoding/json" 23 ) 24 25 // MarshalNoEscape is the same functionality as json.Marshal but 26 // with HTML escaping disabled 27 func MarshalNoEscape(v any) ([]byte, error) { 28 buf := bytes.Buffer{} 29 enc := json.NewEncoder(&buf) 30 enc.SetEscapeHTML(false) 31 err := enc.Encode(v) 32 if err != nil { 33 return nil, err 34 } 35 return buf.Bytes(), nil 36 } 37 38 // MarshalIndentNoEscape is the same functionality as json.MarshalIndent but with HTML escaping 39 // disabled 40 func MarshalIndentNoEscape(v any, prefix, indent string) ([]byte, error) { 41 buf := bytes.Buffer{} 42 enc := json.NewEncoder(&buf) 43 enc.SetEscapeHTML(false) 44 enc.SetIndent(prefix, indent) 45 err := enc.Encode(v) 46 if err != nil { 47 return nil, err 48 } 49 return buf.Bytes(), nil 50 }