JsonSerializer.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * JsonSerializer.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 "JsonSerializer.h"
  12. #include "../JsonNode.h"
  13. JsonSerializer::JsonSerializer(JsonNode & root_):
  14. JsonSerializeFormat(root_, true)
  15. {
  16. }
  17. void JsonSerializer::serializeBool(const std::string & fieldName, bool & value)
  18. {
  19. current->operator[](fieldName).Bool() = value;
  20. }
  21. void JsonSerializer::serializeBoolEnum(const std::string & fieldName, const std::string & trueValue, const std::string & falseValue, bool & value)
  22. {
  23. current->operator[](fieldName).String() = value ? trueValue : falseValue;
  24. }
  25. void JsonSerializer::serializeFloat(const std::string & fieldName, double & value)
  26. {
  27. current->operator[](fieldName).Float() = value;
  28. }
  29. void JsonSerializer::serializeIntEnum(const std::string & fieldName, const std::vector<std::string> & enumMap, const si32 defaultValue, si32 & value)
  30. {
  31. current->operator[](fieldName).String() = enumMap.at(value);
  32. }
  33. void JsonSerializer::serializeIntId(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const si32 defaultValue, si32& value)
  34. {
  35. if(defaultValue == value)
  36. return;
  37. std::string identifier = encoder(value);
  38. serializeString(fieldName, identifier);
  39. }
  40. void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  41. {
  42. assert(standard.size() == value.size());
  43. if(standard == value)
  44. return;
  45. auto & target = current->operator[](fieldName)["anyOf"].Vector();
  46. for(si32 idx = 0; idx < value.size(); idx ++)
  47. {
  48. if(value[idx])
  49. {
  50. JsonNode val(JsonNode::DATA_STRING);
  51. val.String() = encoder(idx);
  52. target.push_back(std::move(val));
  53. }
  54. }
  55. }
  56. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  57. {
  58. current->operator[](fieldName).String() = value;
  59. }