JsonDeserializer.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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::serializeEnum(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. const JsonNode & anyOf = field["anyOf"];
  60. const JsonNode & allOf = field["allOf"];
  61. const JsonNode & noneOf = field["noneOf"];
  62. if(anyOf.Vector().empty() && allOf.Vector().empty())
  63. {
  64. //permissive mode
  65. value = standard;
  66. }
  67. else
  68. {
  69. //restrictive mode
  70. value.clear();
  71. value.resize(standard.size(), false);
  72. readLICPart(anyOf, decoder, true, value);
  73. readLICPart(allOf, decoder, true, value);
  74. }
  75. readLICPart(noneOf, decoder, false, value);
  76. }
  77. void JsonDeserializer::serializeLIC(const std::string & fieldName, LIC & value)
  78. {
  79. const JsonNode & field = current->operator[](fieldName);
  80. const JsonNode & anyOf = field["anyOf"];
  81. const JsonNode & allOf = field["allOf"];
  82. const JsonNode & noneOf = field["noneOf"];
  83. if(anyOf.Vector().empty())
  84. {
  85. //permissive mode
  86. value.any = value.standard;
  87. }
  88. else
  89. {
  90. //restrictive mode
  91. value.any.clear();
  92. value.any.resize(value.standard.size(), false);
  93. readLICPart(anyOf, value.decoder, true, value.any);
  94. }
  95. readLICPart(allOf, value.decoder, true, value.all);
  96. readLICPart(noneOf, value.decoder, true, value.none);
  97. }
  98. void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
  99. {
  100. value = current->operator[](fieldName).String();
  101. }
  102. void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
  103. {
  104. for(size_t index = 0; index < part.Vector().size(); index++)
  105. {
  106. const std::string & identifier = part.Vector()[index].String();
  107. const si32 rawId = decoder(identifier);
  108. if(rawId >= 0)
  109. {
  110. if(rawId < value.size())
  111. value[rawId] = val;
  112. else
  113. logGlobal->errorStream() << "JsonDeserializer::serializeLIC: id out of bounds " << rawId;
  114. }
  115. }
  116. }