From 295dbc3055af7a6bf9d61a0a0b72f66735330a78 Mon Sep 17 00:00:00 2001 From: Aaron Eppert Date: Mon, 26 Oct 2015 17:55:01 -0400 Subject: [PATCH] Fix for JSON formatter In the event that the first entry in a record is optional AND not present, the serializer will incorrectly add a leading comma. This leading common is invalid JSON and will, more often than not, cause parser failures downstream. --- src/threading/formatters/JSON.cc | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/threading/formatters/JSON.cc b/src/threading/formatters/JSON.cc index 1a2fd84c4a..3e5bfe9391 100644 --- a/src/threading/formatters/JSON.cc +++ b/src/threading/formatters/JSON.cc @@ -35,8 +35,14 @@ bool JSON::Describe(ODesc* desc, int num_fields, const Field* const * fields, const u_char* bytes = desc->Bytes(); int len = desc->Len(); - if ( i > 0 && len > 0 && bytes[len-1] != ',' && vals[i]->present ) - desc->AddRaw(","); + if ( i > 0 && len > 0 && bytes[len-1] != ',' && vals[i]->present ) { + // Issue if the first value of a record is optional AND not present + // then an empty json field will be produced, which is invalid. + // - ANE - 10/26/2015 + if (len > 1 && bytes[len-1] != '{') { + desc->AddRaw(","); + } + } if ( ! Describe(desc, vals[i], fields[i]->name) ) return false;