From 6f4c597374d5cdad9ad21a06300d0ecd1f60a601 Mon Sep 17 00:00:00 2001 From: Alina Lenk Date: Sun, 21 Apr 2024 22:26:34 +0200 Subject: [PATCH 3/3] json protocol: allow object creation in packet code See RM #496 Signed-off-by: Alina Lenk --- common/generate_packets.py | 16 ++++++++-------- common/networking/dataio_json.c | 31 +++++++++++++++++++------------ common/networking/dataio_json.h | 2 ++ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/common/generate_packets.py b/common/generate_packets.py index 4989124e9f..62bd79dbe5 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -1476,11 +1476,11 @@ differ = FALSE; {location.json_subloc}->number = -1; /* Create the diff array element. */ - e |= DIO_PUT(farray, &dout, &field_addr, 2); + e |= DIO_PUT(object, &dout, &field_addr); /* Enter diff array element (start at the index address). */ {location.json_subloc}->number = count_{location.index}++; - {location.json_subloc}->sub_location = plocation_elem_new(0); + {location.json_subloc}->sub_location = plocation_field_new("index"); #endif /* FREECIV_JSON_CONNECTION */ /* Write the index */ @@ -1488,7 +1488,7 @@ differ = FALSE; #ifdef FREECIV_JSON_CONNECTION /* Content address. */ - {location.json_subloc}->sub_location->number = 1; + {location.json_subloc}->sub_location->name = "data"; #endif /* FREECIV_JSON_CONNECTION */ {inner_put}\ @@ -1505,11 +1505,11 @@ differ = FALSE; {location.json_subloc}->number = -1; /* Create the terminating diff array element. */ - e |= DIO_PUT(farray, &dout, &field_addr, 1); + e |= DIO_PUT(object, &dout, &field_addr); /* Enter diff array element (start at the index address). */ {location.json_subloc}->number = count_{location.index}; - {location.json_subloc}->sub_location = plocation_elem_new(0); + {location.json_subloc}->sub_location = plocation_field_new("index"); #endif /* FREECIV_JSON_CONNECTION */ /* Write the sentinel value */ @@ -1571,7 +1571,7 @@ differ = FALSE; /* Enter array. */ {location.json_subloc} = plocation_elem_new(0); /* Enter diff array element. */ -{location.json_subloc}->sub_location = plocation_elem_new(0); +{location.json_subloc}->sub_location = plocation_field_new(nullptr); #endif /* FREECIV_JSON_CONNECTION */ while (TRUE) {{ @@ -1581,7 +1581,7 @@ while (TRUE) {{ /* Move to the next diff array element. */ {location.json_subloc}->number++; /* Start at the index address. */ - {location.json_subloc}->sub_location->number = 0; + {location.json_subloc}->sub_location->name = "index"; #endif /* FREECIV_JSON_CONNECTION */ /* Read next index */ @@ -1599,7 +1599,7 @@ while (TRUE) {{ #ifdef FREECIV_JSON_CONNECTION /* Content address. */ - {location.json_subloc}->sub_location->number = 1; + {location.json_subloc}->sub_location->name = "data"; #endif /* FREECIV_JSON_CONNECTION */ {inner_get}\ diff --git a/common/networking/dataio_json.c b/common/networking/dataio_json.c index 9b5fa69ef1..59ba289560 100644 --- a/common/networking/dataio_json.c +++ b/common/networking/dataio_json.c @@ -120,22 +120,12 @@ static int plocation_write_elem(json_t *item, e = json_array_set_new(item, location->number, data); } } else { - // PTZ200718 handles last element get case... - // better here than all around put_array-diff algo ? size_t n = (location->number == -1) ? (json_array_size(item) - 1) : location->number; - json_t *sub_item = json_array_get(item, n); - - if (json_is_array(sub_item)) { - e = plocation_write_data(sub_item, - location->sub_location, data); - } else { - log_error("ERROR:plocation_write_elem:array not found for sub location:" - "%s @[" SIZE_T_PRINTF "]", - json_dumps(item, JSON_DECODE_ANY), location->number); - } + e = plocation_write_data(json_array_get(item, n), + location->sub_location, data); } return e; @@ -921,6 +911,23 @@ int dio_put_farray_json(struct json_data_out *dout, return e; } +/**********************************************************************//** + Create an empty JSON object. +**************************************************************************/ +int dio_put_object_json(struct json_data_out *dout, + const struct plocation *location) +{ + int e = 0; + + if (dout->json) { + e |= plocation_write_data(dout->json, location, json_object()); + } else { + /* No caller needs this */ + } + + return e; +} + /**********************************************************************//** Insert uint32 value. **************************************************************************/ diff --git a/common/networking/dataio_json.h b/common/networking/dataio_json.h index 06b242600b..2739a83e60 100644 --- a/common/networking/dataio_json.h +++ b/common/networking/dataio_json.h @@ -130,6 +130,8 @@ bool dio_get_uint16_vec8_json(struct connection *pc, struct data_in *din, /* puts */ int dio_put_farray_json(struct json_data_out *dout, const struct plocation *location, int size); +int dio_put_object_json(struct json_data_out *dout, + const struct plocation *location); int dio_put_type_json(struct json_data_out *dout, enum data_type type, const struct plocation *location, -- 2.34.1