MCPcopy Create free account
hub / github.com/dolthub/doltgresql / EncodeJSON

Function EncodeJSON

postgres/parser/json/encode.go:175–200  ·  view source on GitHub ↗

EncodeJSON encodes a JSON value as a sequence of bytes.

(appendTo []byte, j JSON)

Source from the content-addressed store, hash-verified

173
174// EncodeJSON encodes a JSON value as a sequence of bytes.
175func 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.
203func DecodeJSON(b []byte) ([]byte, JSON, error) {

Callers 1

doRandomJSONFunction · 0.85

Calls 5

encodedMethod · 0.95
EncodeUint32AscendingFunction · 0.92
PutUint32AscendingFunction · 0.92
TypeMethod · 0.65
encodeMethod · 0.65

Tested by

no test coverage detected