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