TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ write_msgpack()

template<typename BasicJsonType , typename CharType >
void nlohmann::detail::binary_writer< BasicJsonType, CharType >::write_msgpack ( const BasicJsonType &  j)
inline
Parameters
[in]jJSON value to serialize

Definition at line 14215 of file json.hpp.

14216 {
14217 switch (j.type())
14218 {
14219 case value_t::null: // nil
14220 {
14221 oa->write_character(to_char_type(0xC0));
14222 break;
14223 }
14224
14225 case value_t::boolean: // true and false
14226 {
14227 oa->write_character(j.m_value.boolean
14228 ? to_char_type(0xC3)
14229 : to_char_type(0xC2));
14230 break;
14231 }
14232
14234 {
14235 if (j.m_value.number_integer >= 0)
14236 {
14237 // MessagePack does not differentiate between positive
14238 // signed integers and unsigned integers. Therefore, we used
14239 // the code from the value_t::number_unsigned case here.
14240 if (j.m_value.number_unsigned < 128)
14241 {
14242 // positive fixnum
14243 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
14244 }
14245
14246 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
14247 {
14248 // uint 8
14249 oa->write_character(to_char_type(0xCC));
14250 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
14251 }
14252
14253 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
14254 {
14255 // uint 16
14256 oa->write_character(to_char_type(0xCD));
14257 write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
14258 }
14259
14260 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
14261 {
14262 // uint 32
14263 oa->write_character(to_char_type(0xCE));
14264 write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
14265 }
14266
14267 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
14268 {
14269 // uint 64
14270 oa->write_character(to_char_type(0xCF));
14271 write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
14272 }
14273 }
14274
14275 else
14276 {
14277 if (j.m_value.number_integer >= -32)
14278 {
14279 // negative fixnum
14280 write_number(static_cast<std::int8_t>(j.m_value.number_integer));
14281 }
14282
14283 else if (j.m_value.number_integer >= (std::numeric_limits<std::int8_t>::min)() &&
14284 j.m_value.number_integer <= (std::numeric_limits<std::int8_t>::max)())
14285 {
14286 // int 8
14287 oa->write_character(to_char_type(0xD0));
14288 write_number(static_cast<std::int8_t>(j.m_value.number_integer));
14289 }
14290
14291 else if (j.m_value.number_integer >= (std::numeric_limits<std::int16_t>::min)() &&
14292 j.m_value.number_integer <= (std::numeric_limits<std::int16_t>::max)())
14293 {
14294 // int 16
14295 oa->write_character(to_char_type(0xD1));
14296 write_number(static_cast<std::int16_t>(j.m_value.number_integer));
14297 }
14298
14299 else if (j.m_value.number_integer >= (std::numeric_limits<std::int32_t>::min)() &&
14300 j.m_value.number_integer <= (std::numeric_limits<std::int32_t>::max)())
14301 {
14302 // int 32
14303 oa->write_character(to_char_type(0xD2));
14304 write_number(static_cast<std::int32_t>(j.m_value.number_integer));
14305 }
14306
14307 else if (j.m_value.number_integer >= (std::numeric_limits<std::int64_t>::min)() &&
14308 j.m_value.number_integer <= (std::numeric_limits<std::int64_t>::max)())
14309 {
14310 // int 64
14311 oa->write_character(to_char_type(0xD3));
14312 write_number(static_cast<std::int64_t>(j.m_value.number_integer));
14313 }
14314 }
14315
14316 break;
14317 }
14318
14320 {
14321 if (j.m_value.number_unsigned < 128)
14322 {
14323 // positive fixnum
14324 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
14325 }
14326
14327 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint8_t>::max)())
14328 {
14329 // uint 8
14330 oa->write_character(to_char_type(0xCC));
14331 write_number(static_cast<std::uint8_t>(j.m_value.number_integer));
14332 }
14333
14334 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint16_t>::max)())
14335 {
14336 // uint 16
14337 oa->write_character(to_char_type(0xCD));
14338 write_number(static_cast<std::uint16_t>(j.m_value.number_integer));
14339 }
14340
14341 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint32_t>::max)())
14342 {
14343 // uint 32
14344 oa->write_character(to_char_type(0xCE));
14345 write_number(static_cast<std::uint32_t>(j.m_value.number_integer));
14346 }
14347
14348 else if (j.m_value.number_unsigned <= (std::numeric_limits<std::uint64_t>::max)())
14349 {
14350 // uint 64
14351 oa->write_character(to_char_type(0xCF));
14352 write_number(static_cast<std::uint64_t>(j.m_value.number_integer));
14353 }
14354
14355 break;
14356 }
14357
14359 {
14360 write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack);
14361 break;
14362 }
14363
14364 case value_t::string:
14365 {
14366 // step 1: write control byte and the string length
14367 const auto N = j.m_value.string->size();
14368
14369 if (N <= 31)
14370 {
14371 // fixstr
14372 write_number(static_cast<std::uint8_t>(0xA0 | N));
14373 }
14374
14375 else if (N <= (std::numeric_limits<std::uint8_t>::max)())
14376 {
14377 // str 8
14378 oa->write_character(to_char_type(0xD9));
14379 write_number(static_cast<std::uint8_t>(N));
14380 }
14381
14382 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14383 {
14384 // str 16
14385 oa->write_character(to_char_type(0xDA));
14386 write_number(static_cast<std::uint16_t>(N));
14387 }
14388
14389 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14390 {
14391 // str 32
14392 oa->write_character(to_char_type(0xDB));
14393 write_number(static_cast<std::uint32_t>(N));
14394 }
14395
14396 // step 2: write the string
14397 oa->write_characters(
14398 reinterpret_cast<const CharType *>(j.m_value.string->c_str()),
14399 j.m_value.string->size());
14400 break;
14401 }
14402
14403 case value_t::array:
14404 {
14405 // step 1: write control byte and the array size
14406 const auto N = j.m_value.array->size();
14407
14408 if (N <= 15)
14409 {
14410 // fixarray
14411 write_number(static_cast<std::uint8_t>(0x90 | N));
14412 }
14413
14414 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14415 {
14416 // array 16
14417 oa->write_character(to_char_type(0xDC));
14418 write_number(static_cast<std::uint16_t>(N));
14419 }
14420
14421 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14422 {
14423 // array 32
14424 oa->write_character(to_char_type(0xDD));
14425 write_number(static_cast<std::uint32_t>(N));
14426 }
14427
14428 // step 2: write each element
14429 for (const auto &el : *j.m_value.array)
14430 {
14431 write_msgpack(el);
14432 }
14433
14434 break;
14435 }
14436
14437 case value_t::binary:
14438 {
14439 // step 0: determine if the binary type has a set subtype to
14440 // determine whether or not to use the ext or fixext types
14441 const bool use_ext = j.m_value.binary->has_subtype();
14442 // step 1: write control byte and the byte string length
14443 const auto N = j.m_value.binary->size();
14444
14445 if (N <= (std::numeric_limits<std::uint8_t>::max)())
14446 {
14447 std::uint8_t output_type{};
14448 bool fixed = true;
14449
14450 if (use_ext)
14451 {
14452 switch (N)
14453 {
14454 case 1:
14455 output_type = 0xD4; // fixext 1
14456 break;
14457
14458 case 2:
14459 output_type = 0xD5; // fixext 2
14460 break;
14461
14462 case 4:
14463 output_type = 0xD6; // fixext 4
14464 break;
14465
14466 case 8:
14467 output_type = 0xD7; // fixext 8
14468 break;
14469
14470 case 16:
14471 output_type = 0xD8; // fixext 16
14472 break;
14473
14474 default:
14475 output_type = 0xC7; // ext 8
14476 fixed = false;
14477 break;
14478 }
14479 }
14480
14481 else
14482 {
14483 output_type = 0xC4; // bin 8
14484 fixed = false;
14485 }
14486
14487 oa->write_character(to_char_type(output_type));
14488
14489 if (!fixed)
14490 {
14491 write_number(static_cast<std::uint8_t>(N));
14492 }
14493 }
14494
14495 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14496 {
14497 std::uint8_t output_type = use_ext
14498 ? 0xC8 // ext 16
14499 : 0xC5; // bin 16
14500 oa->write_character(to_char_type(output_type));
14501 write_number(static_cast<std::uint16_t>(N));
14502 }
14503
14504 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14505 {
14506 std::uint8_t output_type = use_ext
14507 ? 0xC9 // ext 32
14508 : 0xC6; // bin 32
14509 oa->write_character(to_char_type(output_type));
14510 write_number(static_cast<std::uint32_t>(N));
14511 }
14512
14513 // step 1.5: if this is an ext type, write the subtype
14514 if (use_ext)
14515 {
14516 write_number(static_cast<std::int8_t>(j.m_value.binary->subtype()));
14517 }
14518
14519 // step 2: write the byte string
14520 oa->write_characters(
14521 reinterpret_cast<const CharType *>(j.m_value.binary->data()),
14522 N);
14523 break;
14524 }
14525
14526 case value_t::object:
14527 {
14528 // step 1: write control byte and the object size
14529 const auto N = j.m_value.object->size();
14530
14531 if (N <= 15)
14532 {
14533 // fixmap
14534 write_number(static_cast<std::uint8_t>(0x80 | (N & 0xF)));
14535 }
14536
14537 else if (N <= (std::numeric_limits<std::uint16_t>::max)())
14538 {
14539 // map 16
14540 oa->write_character(to_char_type(0xDE));
14541 write_number(static_cast<std::uint16_t>(N));
14542 }
14543
14544 else if (N <= (std::numeric_limits<std::uint32_t>::max)())
14545 {
14546 // map 32
14547 oa->write_character(to_char_type(0xDF));
14548 write_number(static_cast<std::uint32_t>(N));
14549 }
14550
14551 // step 2: write each element
14552 for (const auto &el : *j.m_value.object)
14553 {
14554 write_msgpack(el.first);
14555 write_msgpack(el.second);
14556 }
14557
14558 break;
14559 }
14560
14561 case value_t::discarded:
14562 default:
14563 break;
14564 }
14565 }
output_adapter_t< CharType > oa
the output
Definition: json.hpp:15519
void write_msgpack(const BasicJsonType &j)
Definition: json.hpp:14215
@ number_integer
number value (signed integer)
@ discarded
discarded by the parser callback function
@ binary
binary array (ordered collection of bytes)
@ object
object (unordered set of name/value pairs)
@ number_float
number value (floating-point)
@ number_unsigned
number value (unsigned integer)
@ array
array (ordered collection of values)