JsonDeserializer.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. const JsonVector & anyOf = field["anyOf"].Vector();
  45. const JsonVector & allOf = field["allOf"].Vector();
  46. const JsonVector & noneOf = field["noneOf"].Vector();
  47. if(anyOf.empty() && allOf.empty())
  48. {
  49. //permissive mode
  50. value = standard;
  51. }
  52. else
  53. {
  54. //restrictive mode
  55. value.clear();
  56. value.resize(standard.size(), false);
  57. for(size_t index = 0; index < anyOf.size(); index++)
  58. {
  59. const std::string & identifier = anyOf[index].String();
  60. si32 rawId = decoder(identifier);
  61. if(rawId >= 0)
  62. value[rawId] = true;
  63. }
  64. for(size_t index = 0; index < allOf.size(); index++)
  65. {
  66. const std::string & identifier = allOf[index].String();
  67. si32 rawId = decoder(identifier);
  68. if(rawId >=0)
  69. value[rawId] = true;
  70. }
  71. }
  72. for(size_t index = 0; index < noneOf.size(); index++)
  73. {
  74. const std::string & identifier = noneOf[index].String();
  75. si32 rawId = decoder(identifier);
  76. if(rawId >=0 )
  77. value[rawId] = false;
  78. }
  79. }
  80. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  81. {
  82. value = current->operator[](fieldName).String();
  83. }