EncodeJSON encodes a JSON value as a sequence of bytes.
(appendTo []byte, j JSON)
| 153 | |
| 154 | // EncodeJSON encodes a JSON value as a sequence of bytes. |
| 155 | func EncodeJSON(appendTo []byte, j JSON) ([]byte, error) { |
| 156 | switch j.Type() { |
| 157 | case ArrayJSONType, ObjectJSONType: |
| 158 | // We just discard the JEntry in these cases. |
| 159 | var err error |
| 160 | _, appendTo, err = j.encode(appendTo) |
| 161 | if err != nil { |
| 162 | return appendTo, err |
| 163 | } |
| 164 | return appendTo, nil |
| 165 | default: // j is a scalar, so we must construct a scalar container for it at the top level. |
| 166 | // Scalar container header. |
| 167 | appendTo = encoding.EncodeUint32Ascending(appendTo, scalarContainerTag) |
| 168 | // Reserve space for scalar jEntry. |
| 169 | jEntryIdx := len(appendTo) |
| 170 | appendTo = encoding.EncodeUint32Ascending(appendTo, 0) |
| 171 | var entry jEntry |
| 172 | var err error |
| 173 | entry, appendTo, err = j.encode(appendTo) |
| 174 | if err != nil { |
| 175 | return appendTo, err |
| 176 | } |
| 177 | appendTo = encoding.PutUint32Ascending(appendTo, entry.encoded(lengthMode), jEntryIdx) |
| 178 | return appendTo, nil |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // DecodeJSON decodes a value encoded with EncodeJSON. |
| 183 | func DecodeJSON(b []byte) ([]byte, JSON, error) { |
no test coverage detected
searching dependent graphs…