JsonSerializer.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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::serializeEnum(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. writeLICPart(fieldName, "anyOf", encoder, value);
  50. }
  51. void JsonSerializer::serializeLIC(const std::string & fieldName, LIC & value)
  52. {
  53. if(value.any != value.standard)
  54. {
  55. writeLICPart(fieldName, "anyOf", value.encoder, value.any);
  56. }
  57. writeLICPart(fieldName, "allOf", value.encoder, value.all);
  58. writeLICPart(fieldName, "noneOf", value.encoder, value.none);
  59. }
  60. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  61. {
  62. if(value != "")
  63. current->operator[](fieldName).String() = value;
  64. }
  65. void JsonSerializer::writeLICPart(const std::string& fieldName, const std::string& partName, const TEncoder& encoder, const std::vector<bool> & data)
  66. {
  67. auto & target = current->operator[](fieldName)[partName].Vector();
  68. for(si32 idx = 0; idx < data.size(); idx ++)
  69. {
  70. if(data[idx])
  71. {
  72. JsonNode val(JsonNode::DATA_STRING);
  73. val.String() = encoder(idx);
  74. target.push_back(std::move(val));
  75. }
  76. }
  77. }