Skip to content

Commit

Permalink
Add new json serialization type ndpi_serialization_format_inner_json
Browse files Browse the repository at this point in the history
  • Loading branch information
cardigliano committed Oct 31, 2024
1 parent a97a130 commit 8ad34b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/include/ndpi_typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 9 additions & 3 deletions src/lib/ndpi_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -719,15 +722,17 @@ 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) {
if(serializer->status.flags & NDPI_SERIALIZER_STATUS_ARRAY)
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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 8ad34b3

Please sign in to comment.