JsonSerializer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  34. {
  35. assert(standard.size() == value.size());
  36. if(standard == value)
  37. return;
  38. auto & target = current->operator[](fieldName)["anyOf"].Vector();
  39. for(si32 idx = 0; idx < value.size(); idx ++)
  40. {
  41. if(value[idx])
  42. {
  43. JsonNode val(JsonNode::DATA_STRING);
  44. val.String() = encoder(idx);
  45. target.push_back(std::move(val));
  46. }
  47. }
  48. }
  49. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  50. {
  51. current->operator[](fieldName).String() = value;
  52. }