JsonUtils.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 JsonNode & files);
  42. DLL_LINKAGE JsonNode assembleFromFiles(const JsonNode & files, bool & isValid);
  43. DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files);
  44. DLL_LINKAGE JsonNode assembleFromFiles(const std::vector<std::string> & files, std::string modName, bool & isValid);
  45. /// This version loads all files with same name (overridden by mods)
  46. DLL_LINKAGE JsonNode assembleFromFiles(const std::string & filename);
  47. /**
  48. * @brief removes all nodes that are identical to default entry in schema
  49. * @param node - JsonNode to minimize
  50. * @param schemaName - name of schema to use
  51. * @note for minimizing data must be valid against given schema
  52. */
  53. DLL_LINKAGE void minimize(JsonNode & node, const std::string & schemaName);
  54. /// opposed to minimize, adds all missing, required entries that have default value
  55. DLL_LINKAGE void maximize(JsonNode & node, const std::string & schemaName);
  56. /**
  57. * @brief validate node against specified schema
  58. * @param node - JsonNode to check
  59. * @param schemaName - name of schema to use
  60. * @param dataName - some way to identify data (printed in console in case of errors)
  61. * @returns true if data in node fully compliant with schema
  62. */
  63. DLL_LINKAGE bool validate(const JsonNode & node, const std::string & schemaName, const std::string & dataName);
  64. /// get schema by json URI: vcmi:<name of file in schemas directory>#<entry in file, optional>
  65. /// example: schema "vcmi:settings" is used to check user settings
  66. DLL_LINKAGE const JsonNode & getSchema(const std::string & URI);
  67. /// detects potential conflicts - json entries present in both nodes
  68. /// returns JsonNode that contains list of conflicting keys
  69. /// For each conflict - list of conflicting mods and list of conflicting json values
  70. /// result[pathToKey][modID] -> node that was conflicting
  71. DLL_LINKAGE void detectConflicts(JsonNode & result, const JsonNode & left, const JsonNode & right, const std::string & keyName);
  72. }
  73. VCMI_LIB_NAMESPACE_END