You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_patch.h 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2021 Alexadru Ardelean.
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See COPYING for details.
  6. *
  7. */
  8. /**
  9. * @file
  10. * @brief JSON Patch (RFC 6902) implementation for manipulating JSON objects
  11. */
  12. #ifndef _json_patch_h_
  13. #define _json_patch_h_
  14. #include "json_pointer.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * Details of an error that occurred during json_patch_apply()
  20. */
  21. struct json_patch_error {
  22. /**
  23. * An errno value indicating what kind of error occurred.
  24. * Possible values include:
  25. * - ENOENT - A path referenced in the operation does not exist.
  26. * - EINVAL - An invalid operation or with invalid path was attempted
  27. * - ENOMEM - Unable to allocate memory
  28. * - EFAULT - Invalid arguments were passed to json_patch_apply()
  29. * (i.e. a C API error, vs. a data error like EINVAL)
  30. */
  31. int errno_code;
  32. /**
  33. * The index into the patch array of the operation that failed,
  34. * or SIZE_T_MAX for overall errors.
  35. */
  36. size_t patch_failure_idx;
  37. /**
  38. * A human readable error message.
  39. * Allocated from static storage, does not need to be freed.
  40. */
  41. const char *errmsg;
  42. };
  43. /**
  44. * Apply the JSON patch to the base object.
  45. * The patch object must be formatted as per RFC 6902, i.e.
  46. * a json_type_array containing patch operations.
  47. * If the patch is not correctly formatted, an error will
  48. * be returned.
  49. *
  50. * The json_object at *base will be modified in place.
  51. * Exactly one of *base or copy_from must be non-NULL.
  52. * If *base is NULL, a new copy of copy_from will allocated and populated
  53. * using json_object_deep_copy(). In this case json_object_put() _must_ be
  54. * used to free *base even if the overall patching operation fails.
  55. *
  56. * If anything fails during patching a negative value will be returned,
  57. * and patch_error (if non-NULL) will be populated with error details.
  58. *
  59. * @param base a pointer to the JSON object which to patch
  60. * @param patch the JSON object that describes the patch to be applied
  61. * @param copy_from a JSON object to copy to *base
  62. * @param patch_error optional, details about errors
  63. *
  64. * @return negative if an error (or not found), or 0 if patch completely applied
  65. */
  66. JSON_EXPORT int json_patch_apply(struct json_object *copy_from, struct json_object *patch,
  67. struct json_object **base, struct json_patch_error *patch_error);
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. #endif