JsonDeserializer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  40. {
  41. const JsonNode & field = current->operator[](fieldName);
  42. if(field.isNull())
  43. return;
  44. auto loadPart = [&](const JsonVector & part, const bool val)
  45. {
  46. for(size_t index = 0; index < part.size(); index++)
  47. {
  48. const std::string & identifier = part[index].String();
  49. si32 rawId = decoder(identifier);
  50. if(rawId >= 0)
  51. {
  52. if(rawId < value.size())
  53. value[rawId] = val;
  54. else
  55. logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" id out of bounds " << rawId;
  56. }
  57. else
  58. {
  59. logGlobal->errorStream() << "JsonDeserializer::serializeLIC: " << fieldName <<" identifier not resolved " << identifier;
  60. }
  61. }
  62. };
  63. const JsonVector & anyOf = field["anyOf"].Vector();
  64. const JsonVector & allOf = field["allOf"].Vector();
  65. const JsonVector & noneOf = field["noneOf"].Vector();
  66. if(anyOf.empty() && allOf.empty())
  67. {
  68. //permissive mode
  69. value = standard;
  70. }
  71. else
  72. {
  73. //restrictive mode
  74. value.clear();
  75. value.resize(standard.size(), false);
  76. loadPart(anyOf, true);
  77. loadPart(allOf, true);
  78. }
  79. loadPart(noneOf, false);
  80. }
  81. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  82. {
  83. value = current->operator[](fieldName).String();
  84. }