EncodeJSON encodes a JSON value as a sequence of bytes.
(appendTo []byte, j JSON)
| 173 | |
| 174 | // EncodeJSON encodes a JSON value as a sequence of bytes. |
| 175 | func EncodeJSON(appendTo []byte, j JSON) ([]byte, error) { |
| 176 | switch j.Type() { |
| 177 | case ArrayJSONType, ObjectJSONType: |
| 178 | // We just discard the JEntry in these cases. |
| 179 | var err error |
| 180 | _, appendTo, err = j.encode(appendTo) |
| 181 | if err != nil { |
| 182 | return appendTo, err |
| 183 | } |
| 184 | return appendTo, nil |
| 185 | default: // j is a scalar, so we must construct a scalar container for it at the top level. |
| 186 | // Scalar container header. |
| 187 | appendTo = encoding.EncodeUint32Ascending(appendTo, scalarContainerTag) |
| 188 | // Reserve space for scalar jEntry. |
| 189 | jEntryIdx := len(appendTo) |
| 190 | appendTo = encoding.EncodeUint32Ascending(appendTo, 0) |
| 191 | var entry jEntry |
| 192 | var err error |
| 193 | entry, appendTo, err = j.encode(appendTo) |
| 194 | if err != nil { |
| 195 | return appendTo, err |
| 196 | } |
| 197 | appendTo = encoding.PutUint32Ascending(appendTo, entry.encoded(lengthMode), jEntryIdx) |
| 198 | return appendTo, nil |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // DecodeJSON decodes a value encoded with EncodeJSON. |
| 203 | func DecodeJSON(b []byte) ([]byte, JSON, error) { |
no test coverage detected