JsonDeserializer.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * JsonDeserializer.cpp, 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. #include "StdInc.h"
  11. #include "JsonDeserializer.h"
  12. #include "../JsonNode.h"
  13. JsonDeserializer::JsonDeserializer(JsonNode & root_):
  14. JsonSerializeFormat(root_, false)
  15. {
  16. }
  17. void JsonDeserializer::serializeBool(const std::string & fieldName, bool & value)
  18. {
  19. value = current->operator[](fieldName).Bool();
  20. }
  21. void JsonDeserializer::serializeBoolEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value)
  22. {
  23. const JsonNode & tmp = current->operator[](fieldName);
  24. value = tmp.String() == trueValue;
  25. }
  26. void JsonDeserializer::serializeFloat(const std::string & fieldName, double & value)
  27. {
  28. value = current->operator[](fieldName).Float();
  29. }
  30. void JsonDeserializer::serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value)
  31. {
  32. const std::string & valueName = current->operator[](fieldName).String();
  33. si32 rawValue = vstd::find_pos(enumMap, valueName);
  34. if(rawValue < 0)
  35. value = defaultValue;
  36. else
  37. value = rawValue;
  38. }
  39. void JsonDeserializer::serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32 & value)
  40. {
  41. std::string identifier;
  42. serializeString(fieldName, identifier);
  43. if(identifier == "")
  44. {
  45. value = defaultValue;
  46. return;
  47. }
  48. si32 rawId = decoder(identifier);
  49. if(rawId >= 0)
  50. value = rawId;
  51. else
  52. value = defaultValue;
  53. }
  54. void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  55. {
  56. const JsonNode & field = current->operator[](fieldName);
  57. if(field.isNull())
  58. return;
  59. auto loadPart = [&](const JsonVector & part, const bool val)
  60. {
  61. for(size_t index = 0; index < part.size(); index++)
  62. {
  63. const std::string & identifier = part[index].String();
  64. si32 rawId = decoder(identifier);
  65. if(rawId >= 0)
  66. {
  67. if(rawId < value.size())
  68. value[rawId] = val;
  69. else
  70. logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" id out of bounds " << rawId;
  71. }
  72. else
  73. {
  74. logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" identifier not resolved " << identifier;
  75. }
  76. }
  77. };
  78. const JsonVector & anyOf = field["anyOf"].Vector();
  79. const JsonVector & allOf = field["allOf"].Vector();
  80. const JsonVector & noneOf = field["noneOf"].Vector();
  81. if(anyOf.empty() && allOf.empty())
  82. {
  83. //permissive mode
  84. value = standard;
  85. }
  86. else
  87. {
  88. //restrictive mode
  89. value.clear();
  90. value.resize(standard.size(), false);
  91. loadPart(anyOf, true);
  92. loadPart(allOf, true);
  93. }
  94. loadPart(noneOf, false);
  95. }
  96. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  97. {
  98. value = current->operator[](fieldName).String();
  99. }