webutils.cpp 975 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "webutils.hpp"
  4. #include <string>
  5. #include <algorithm>
  6. #include <iostream>
  7. #include <nlohmann/json.hpp>
  8. /**
  9. * Merge patch into orig recursively
  10. * if merge_null is true, this operates like json::merge_patch
  11. * if merge_null is false, then if patch has null, it does not override orig
  12. * Returns true on success
  13. */
  14. bool mergeData(nlohmann::json &orig, const nlohmann::json &patch, bool merge_null) {
  15. if (!patch.is_object()) {
  16. // then it's a value
  17. if ((patch.is_null() && merge_null) || (!patch.is_null())) {
  18. orig = patch;
  19. }
  20. return true;
  21. }
  22. if (!orig.is_object()) {
  23. orig = nlohmann::json::object();
  24. }
  25. for (auto it = patch.begin(); it != patch.end(); ++it) {
  26. if (it.value().is_null()) {
  27. if (merge_null) {
  28. orig.erase(it.key());
  29. }
  30. } else {
  31. mergeData(orig[it.key()], it.value(), merge_null);
  32. }
  33. }
  34. return true;
  35. }