VarHolder.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // VarHolder.cpp
  3. //
  4. // $Id: //poco/svn/Foundation/src/VarHolder.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: VarHolder
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Dynamic/VarHolder.h"
  16. #include "Poco/Dynamic/Var.h"
  17. namespace Poco {
  18. namespace Dynamic {
  19. VarHolder::VarHolder()
  20. {
  21. }
  22. VarHolder::~VarHolder()
  23. {
  24. }
  25. namespace Impl {
  26. void escape(std::string& target, const std::string& source)
  27. {
  28. std::string::const_iterator it(source.begin());
  29. std::string::const_iterator end(source.end());
  30. for (; it != end; ++it)
  31. {
  32. switch (*it)
  33. {
  34. case '"':
  35. target += "\\\"";
  36. break;
  37. case '\\':
  38. target += "\\\\";
  39. break;
  40. case '\b':
  41. target += "\\b";
  42. break;
  43. case '\f':
  44. target += "\\f";
  45. break;
  46. case '\n':
  47. target += "\\n";
  48. break;
  49. case '\r':
  50. target += "\\r";
  51. break;
  52. case '\t':
  53. target += "\\t";
  54. break;
  55. default:
  56. target += *it;
  57. break;
  58. }
  59. }
  60. }
  61. bool isJSONString(const Var& any)
  62. {
  63. return any.type() == typeid(std::string) ||
  64. any.type() == typeid(char) ||
  65. any.type() == typeid(char*) ||
  66. any.type() == typeid(Poco::DateTime) ||
  67. any.type() == typeid(Poco::LocalDateTime) ||
  68. any.type() == typeid(Poco::Timestamp);
  69. }
  70. void appendJSONString(std::string& val, const Var& any)
  71. {
  72. val += '"';
  73. escape(val, any.convert<std::string>());
  74. val += '"';
  75. }
  76. void appendJSONKey(std::string& val, const Var& any)
  77. {
  78. return appendJSONString(val, any);
  79. }
  80. void appendJSONValue(std::string& val, const Var& any)
  81. {
  82. if (any.isEmpty())
  83. {
  84. val.append("null");
  85. }
  86. else
  87. {
  88. bool isStr = isJSONString(any);
  89. if (isStr)
  90. {
  91. appendJSONString(val, any.convert<std::string>());
  92. }
  93. else
  94. {
  95. val.append(any.convert<std::string>());
  96. }
  97. }
  98. }
  99. } // namespace Impl
  100. } } // namespace Poco::Dynamic