diff --git a/common/generate_packets.py b/common/generate_packets.py index df033fcd4e..e3bf3be10d 100755 --- a/common/generate_packets.py +++ b/common/generate_packets.py @@ -1476,11 +1476,11 @@ def _get_code_put_diff(self, location: Location) -> str: {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 @@ def _get_code_put_diff(self, location: Location) -> str: #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 @@ def _get_code_put_diff(self, location: Location) -> str: {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 @@ def _get_code_get_diff(self, location: Location) -> str: /* Enter array (start at initial element). */ {location.json_subloc} = plocation_elem_new(0); /* Enter diff array element (start at the index address). */ -{location.json_subloc}->sub_location = plocation_elem_new(0); +{location.json_subloc}->sub_location = plocation_field_new("index"); #endif /* FREECIV_JSON_CONNECTION */ while (TRUE) {{ @@ -1592,7 +1592,7 @@ def _get_code_get_diff(self, location: Location) -> str: #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}\ @@ -1601,7 +1601,7 @@ def _get_code_get_diff(self, location: Location) -> str: /* Move to the next diff array element. */ {location.json_subloc}->number++; /* Back to the index address. */ - {location.json_subloc}->sub_location->number = 0; + {location.json_subloc}->sub_location->name = "index"; #endif /* FREECIV_JSON_CONNECTION */ }} 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,