示例#1
0
文件: main.c 项目: yqhou/projects
int main()
{
	int i = 0;
    int rc;
	while (i++ < 1)
	{
		json_t *jso = json_new();
		json_add_string(jso, "string1", "string1_val");
		json_add_string(jso, "string2", "string2_val");
		json_add_int(jso, "integer1", 12345);
		printf("%s\n", json_get_string(jso, "string1"));
		printf("%s\n%d\n", json_get_string(jso, "string2"),
			json_get_int(jso, "integer1"));

		json_padd_string(jso, "sub-object/sub-string", "sub-string-val");
		json_padd_string(jso, "object1/sub-object/sub-string", "sub-string-val");
		char *path = "object1/sub-object1/sub-sub-object1//sub-sub-string1";
		json_padd_string(jso, path, "sub-sub-string1-val ");
		printf("sub-sub-string1=[%s]\n", json_pget_string(jso, path));
		json_add_string(jso, "string3", "string3_val");
		json_padd_string(jso, "string4", "string4_val");
//		LOG("\n%s", json_to_string(jso));

        array_t *arr = json_new_array( jso, "array0" );
        rc = json_array_add_string( arr, 0, "array-item", "array-item0-val" );
        rc = json_array_add_int( arr, 0, "array-item-int", 1234 );
        rc = json_array_add_string( arr, 1, "array-item", "array-item1-val" );
        rc = json_array_add_int( arr, 1, "array-item-int", 2234 );
        rc = json_array_add_string( arr, 2, "array-item", "array-item2-val" );
        rc = json_array_add_int( arr, 2, "array-item-int", 3234 );
        /*
        LOG( "rc = [%d][%s][%s][%s][%d]", rc, json_array_get_string( arr, 0, "array-item"),
            json_array_get_string( arr, 1, "array-item" ),
            json_array_get_string( arr, 2, "array-item" ),
            json_array_get_int( arr, 2, "array-item-int") );
            */

        //printf( "====[%d]===[%s]-=-----\n", json_array_getlen(arr),json_array_get_string( arr, 0, "array-item" ) );
        json_t *tmp1 = json_array_get_item( arr, 1 );
        //printf( "-----------\n%s\n--------\n", json_to_string(tmp1) );
		//printf("\n%s\n", json_to_string(jso));
        LOG( "\n%s\n", json_to_string(jso) );
		json_free(&jso);
	}
	//system("pause");
}
// formats update order request errors
int update_order_request_http_format_errors(
  validation_error_t **validation_errors,
  int validation_errors_count,
  json_t **json,
  json_context_t *json_context)
{
  json_t *json_return = NULL;
  char *validation_error_code = NULL;

  check_not_null(validation_errors);
  check_not_null(json);
  check_not_null(json_context);

  json_return = json_array_malloc();
  check_not_null(json_return);

  validation_error_code = calloc(1024, sizeof(char));
  check_mem(validation_error_code);

  for (int i = 0; i < validation_errors_count; i++)
  {
    char *validation_error_json = validation_errors_json[validation_errors[i]->error_code];

    if (validation_errors[i]->validation_path->property == UPDATE_ORDER_REQUEST_ORDER_ID)
    {
      check_result_greater(sprintf(validation_error_code, "order-id-%s", validation_error_json), 0);
    }
    else if (validation_errors[i]->validation_path->property == UPDATE_ORDER_REQUEST_CUSTOMER_NAME)
    {
      check_result_greater(sprintf(validation_error_code, "customer-name-%s", validation_error_json), 0);
    }
    else if (validation_errors[i]->validation_path->property == UPDATE_ORDER_REQUEST_ORDER_ITEMS)
    {
      if (validation_errors[i]->validation_path->index == -1)
      {
        check_result_greater(sprintf(validation_error_code, "order-items-%s", validation_error_json), 0);
      }
      else
      {
        check_result(
          update_order_request_order_item_http_format_error(
            validation_errors[i],
            validation_error_code),
          0);
      }
    }
    else if (validation_errors[i]->validation_path->property == UPDATE_ORDER_REQUEST_TOTAL)
    {
      check_result_greater(sprintf(validation_error_code, "total-%s", validation_error_json), 0);
    }
    else
    {
      sentinel("validation_path->property: %d", validation_errors[i]->validation_path->property);
    }

    check_result(
      json_array_add_string(
        json_return,
        validation_error_code,
        json_context),
      0);
  }

  free(validation_error_code);

  *json = json_return;

  return 0;

error:

  if (json_return != NULL) { json_free(json_return); }
  if (validation_error_code != NULL) { free(validation_error_code); }

  return -1;
}