Correctly Serialize RubyNil values when serializing to JSON

The custom serializer introduced in #8371 did change the behavior or JSON
serialization when one of the field was a RubyNil values.

The orignal behavior was to serialize RubyNil values into their
Javascript Null. The PR #8371 changed that behavior to instead
serialized an empty string and changed the expected type.

This commit revert the behavior to serialize to Null and also tests
around the events class to prevent that.

Fixes #8386
This commit is contained in:
Pier-Hugues Pellerin 2017-09-26 10:26:22 -04:00
parent be573eb878
commit 2bdd6ec1ef
2 changed files with 6 additions and 1 deletions

View file

@ -10,6 +10,11 @@ TIMESTAMP = "@timestamp"
describe LogStash::Event do
context "to_json" do
it "should correctly serialize RubyNil values a Null values" do
e = LogStash::Event.new({ "null_value" => nil, TIMESTAMP => "2015-05-28T23:02:05.350Z"})
expect(JSON.parse(e.to_json)).to eq(JSON.parse("{\"null_value\":null,\"@timestamp\":\"2015-05-28T23:02:05.350Z\",\"@version\":\"1\"}"))
end
it "should serialize simple values" do
e = LogStash::Event.new({"foo" => "bar", "bar" => 1, "baz" => 1.0, TIMESTAMP => "2015-05-28T23:02:05.350Z"})
expect(JSON.parse(e.to_json)).to eq(JSON.parse("{\"foo\":\"bar\",\"bar\":1,\"baz\":1.0,\"@timestamp\":\"2015-05-28T23:02:05.350Z\",\"@version\":\"1\"}"))

View file

@ -314,7 +314,7 @@ public final class ObjectMappers {
@Override
public void serialize(final RubyNil value, final JsonGenerator jgen,
final SerializerProvider provider) throws IOException {
jgen.writeString("");
jgen.writeNull();
}
@Override