void axis2_json_write_node()

in src/core/transport/http/sender/axis2_json_writer.c [59:122]


void axis2_json_write_node(json_object* parent, axiom_node_t* om_node, const axutil_env_t* env)
{
    axiom_element_t* elem;
    axiom_node_t* child_node;
    const axis2_char_t* local_name;
    json_object* obj;
    json_object* array = NULL;

    if (!om_node || axiom_node_get_node_type(om_node, env) != AXIOM_ELEMENT)
        return;

    elem = (axiom_element_t*)axiom_node_get_data_element(om_node, env);
    local_name = axiom_element_get_localname(elem, env);

    child_node = axiom_node_get_first_element(om_node, env);

    /* find existing object */
    if (json_object_object_get_ex(parent, local_name, &obj))
    {
        /* check that object is array? */
        if (!json_object_is_type(obj, json_type_array))
        {
            /* convert to array */
            obj = json_object_get(obj);
            array = json_object_new_array();
            json_object_array_add(array, obj);
            json_object_object_del(parent, local_name);
            json_object_object_add(parent, local_name, array);
        }
        else
            array = obj;
    }

    if (!child_node)
    {
        /* this is a leaf node */
        json_object* json_value = NULL;

        /* check for nillable */
        if (!axis2_json_element_is_nil(elem, env))
        {
            const axis2_char_t* value =
                    axiom_element_get_text(elem, env, om_node);
            json_value = json_object_new_string(value ? value : "");
        }

        if (array)
            json_object_array_add(array, json_value);
        else
            json_object_object_add(parent, local_name, json_value);
        return;
    }

    /* iterate through children elements */
    obj = json_object_new_object();
    if (array)
        json_object_array_add(array, obj);
    else
        json_object_object_add(parent, local_name, obj);

    for (; child_node; child_node = axiom_node_get_next_sibling(child_node, env))
        if (axiom_node_get_node_type(child_node, env) == AXIOM_ELEMENT)
            axis2_json_write_node(obj, child_node, env);
}