MCPcopy Create free account
hub / github.com/francoispqt/gojay / ObjectKeyWithKeys

Method ObjectKeyWithKeys

encode_object.go:265–299  ·  view source on GitHub ↗

ObjectKeyWithKeys adds a struct to be encoded, must be used inside an object as it will encode a key. Value must implement MarshalerJSONObject. It will only encode the keys in keys.

(key string, value MarshalerJSONObject, keys []string)

Source from the content-addressed store, hash-verified

263// ObjectKeyWithKeys adds a struct to be encoded, must be used inside an object as it will encode a key.
264// Value must implement MarshalerJSONObject. It will only encode the keys in keys.
265func (enc *Encoder) ObjectKeyWithKeys(key string, value MarshalerJSONObject, keys []string) {
266 if enc.hasKeys {
267 if !enc.keyExists(key) {
268 return
269 }
270 }
271 if value.IsNil() {
272 enc.grow(2 + len(key))
273 r := enc.getPreviousRune()
274 if r != '{' {
275 enc.writeByte(',')
276 }
277 enc.writeByte('"')
278 enc.writeStringEscape(key)
279 enc.writeBytes(objKeyObj)
280 enc.writeByte('}')
281 return
282 }
283 enc.grow(5 + len(key))
284 r := enc.getPreviousRune()
285 if r != '{' {
286 enc.writeByte(',')
287 }
288 enc.writeByte('"')
289 enc.writeStringEscape(key)
290 enc.writeBytes(objKeyObj)
291 var origKeys = enc.keys
292 var origHasKeys = enc.hasKeys
293 enc.hasKeys = true
294 enc.keys = keys
295 value.MarshalJSONObject(enc)
296 enc.hasKeys = origHasKeys
297 enc.keys = origKeys
298 enc.writeByte('}')
299}
300
301// ObjectKeyOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
302// Must be used inside a slice or array encoding (does not encode a key)

Callers 1

TestEncodeObjectWithKeysFunction · 0.95

Calls 8

keyExistsMethod · 0.95
growMethod · 0.95
getPreviousRuneMethod · 0.95
writeByteMethod · 0.95
writeStringEscapeMethod · 0.95
writeBytesMethod · 0.95
IsNilMethod · 0.65
MarshalJSONObjectMethod · 0.65

Tested by 1

TestEncodeObjectWithKeysFunction · 0.76