BSONWriter.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // BSONWriter.cpp
  3. //
  4. // $Id$
  5. //
  6. // Library: MongoDB
  7. // Package: MongoDB
  8. // Module: BSONWriter
  9. //
  10. // Implementation of the BSONWriter class.
  11. //
  12. // Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #include <sstream>
  38. #include "Poco/MongoDB/BSONWriter.h"
  39. namespace Poco {
  40. namespace MongoDB {
  41. BSONWriter::BSONWriter(Poco::BinaryWriter& writer) : _writer(writer)
  42. {
  43. }
  44. BSONWriter::~BSONWriter()
  45. {
  46. }
  47. void BSONWriter::write(const Document& doc)
  48. {
  49. if ( doc.empty() )
  50. {
  51. _writer << 5;
  52. }
  53. else
  54. {
  55. std::stringstream sstream;
  56. Poco::BinaryWriter tempWriter(sstream);
  57. for(Document::ConstIterator it = doc.begin(); it != doc.end(); ++it)
  58. {
  59. const Dynamic::Var& element = it->second;
  60. if ( element.isEmpty() )
  61. {
  62. tempWriter << (unsigned char) 0x0A;
  63. BSONWriter(tempWriter).writeCString(it->first);
  64. }
  65. else if ( element.isNumeric() )
  66. {
  67. if ( element.isInteger() )
  68. {
  69. tempWriter << (unsigned char) 0x10;
  70. BSONWriter(tempWriter).writeCString(it->first);
  71. tempWriter << (Int32) element;
  72. }
  73. else
  74. {
  75. tempWriter << (unsigned char) 0x01;
  76. BSONWriter(tempWriter).writeCString(it->first);
  77. tempWriter << (double) element;
  78. }
  79. }
  80. else if ( element.isString() )
  81. {
  82. tempWriter << (unsigned char) 0x02;
  83. BSONWriter(tempWriter).writeCString(it->first);
  84. const std::string& s = element.extract<std::string>();
  85. tempWriter << (Poco::Int32) (s.length() + 1);
  86. BSONWriter(tempWriter).writeCString(s);
  87. }
  88. else if ( element.isStruct() )
  89. {
  90. tempWriter << (unsigned char) 0x03;
  91. BSONWriter(tempWriter).writeCString(it->first);
  92. BSONWriter(tempWriter).write(element.extract<Document>());
  93. }
  94. else if ( element.isArray() ) // TODO
  95. {
  96. }
  97. else if ( element.type() == typeid(bool) )
  98. {
  99. tempWriter << (unsigned char) 0x08;
  100. BSONWriter(tempWriter).writeCString(it->first);
  101. tempWriter << (element.extract<bool>() ? 0x01 : 0x00);
  102. }
  103. else if ( element.type() == typeid(DateTime) )
  104. {
  105. }
  106. }
  107. tempWriter.flush();
  108. Poco::Int32 len = 5 + sstream.tellp(); /* 5 = sizeof(len) + 0-byte */
  109. _writer << len;
  110. _writer.writeRaw(sstream.str());
  111. }
  112. _writer << '\0';
  113. }
  114. void BSONWriter::writeCString(const std::string& value)
  115. {
  116. _writer.writeRaw(value);
  117. _writer << (unsigned char) 0x00;
  118. }
  119. }} // Namespace Poco::MongoDB