2
0

JsonSerializer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(const IInstanceResolver * instanceResolver_, JsonNode & root_):
  14. JsonSerializeFormat(instanceResolver_, root_, true)
  15. {
  16. }
  17. void JsonSerializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
  18. {
  19. if(!boost::logic::indeterminate(value))
  20. current->operator[](fieldName).Bool() = value;
  21. }
  22. void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  23. {
  24. if(!defaultValue || defaultValue.get() != value)
  25. {
  26. std::string identifier = encoder(value);
  27. serializeString(fieldName, identifier);
  28. }
  29. }
  30. void JsonSerializer::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
  31. {
  32. if(!defaultValue || defaultValue.get() != value)
  33. current->operator[](fieldName).Float() = value;
  34. }
  35. void JsonSerializer::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> & defaultValue)
  36. {
  37. if(!defaultValue || defaultValue.get() != value)
  38. current->operator[](fieldName).Integer() = value;
  39. }
  40. void JsonSerializer::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  41. {
  42. if(!defaultValue || defaultValue.get() != value)
  43. current->operator[](fieldName).String() = enumMap.at(value);
  44. }
  45. void JsonSerializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
  46. {
  47. if(value.empty())
  48. return;
  49. JsonVector & data = current->operator[](fieldName).Vector();
  50. data.reserve(value.size());
  51. for(const si32 rawId : value)
  52. {
  53. JsonNode jsonElement(JsonNode::JsonType::DATA_STRING);
  54. jsonElement.String() = encoder(rawId);
  55. data.push_back(std::move(jsonElement));
  56. }
  57. }
  58. void JsonSerializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  59. {
  60. assert(standard.size() == value.size());
  61. if(standard == value)
  62. return;
  63. writeLICPart(fieldName, "anyOf", encoder, value);
  64. }
  65. void JsonSerializer::serializeLIC(const std::string & fieldName, LIC & value)
  66. {
  67. if(value.any != value.standard)
  68. writeLICPart(fieldName, "anyOf", value.encoder, value.any);
  69. writeLICPart(fieldName, "allOf", value.encoder, value.all);
  70. writeLICPart(fieldName, "noneOf", value.encoder, value.none);
  71. }
  72. void JsonSerializer::serializeLIC(const std::string & fieldName, LICSet & value)
  73. {
  74. if(value.any != value.standard)
  75. writeLICPart(fieldName, "anyOf", value.encoder, value.any);
  76. writeLICPart(fieldName, "allOf", value.encoder, value.all);
  77. writeLICPart(fieldName, "noneOf", value.encoder, value.none);
  78. }
  79. void JsonSerializer::serializeString(const std::string & fieldName, std::string & value)
  80. {
  81. if(value != "")
  82. current->operator[](fieldName).String() = value;
  83. }
  84. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::vector<bool> & data)
  85. {
  86. std::vector<std::string> buf;
  87. buf.reserve(data.size());
  88. for(si32 idx = 0; idx < data.size(); idx++)
  89. if(data[idx])
  90. buf.push_back(encoder(idx));
  91. writeLICPartBuffer(fieldName, partName, buf);
  92. }
  93. void JsonSerializer::writeLICPart(const std::string & fieldName, const std::string & partName, const TEncoder & encoder, const std::set<si32> & data)
  94. {
  95. std::vector<std::string> buf;
  96. buf.reserve(data.size());
  97. for(const si32 item : data)
  98. buf.push_back(encoder(item));
  99. writeLICPartBuffer(fieldName, partName, buf);
  100. }
  101. void JsonSerializer::writeLICPartBuffer(const std::string & fieldName, const std::string & partName, std::vector<std::string> & buffer)
  102. {
  103. if(!buffer.empty())
  104. {
  105. std::sort(buffer.begin(), buffer.end());
  106. auto & target = current->operator[](fieldName)[partName].Vector();
  107. for(auto & s : buffer)
  108. {
  109. JsonNode val(JsonNode::JsonType::DATA_STRING);
  110. std::swap(val.String(), s);
  111. target.push_back(std::move(val));
  112. }
  113. }
  114. }