static int pb_encode_array(pb_ostream_t *stream, const pc_JSON *gprotos, const pc_JSON *protos, const pc_JSON *proto, pc_JSON *array) { pc_JSON *type = pc_JSON_GetObjectItem(proto, "type"); const char *type_text = type->valuestring; size_t len = pc_JSON_GetArraySize(array); size_t i; /* simple msg */ if (pb_get_type(type_text) && pb_get_type(type_text) != PB_string) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_varint(stream, len)) { return 0; } for (i = 0; i < len; i++) { if (!pb_encode_proto(stream, gprotos, protos, proto, pc_JSON_GetArrayItem(array, i))) { return 0; } } } else { for (i = 0; i < len; i++) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_proto(stream, gprotos, protos, proto, pc_JSON_GetArrayItem(array, i))) { return 0; } } } return 1; }
static int checkreturn pb_encode_array(pb_ostream_t *stream, const json_t *gprotos, const json_t *protos, const json_t *proto, json_t *array) { json_t *type = json_object_get(proto, "type"); const char *type_text = json_string_value(type); size_t len = json_array_size(array); size_t i; // simple msg if (pb__get_type(type_text) && pb__get_type(type_text) != PB_string) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_varint(stream, len)) { return 0; } for (i = 0; i < len; i++) { if (!pb_encode_proto(stream, gprotos, protos, proto, json_array_get(array, i))) { return 0; } } } else { for (i = 0; i < len; i++) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_proto(stream, gprotos, protos, proto, json_array_get(array, i))) { return 0; } } } return 1; }
static int checkreturn pb_encode(pb_ostream_t *stream, const json_t *gprotos, const json_t *protos, json_t *msg) { json_t *root, *value, *option, *proto; root = msg; const char *option_text; const char *key; void *iter = json_object_iter(root); while (iter) { key = json_object_iter_key(iter); value = json_object_iter_value(iter); proto = json_object_get(protos, key); if (proto) { option = json_object_get(proto, "option"); option_text = json_string_value(option); if (strcmp(option_text, "required") == 0 || strcmp(option_text, "optional") == 0) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_proto(stream, gprotos, protos, proto, value)) { return 0; } } else if (strcmp(option_text, "repeated") == 0) { if (json_is_array(value)) { if (!pb_encode_array(stream, gprotos, protos, proto, value)) { return 0; } } } } else { return 0; } iter = json_object_iter_next(root, iter); } return 1; }
static int pb_encode(pb_ostream_t *stream, const pc_JSON *gprotos, const pc_JSON *protos, pc_JSON *msg) { pc_JSON *root = msg, *value, *option, *proto; const char *option_text; const char *key; value = root->child; while (value) { key = value->string; proto = pc_JSON_GetObjectItem(protos, key); if (proto) { option = pc_JSON_GetObjectItem(proto, "option"); option_text = option->valuestring; if (strcmp(option_text, "required") == 0 || strcmp(option_text, "optional") == 0) { if (!pb_encode_tag_for_field(stream, proto)) { return 0; } if (!pb_encode_proto(stream, gprotos, protos, proto, value)) { return 0; } } else if (strcmp(option_text, "repeated") == 0) { if (value->type == pc_JSON_Array) { if (!pb_encode_array(stream, gprotos, protos, proto, value)) { return 0; } } } } else { return 0; } value = value->next; } return 1; }