Skip to content

Commit

Permalink
Fixes serialization of null in a collection of objects (#129)
Browse files Browse the repository at this point in the history
* Adds serialization of null in a collection'

* Fix test
  • Loading branch information
rkodev committed Feb 12, 2024
1 parent a13c7c5 commit 00f8585
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.6] - 2024-02-12

### Changed

- Fixes serilaization of `null` values in collections of Objects.

## [1.0.5] - 2024-01-10

### Changed
Expand Down
13 changes: 10 additions & 3 deletions json_serialization_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,16 @@ func (w *JsonSerializationWriter) WriteCollectionOfObjectValues(key string, coll
}
w.writeArrayStart()
for _, item := range collection {
err := w.WriteObjectValue("", item)
if err != nil {
return err
if item != nil {
err := w.WriteObjectValue("", item)
if err != nil {
return err
}
} else {
err := w.WriteNullValue("")
if err != nil {
return err
}
}
w.writePropertySeparator()
}
Expand Down
20 changes: 20 additions & 0 deletions json_serialization_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ func TestWriteInvalidAdditionalData(t *testing.T) {
assert.True(t, IsJSON("{"+stringResult+"}"))
}

func TestWriteACollectionWithNill(t *testing.T) {
serializer := NewJsonSerializationWriter()
value := "value"
serializer.WriteStringValue("key", &value)

prop1Value1 := internal.NewTestEntity()
idIntValue1 := "11"
prop1Value1.SetId(&idIntValue1)

collection := []absser.Parsable{nil, prop1Value1}
err := serializer.WriteCollectionOfObjectValues("", collection)

assert.Nil(t, err)
result, err := serializer.GetSerializedContent()

stringResult := string(result[:])
assert.Contains(t, stringResult, "null,")
assert.Contains(t, stringResult, "\"key\":\"value\",[null,{\"id\":\"11\"}]")
}

func IsJSON(str string) bool {
var js json.RawMessage
err := json.Unmarshal([]byte(str), &js)
Expand Down

0 comments on commit 00f8585

Please sign in to comment.