Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}