zabbix-agent2/src/libs/zbxjson/json.c
/******************************************************************************
 *                                                                            *
 * Function: zbx_json_open_path                                               *
 *                                                                            *
 * Purpose: opens an object by definite json path                             *
 *                                                                            *
 * Return value: SUCCESS - processed successfully                             *
 *               FAIL - an error occurred                                     *
 *                                                                            *
 * Comments: Only direct path to single object in dot or bracket notation     *
 *           is supported.                                                    *
 *                                                                            *
 ******************************************************************************/
zbx_json_open_path

Purpose: opens an object by definite json path

Return value: SUCCESS - processed successfully
FAIL - an error occurred

Comments: Only direct path to single object in dot or bracket notation
is supported.
zbx_json_open_path Purpose : opens an object by definite json path Return value : SUCCESS - processed successfully FAIL - an error occurred Comments : Only direct path to single object in dot or bracket notation is supported .
int	zbx_json_open_path(const struct zbx_json_parse *jp, const char *path, struct zbx_json_parse *out)
{
	int			i, ret = FAIL;
	struct zbx_json_parse	object;
	zbx_jsonpath_t		jsonpath;
	object = *jp;
	if (FAIL == zbx_jsonpath_compile(path, &jsonpath))
		return FAIL;
	if (0 == jsonpath.definite)
	{
		zbx_set_json_strerror("cannot use indefinite path when opening sub element");
		goto out;
	}
	for (i = 0; i < jsonpath.segments_num; i++)
	{
		const char		*p;
		zbx_jsonpath_segment_t	*segment = &jsonpath.segments[i];
		if (ZBX_JSONPATH_SEGMENT_MATCH_LIST != segment->type)
		{
			zbx_set_json_strerror("jsonpath segment %d is not a name or index", i + 1);
			goto out;
		}
		if (ZBX_JSONPATH_LIST_INDEX == segment->data.list.type)
		{
			int	index;
			if ('[' != *object.start)
				goto out;
			memcpy(&index, segment->data.list.values->data, sizeof(int));
			for (p = NULL; NULL != (p = zbx_json_next(&object, p)) && 0 != index; index--)
				;
			if (0 != index || NULL == p)
			{
				zbx_set_json_strerror("array index out of bounds in jsonpath segment %d", i + 1);
				goto out;
			}
		}
		else
		{
			if (NULL == (p = zbx_json_pair_by_name(&object, (char *)&segment->data.list.values->data)))
			{
				zbx_set_json_strerror("object not found in jsonpath segment %d", i + 1);
				goto out;
			}
		}
		object.start = p;
		if (NULL == (object.end = __zbx_json_rbracket(p)))
			object.end = p + json_parse_value(p, NULL) - 1;
	}
	*out = object;
	ret = SUCCEED;
out:
	zbx_jsonpath_clear(&jsonpath);
	return ret;
}
int zbx_json_open_path ( const struct zbx_json_parse * jp , const char * path , struct zbx_json_parse * out ) { int i , ret = FAIL ; struct zbx_json_parse object ; zbx_jsonpath_t jsonpath ; object = * jp ; if ( FAIL == zbx_jsonpath_compile ( path , & jsonpath ) ) return FAIL ; if ( 0 == jsonpath . definite ) { zbx_set_json_strerror ( " " ) ; goto out ; } for ( i = 0 ; i < jsonpath . segments_num ; i ++ ) { const char * p ; zbx_jsonpath_segment_t * segment = & jsonpath . segments [ i ] ; if ( ZBX_JSONPATH_SEGMENT_MATCH_LIST != segment -> type ) { zbx_set_json_strerror ( " " , i + 1 ) ; goto out ; } if ( ZBX_JSONPATH_LIST_INDEX == segment -> data . list . type ) { int index ; if ( ' ' != * object . start ) goto out ; memcpy ( & index , segment -> data . list . values -> data , sizeof ( int ) ) ; for ( p = NULL ; NULL != ( p = zbx_json_next ( & object , p ) ) && 0 != index ; index -- ) ; if ( 0 != index || NULL == p ) { zbx_set_json_strerror ( " " , i + 1 ) ; goto out ; } } else { if ( NULL == ( p = zbx_json_pair_by_name ( & object , ( char * ) & segment -> data . list . values -> data ) ) ) { zbx_set_json_strerror ( " " , i + 1 ) ; goto out ; } } object . start = p ; if ( NULL == ( object . end = __zbx_json_rbracket ( p ) ) ) object . end = p + json_parse_value ( p , NULL ) - 1 ; } * out = object ; ret = SUCCEED ; out : zbx_jsonpath_clear ( & jsonpath ) ; return ret ; }
Function: zbx_json_open_path                                               
                                                                            
 Purpose: opens an object by definite json path                             
                                                                            
 Return value: SUCCESS - processed successfully                             
               FAIL - an error occurred                                     
                                                                            
 Comments: Only direct path to single object in dot or bracket notation     
           is supported.
Function : zbx_json_open_path Purpose : opens an object by definite json path Return value : SUCCESS - processed successfully FAIL - an error occurred Comments : Only direct path to single object in dot or bracket notation is supported .