2
0

JsonSerializer.cpp 2.1 KB

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