Esempio n. 1
0
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
{
    if (!patch || (patch->type != cJSON_Object))
    {
        /* scalar value, array or NULL, just duplicate */
        cJSON_Delete(target);
        return cJSON_Duplicate(patch, 1);
    }

    if (!target || (target->type != cJSON_Object))
    {
        cJSON_Delete(target);
        target = cJSON_CreateObject();
    }

    patch = patch->child;
    while (patch)
    {
        if (patch->type == cJSON_NULL)
        {
            /* NULL is the indicator to remove a value, see RFC7396 */
            cJSON_DeleteItemFromObject(target, patch->string);
        }
        else
        {
            cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
            cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
        }
        patch = patch->next;
    }
    return target;
}
Esempio n. 2
0
static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive)
{
    cJSON *patch_child = NULL;

    if (!cJSON_IsObject(patch))
    {
        /* scalar value, array or NULL, just duplicate */
        cJSON_Delete(target);
        return cJSON_Duplicate(patch, 1);
    }

    if (!cJSON_IsObject(target))
    {
        cJSON_Delete(target);
        target = cJSON_CreateObject();
    }

    patch_child = patch->child;
    while (patch_child != NULL)
    {
        if (cJSON_IsNull(patch_child))
        {
            /* NULL is the indicator to remove a value, see RFC7396 */
            if (case_sensitive)
            {
                cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string);
            }
            else
            {
                cJSON_DeleteItemFromObject(target, patch_child->string);
            }
        }
        else
        {
            cJSON *replace_me = NULL;
            cJSON *replacement = NULL;

            if (case_sensitive)
            {
                replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string);
            }
            else
            {
                replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
            }

            replacement = merge_patch(replace_me, patch_child, case_sensitive);
            if (replacement == NULL)
            {
                return NULL;
            }

            cJSON_AddItemToObject(target, patch_child->string, replacement);
        }
        patch_child = patch_child->next;
    }
    return target;
}
Esempio n. 3
0
/* detach an item at the given path */
static cJSON *detach_path(cJSON *object, const unsigned char *path, const cJSON_bool case_sensitive)
{
    unsigned char *parent_pointer = NULL;
    unsigned char *child_pointer = NULL;
    cJSON *parent = NULL;
    cJSON *detached_item = NULL;

    /* copy path and split it in parent and child */
    parent_pointer = cJSONUtils_strdup(path);
    if (parent_pointer == NULL) {
        goto cleanup;
    }

    child_pointer = (unsigned char*)strrchr((char*)parent_pointer, '/'); /* last '/' */
    if (child_pointer == NULL)
    {
        goto cleanup;
    }
    /* split strings */
    child_pointer[0] = '\0';
    child_pointer++;

    parent = get_item_from_pointer(object, (char*)parent_pointer, case_sensitive);
    decode_pointer_inplace(child_pointer);

    if (cJSON_IsArray(parent))
    {
        size_t index = 0;
        if (!decode_array_index_from_pointer(child_pointer, &index))
        {
            goto cleanup;
        }
        detached_item = detach_item_from_array(parent, index);
    }
    else if (cJSON_IsObject(parent))
    {
        detached_item = cJSON_DetachItemFromObject(parent, (char*)child_pointer);
    }
    else
    {
        /* Couldn't find object to remove child from. */
        goto cleanup;
    }

cleanup:
    if (parent_pointer != NULL)
    {
        cJSON_free(parent_pointer);
    }

    return detached_item;
}
Esempio n. 4
0
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const unsigned char *path)
{
    unsigned char *parentptr = NULL;
    unsigned char *childptr = NULL;
    cJSON *parent = NULL;
    cJSON *ret = NULL;

    /* copy path and split it in parent and child */
    parentptr = cJSONUtils_strdup(path);
    if (parentptr == NULL) {
        return NULL;
    }

    childptr = (unsigned char*)strrchr((char*)parentptr, '/'); /* last '/' */
    if (childptr == NULL)
    {
        free(parentptr);
        return NULL;
    }
    /* split strings */
    *childptr++ = '\0';

    parent = cJSONUtils_GetPointer(object, (char*)parentptr);
    cJSONUtils_InplaceDecodePointerString(childptr);

    if (!parent)
    {
        /* Couldn't find object to remove child from. */
        ret = NULL;
    }
    else if (cJSON_IsArray(parent))
    {
        ret = cJSON_DetachItemFromArray(parent, atoi((char*)childptr));
    }
    else if (cJSON_IsObject(parent))
    {
        ret = cJSON_DetachItemFromObject(parent, (char*)childptr);
    }
    free(parentptr);

    /* return the detachted item */
    return ret;
}
Esempio n. 5
0
static cJSON *cJSONUtils_PatchDetach(cJSON *object, const char *path)
{
    char *parentptr = 0;
    char *childptr = 0;
    cJSON *parent = 0;
    cJSON *ret = 0;

    /* copy path and split it in parent and child */
    parentptr = cJSONUtils_strdup(path);
    childptr = strrchr(parentptr, '/'); /* last '/' */
    if (childptr)
    {
        /* split strings */
        *childptr++ = '\0';
    }
    parent = cJSONUtils_GetPointer(object, parentptr);
    cJSONUtils_InplaceDecodePointerString(childptr);

    if (!parent)
    {
        /* Couldn't find object to remove child from. */
        ret = 0;
    }
    else if (parent->type == cJSON_Array)
    {
        ret = cJSON_DetachItemFromArray(parent, atoi(childptr));
    }
    else if (parent->type == cJSON_Object)
    {
        ret = cJSON_DetachItemFromObject(parent, childptr);
    }
    free(parentptr);

    /* return the detachted item */
    return ret;
}
Esempio n. 6
0
void ICACHE_FLASH_ATTR
cJSON_DeleteItemFromObject(cJSON *object, const char *string)
{
    cJSON_Delete(cJSON_DetachItemFromObject(object, string));
}
Esempio n. 7
0
void   cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
Esempio n. 8
0
SWITCH_DECLARE(void)  cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
Esempio n. 9
0
JSONItem JSONItem::detachProperty(const wxString& name)
{
    if(!_json) { return JSONItem(NULL); }
    cJSON* j = cJSON_DetachItemFromObject(_json, name.c_str());
    return JSONItem(j);
}
Esempio n. 10
0
static void *st_poolThread(void* pArg)
{
	char buf[BUFFER_SIZE];
	cJSON *msg = NULL;

	pthread_cleanup_push(st_poolThreadCleanup, NULL);
	for(;;)
	{
		pthread_testcancel();

		if (client_read( &pool.socket, buf, sizeof(buf)) <= 0)
		{
			usleep(50000);
			continue;
		}

		RPC_TRACE("st: <- %s\n", buf);
		msg = cJSON_Parse( buf );
		if( !msg )
		{
			eprintf("%s: failed to parse message: '%s'\n", __FUNCTION__, buf);
			continue;
		}

		elcdRpcType_t type = elcdRpcInvalid;
		cJSON *value = NULL;
		cJSON *id = cJSON_GetObjectItem(msg, "id");
		if( !id || id->type != cJSON_Number || id->valueint == 0 )
		{
			eprintf("%s: missing id\n", __FUNCTION__);
			goto type_known;
		}
		value = cJSON_GetObjectItem( msg, "method" );
		if( value != NULL && value->type == cJSON_String )
		{
			type = elcdRpcRequest;
			goto type_known;
		}
		value = cJSON_DetachItemFromObject( msg, "result" );
		if( value != NULL && value->type != cJSON_NULL )
		{
			type = elcdRpcResult;
			goto type_known;
		}
		value = cJSON_DetachItemFromObject( msg, "error" );
		if( value != NULL && value->type != cJSON_NULL )
		{
			type = elcdRpcError;
		}
type_known:
		dprintf( "%s:   <- %s type %d\n", __FUNCTION__, buf, type);
		switch( type )
		{
			case elcdRpcInvalid:
				eprintf("%s: malformed message: '%s'\n", __FUNCTION__, buf);
				break;
			case elcdRpcRequest:
				/// TODO
				eprintf("%s: don't know what to do with request %s\n", __FUNCTION__, value->valuestring);
				break;
			case elcdRpcError:
			case elcdRpcResult:
			{
				int i;
				for( i = 0; i < RPC_POOL_SIZE; i++ )
					if( (unsigned int)id->valueint == pool.waiting[i].id )
					{
                        if(pool.waiting[i].callback)
                        {
                            pool.waiting[i].callback( type, value, pool.waiting[i].pArg );
                        }
						st_poolFreeAt(i);
						break;
					}
				if( i >= RPC_POOL_SIZE )
				{
					eprintf("%s: lost message %6u\n", __FUNCTION__, (unsigned int)id->valueint);
					cJSON_Delete(value);
				}
				st_poolPrint();
				break;
			}
		}

		cJSON_Delete(msg);
		msg = NULL;
	}
	pthread_cleanup_pop(1);

	pthread_exit(NULL);
}
Esempio n. 11
0
void   cJSON_DeleteItemFromObject(cJSON *object,const char *string, ngx_pool_t *pool) {cJSON_Delete(cJSON_DetachItemFromObject(object,string), pool);}
static cJSON* process_task( cJSON* input )
{
  struct TaskCommandHandler* handler;
  UTV_RESULT result;
  UTV_BOOL failedRequest = UTV_FALSE;
  int idx;
  cJSON* requests;
  cJSON* request;
  cJSON* command;
  cJSON* inputs;
  cJSON* commandName;
  UTV_UINT32 commandNameLength;
  cJSON* output;
  cJSON* responses;
  cJSON* response;
  cJSON* outputs;

  responses = cJSON_CreateArray();

  requests = cJSON_GetObjectItem( input, "REQUESTS" );

  for ( idx = 0; idx < cJSON_GetArraySize( requests ); idx++ )
  {
    request = cJSON_GetArrayItem( requests, idx );
    command = cJSON_DetachItemFromObject( request, "COMMAND" );
    inputs = cJSON_GetObjectItem( request, "INPUTS" );
    commandName = cJSON_GetObjectItem( command, "NAME" );
    commandNameLength = UtvStrlen( (UTV_BYTE*) commandName->valuestring );

    response = cJSON_CreateObject();
    outputs = cJSON_CreateArray();

    cJSON_AddItemToObject( response, "COMMAND", command );

    if ( !failedRequest )
    {
      handler = s_handler_list;

      while ( handler != NULL )
      {
        if ( commandNameLength == UtvStrlen( handler->command_name ) )
        {
          if ( !UtvMemcmp( commandName->valuestring, handler->command_name, commandNameLength ) )
          {
            result = handler->command_handler( inputs, outputs );
            break;
          }
        }

        handler = handler->next;
      }

      if ( NULL == handler )
      {
        UtvMessage( UTV_LEVEL_WARN,
                    "%s: Unknown command \"%s\"",
                    __FUNCTION__,
                    commandName->valuestring );

        result = UTV_LIVE_SUPPORT_COMMAND_UNKNOWN;
      }

      failedRequest = ( result != UTV_OK );
    }
    else
    {
        UtvMessage( UTV_LEVEL_WARN,
                    "%s: Command \"%s\" not processed",
                    __FUNCTION__,
                    commandName->valuestring );

      result = UTV_LIVE_SUPPORT_COMMAND_NOT_PROCESSED;
    }

    cJSON_AddNumberToObject( response, "RESULT", result );
    cJSON_AddItemToObject( response, "OUTPUTS", outputs );
    cJSON_AddItemToArray( responses, response );
  }

  output = cJSON_CreateObject();
  cJSON_AddItemToObject( output, "RESPONSES", responses );

  return ( output );
}