Hello Hartmut,
as discussed @irc channel I am posting the example of my problem (I use
boost 1.47):
#include
#include
#include
#include
#include <iterator>
#include <cassert>
#include <utility>
#include <string>
namespace karma = boost::spirit::karma;
typedef std::pair utf16_t;
template<class OutIter>
struct json_char_encoder2
: boost::spirit::karma::grammar
{
json_char_encoder2()
: json_char_encoder2::base_type(encoded_)
{
json_sym_.add
('"', "\\\"")
('/', "\\/")
('\\', "\\\\")
('\b', "\\b")
('\f', "\\f")
('\n', "\\n")
('\r', "\\r")
('\t', "\\t")
;
using boost::spirit::iso8859_1::print;
encoded_=*(json_sym_ | print | uchar_);
uchar_ = karma::lit("\\u") << karma::hex << karma::hex; //also tried
to use unicode_gen_;
}
karma::rule encoded_;
karma::symbols json_sym_;
karma::rule uchar_;
karma::uint_generator unicode_gen_;
};
int main()
{
assert(2==sizeof(short));
typedef std::back_insert_iteratorstd::string iter_t;
json_char_encoder2 encoder2;
std::string generated;
iter_t sink(generated);
std::string sinput="abc";
sinput.push_back(char(0xAA));
sinput.push_back(char(0xBB));
bool result = karma::generate(sink, encoder2, sinput);
return 0;
}
The problem here is that I would like the generator in case of uchar_ to
retrieve 2 subsequent string bytes and write them out as \uXXXX as
specified at JSON.org. What I get is:
a. IF: replacing utf16_t with unsigned short => and removing 1 of the
karma::hex terminals => 2 calls where each byte is converted to unsigned
short
b. IF: having the source as it is => fails the generation, where the
generated string is empty.
One strange behavior as well: result is true for case b. Why?
Regards,
Ovanes