Skip to content

Commit

Permalink
Merge pull request #102 from francoispqt/update/encode-reset-keys
Browse files Browse the repository at this point in the history
reset keys when encoding nested object
  • Loading branch information
francoispqt authored Mar 22, 2019
2 parents 64fc2cd + de3d2d8 commit bf5c9d9
Showing 1 changed file with 73 additions and 9 deletions.
82 changes: 73 additions & 9 deletions encode_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ func (enc *Encoder) Object(v MarshalerJSONObject) {
enc.writeByte(',')
}
enc.writeByte('{')

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

Expand All @@ -139,13 +149,17 @@ func (enc *Encoder) ObjectWithKeys(v MarshalerJSONObject, keys []string) {
enc.writeByte(',')
}
enc.writeByte('{')

var origKeys = enc.keys
var origHasKeys = enc.hasKeys
enc.hasKeys = true
enc.keys = keys

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

Expand All @@ -162,7 +176,17 @@ func (enc *Encoder) ObjectOmitEmpty(v MarshalerJSONObject) {
enc.writeByte(',')
}
enc.writeByte('{')

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

Expand All @@ -180,19 +204,29 @@ func (enc *Encoder) ObjectNullEmpty(v MarshalerJSONObject) {
return
}
enc.writeByte('{')

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

// ObjectKey adds a struct to be encoded, must be used inside an object as it will encode a key
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKey(key string, value MarshalerJSONObject) {
func (enc *Encoder) ObjectKey(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if value.IsNil() {
if v.IsNil() {
enc.grow(2 + len(key))
r := enc.getPreviousRune()
if r != '{' {
Expand All @@ -212,7 +246,17 @@ func (enc *Encoder) ObjectKey(key string, value MarshalerJSONObject) {
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
value.MarshalJSONObject(enc)

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

Expand Down Expand Up @@ -257,13 +301,13 @@ func (enc *Encoder) ObjectKeyWithKeys(key string, value MarshalerJSONObject, key
// ObjectKeyOmitEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKeyOmitEmpty(key string, value MarshalerJSONObject) {
func (enc *Encoder) ObjectKeyOmitEmpty(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
}
}
if value.IsNil() {
if v.IsNil() {
return
}
enc.grow(5 + len(key))
Expand All @@ -274,14 +318,24 @@ func (enc *Encoder) ObjectKeyOmitEmpty(key string, value MarshalerJSONObject) {
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKeyObj)
value.MarshalJSONObject(enc)

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

// ObjectKeyNullEmpty adds an object to be encoded or skips it if IsNil returns true.
// Must be used inside a slice or array encoding (does not encode a key)
// value must implement MarshalerJSONObject
func (enc *Encoder) ObjectKeyNullEmpty(key string, value MarshalerJSONObject) {
func (enc *Encoder) ObjectKeyNullEmpty(key string, v MarshalerJSONObject) {
if enc.hasKeys {
if !enc.keyExists(key) {
return
Expand All @@ -295,12 +349,22 @@ func (enc *Encoder) ObjectKeyNullEmpty(key string, value MarshalerJSONObject) {
enc.writeByte('"')
enc.writeStringEscape(key)
enc.writeBytes(objKey)
if value.IsNil() {
if v.IsNil() {
enc.writeBytes(nullBytes)
return
}
enc.writeByte('{')
value.MarshalJSONObject(enc)

var origHasKeys = enc.hasKeys
var origKeys = enc.keys
enc.hasKeys = false
enc.keys = nil

v.MarshalJSONObject(enc)

enc.hasKeys = origHasKeys
enc.keys = origKeys

enc.writeByte('}')
}

Expand Down

0 comments on commit bf5c9d9

Please sign in to comment.