JsonUtils.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * JsonUtils.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. namespace JsonUtils
  14. {
  15. /**
  16. * @brief recursively merges source into dest, replacing identical fields
  17. * struct : recursively calls this function
  18. * arrays : each entry will be merged recursively
  19. * values : value in source will replace value in dest
  20. * null : if value in source is present but set to null it will delete entry in dest
  21. * @note this function will destroy data in source
  22. */
  23. DLL_LINKAGE void merge(JsonNode & dest, JsonNode & source, bool ignoreOverride = false, bool copyMeta = false);
  24. /**
  25. * @brief recursively merges source into dest, replacing identical fields
  26. * struct : recursively calls this function
  27. * arrays : each entry will be merged recursively
  28. * values : value in source will replace value in dest
  29. * null : if value in source is present but set to null it will delete entry in dest
  30. * @note this function will preserve data stored in source by creating copy
  31. */
  32. DLL_LINKAGE void mergeCopy(JsonNode & dest, JsonNode source, bool ignoreOverride = false, bool copyMeta = false);
  33. /** @brief recursively merges descendant into copy of base node
  34. * Result emulates inheritance semantic
  35. */
  36. DLL_LINKAGE void inherit(JsonNode & descendant, const JsonNode & base);
  37. /**
  38. * @brief generate one Json structure from multiple files
  39. * @param files - list of filenames with parts of json structure
  40. */
  41. DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files);
  42. DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files, bool & isValid);
  43. /// This version loads all files with same name (overridden by mods)
  44. DLL_LINKAGE JsonNode assembleFromFiles(const std::string & filename);
  45. /**
  46. * @brief removes all nodes that are identical to default entry in schema
  47. * @param node - JsonNode to minimize
  48. * @param schemaName - name of schema to use
  49. * @note for minimizing data must be valid against given schema
  50. */
  51. DLL_LINKAGE void minimize(JsonNode & node, const std::string & schemaName);
  52. /// opposed to minimize, adds all missing, required entries that have default value
  53. DLL_LINKAGE void maximize(JsonNode & node, const std::string & schemaName);
  54. /**
  55. * @brief validate node against specified schema
  56. * @param node - JsonNode to check
  57. * @param schemaName - name of schema to use
  58. * @param dataName - some way to identify data (printed in console in case of errors)
  59. * @returns true if data in node fully compilant with schema
  60. */
  61. DLL_LINKAGE bool validate(const JsonNode & node, const std::string & schemaName, const std::string & dataName);
  62. /// get schema by json URI: vcmi:<name of file in schemas directory>#<entry in file, optional>
  63. /// example: schema "vcmi:settings" is used to check user settings
  64. DLL_LINKAGE const JsonNode & getSchema(const std::string & URI);
  65. }
  66. VCMI_LIB_NAMESPACE_END