~zorba-coders/zorba/bug-1032166

« back to all changes in this revision

Viewing changes to src/api/serialization/serializer.cpp

  • Committer: Tarmac
  • Author(s): Paul J. Lucas
  • Date: 2012-07-26 08:01:41 UTC
  • mfrom: (10951.1.6 bug-878508)
  • Revision ID: tarmac-20120726080141-sx6062smdvbyw019
Now properly serializing JSON for JsonML. Approved: Chris Hillery, Paul J. Lucas

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include "api/unmarshaller.h"
32
32
 
33
33
#include "util/ascii_util.h"
 
34
#include "util/json_util.h"
34
35
#include "util/string_util.h"
35
36
#include "util/unicode_util.h"
36
 
#include "util/utf8_string.h"
37
37
#include "util/utf8_util.h"
38
38
#include "util/xml_util.h"
39
39
 
1208
1208
********************************************************************************/
1209
1209
void serializer::json_emitter::emit_json_string(zstring const &string)
1210
1210
{
1211
 
  tr << '"';
1212
 
  utf8_string<zstring const> const u( string );
1213
 
  FOR_EACH( utf8_string<zstring const>, i, u ) {
1214
 
    unicode::code_point const cp = *i;
1215
 
    if ( ascii::is_cntrl( cp ) ) {
1216
 
      switch ( cp ) {
1217
 
        case '\b': tr << "\\b"; break;
1218
 
        case '\f': tr << "\\f"; break;
1219
 
        case '\n': tr << "\\n"; break;
1220
 
        case '\r': tr << "\\r"; break;
1221
 
        case '\t': tr << "\\t"; break;
1222
 
        default: {
1223
 
          std::ostringstream oss;
1224
 
          oss << std::hex << std::setfill('0') << "\\u" << std::setw(4) << cp;
1225
 
          tr << oss.str();
1226
 
        }
1227
 
      }
1228
 
      continue;
1229
 
    }
1230
 
    if ( unicode::is_supplementary_plane( cp ) ) {
1231
 
      unsigned high, low;
1232
 
      unicode::convert_surrogate( cp, &high, &low );
1233
 
      std::ostringstream oss;
1234
 
      oss << std::hex << std::setfill('0')
1235
 
          << "\\u" << std::setw(4) << high
1236
 
          << "\\u" << std::setw(4) << low;
1237
 
      tr << oss.str();
1238
 
      continue;
1239
 
    }
1240
 
    switch ( cp ) {
1241
 
      case '\\':
1242
 
      case '"':
1243
 
        tr << '\\';
1244
 
        // no break;
1245
 
      default: {
1246
 
        utf8::encoded_char_type ec;
1247
 
        tr.write( ec, utf8::encode( cp, ec ) );
1248
 
      }
1249
 
    }
1250
 
  }
1251
 
  tr << '"';
 
1211
  tr << '"' << json::serialize( string ) << '"';
1252
1212
}
1253
1213
 
1254
1214