From 8ad34b381ca5d352dc4f877f93bc0f7625d8b28c Mon Sep 17 00:00:00 2001 From: Alfredo Cardigliano Date: Thu, 31 Oct 2024 17:36:44 +0100 Subject: [PATCH] Add new json serialization type ndpi_serialization_format_inner_json --- src/include/ndpi_typedefs.h | 4 +++- src/lib/ndpi_serializer.c | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/include/ndpi_typedefs.h b/src/include/ndpi_typedefs.h index d2931ece5ce..b09c6bc21cc 100644 --- a/src/include/ndpi_typedefs.h +++ b/src/include/ndpi_typedefs.h @@ -1653,7 +1653,8 @@ typedef enum { ndpi_serialization_format_tlv, ndpi_serialization_format_json, ndpi_serialization_format_csv, - ndpi_serialization_format_multiline_json + ndpi_serialization_format_multiline_json, /* new-line separated records */ + ndpi_serialization_format_inner_json /* no outer braces */ } ndpi_serialization_format; /* Note: @@ -1719,6 +1720,7 @@ typedef struct { char csv_separator[2]; u_int8_t has_snapshot; u_int8_t multiline_json_array; + u_int8_t inner_json; ndpi_private_serializer_status snapshot; } ndpi_private_serializer; diff --git a/src/lib/ndpi_serializer.c b/src/lib/ndpi_serializer.c index 63877cd6918..ec8bfad51d9 100644 --- a/src/lib/ndpi_serializer.c +++ b/src/lib/ndpi_serializer.c @@ -196,6 +196,9 @@ int ndpi_init_serializer_ll(ndpi_serializer *_serializer, if (fmt == ndpi_serialization_format_multiline_json) { fmt = ndpi_serialization_format_json; serializer->multiline_json_array = 1; + } else if (fmt == ndpi_serialization_format_inner_json) { + fmt = ndpi_serialization_format_json; + serializer->inner_json = 1; } serializer->fmt = fmt; @@ -719,7 +722,8 @@ static inline void ndpi_serialize_json_pre(ndpi_serializer *_serializer) { serializer->status.buffer.size_used--; /* Remove ']' */ serializer->buffer.data[serializer->status.buffer.size_used++] = ','; } - serializer->buffer.data[serializer->status.buffer.size_used++] = '{'; + if (!serializer->inner_json) + serializer->buffer.data[serializer->status.buffer.size_used++] = '{'; } else { if(!serializer->multiline_json_array) { @@ -727,7 +731,8 @@ static inline void ndpi_serialize_json_pre(ndpi_serializer *_serializer) { serializer->status.buffer.size_used--; /* Remove ']' */ } - serializer->status.buffer.size_used--; /* Remove '}' */ + if (!serializer->inner_json) + serializer->status.buffer.size_used--; /* Remove '}' */ if(serializer->status.flags & NDPI_SERIALIZER_STATUS_LIST) { if(!serializer->multiline_json_array) @@ -760,7 +765,8 @@ static inline int ndpi_serialize_json_post(ndpi_serializer *_serializer) { if(serializer->status.buffer.size_used >= serializer->buffer.size) return -1; - serializer->buffer.data[serializer->status.buffer.size_used++] = '}'; + if (!serializer->inner_json) + serializer->buffer.data[serializer->status.buffer.size_used++] = '}'; if (!serializer->multiline_json_array) { if(serializer->status.flags & NDPI_SERIALIZER_STATUS_ARRAY) {