gitee.com/go-spring2/spring-base@v1.1.3/json/json.go (about) 1 /* 2 * Copyright 2012-2019 the original author or 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 * https://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 json 18 19 import ( 20 "encoding/json" 21 "io" 22 ) 23 24 type WrapError struct { 25 err error 26 } 27 28 func (w *WrapError) Error() string { 29 return w.err.Error() 30 } 31 32 // Encoder encodes into byte sequence. 33 type Encoder interface { 34 Encode(v interface{}) error 35 } 36 37 type WrapEncoder struct { 38 E Encoder 39 } 40 41 // Encode encodes into byte sequence. 42 func (w *WrapEncoder) Encode(v interface{}) error { 43 err := w.E.Encode(v) 44 if err != nil { 45 return &WrapError{err: err} 46 } 47 return nil 48 } 49 50 // Decoder decodes a byte sequence. 51 type Decoder interface { 52 Decode(v interface{}) error 53 } 54 55 type WrapDecoder struct { 56 D Decoder 57 } 58 59 // Decode reads the next JSON-encoded value from its 60 // input and stores it in the value pointed to by v. 61 func (w *WrapDecoder) Decode(v interface{}) error { 62 err := w.D.Decode(v) 63 if err != nil { 64 return &WrapError{err: err} 65 } 66 return nil 67 } 68 69 var ( 70 MarshalFunc = json.Marshal 71 MarshalIndentFunc = json.MarshalIndent 72 UnmarshalFunc = json.Unmarshal 73 NewEncoderFunc = func(w io.Writer) Encoder { return json.NewEncoder(w) } 74 NewDecoderFunc = func(r io.Reader) Decoder { return json.NewDecoder(r) } 75 ) 76 77 // Marshal returns the JSON encoding of v. 78 func Marshal(v interface{}) ([]byte, error) { 79 data, err := MarshalFunc(v) 80 if err != nil { 81 return nil, &WrapError{err: err} 82 } 83 return data, nil 84 } 85 86 // MarshalIndent is like Marshal but applies Indent to format the output. 87 func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { 88 data, err := MarshalIndentFunc(v, prefix, indent) 89 if err != nil { 90 return nil, &WrapError{err: err} 91 } 92 return data, nil 93 } 94 95 // Unmarshal parses the JSON-encoded data and stores the result 96 // in the value pointed to by v. 97 func Unmarshal(data []byte, v interface{}) error { 98 err := UnmarshalFunc(data, v) 99 if err != nil { 100 return &WrapError{err: err} 101 } 102 return nil 103 } 104 105 // NewEncoder returns a new encoder that writes to w. 106 func NewEncoder(w io.Writer) Encoder { 107 return &WrapEncoder{NewEncoderFunc(w)} 108 } 109 110 // NewDecoder returns a new decoder that reads from r. 111 func NewDecoder(r io.Reader) Decoder { 112 return &WrapDecoder{NewDecoderFunc(r)} 113 }