Function BinaryEncoder.this

Create a writer that sends its output to the underlying stream oRange.

this (
  ORangeT oRange
);

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeNull();
  writeNull();
}
assert(data == []);

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeInt(-2);
  assert(data == [0x03]);
  writeInt(2147483647);
  assert(data == [0x03, 0xFE, 0xFF, 0xFF, 0xFF, 0x0F]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeLong(9223372036854775807);
  assert(data == [0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01]);
  writeLong(-9223372036854775808);
  assert(data == [
          0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01,
          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  // double accomodates 6-9 decimal digits
  writeFloat(23.2131);
  // Bytes according to IEEE-754 from least to most significant.
  assert(data == [0x6e, 0xb4, 0xb9, 0x41]);
  writeFloat(-76.8129);
  assert(data == [0x6e, 0xb4, 0xb9, 0x41, 0x34, 0xa0, 0x99, 0xc2]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  // double accomodates 15-16 decimal digits
  writeDouble(8329242423.24324);
  // Bytes according to IEEE-754 from least to most significant.
  assert(data == [0x50, 0xE4, 0x73, 0x73, 0x62, 0x07, 0xFF, 0x41]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeString("Grüßen");
  // Bytes according to IEEE-754 from least to most significant.
  //             len:8 zig-zag and var encoded
  assert(data == [0x10, 0x47, 0x72, 0xc3, 0xbc, 0xc3, 0x9f, 0x65, 0x6e]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  ubyte[] bytes = [0x01, 0x02, 0x03, 0x04, 0x05];
  writeFixed(bytes, 3, 2);
  assert(data == [0x04, 0x05]);
  writeFixed(bytes);
  assert(data == [0x04, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  ubyte[] bytes = [0x01, 0x02, 0x03, 0x04, 0x05];
  writeBytes(bytes, 3, 2);
  // len=0x02 => zig-zag + var encoding => 0x04
  assert(data == [0x04, 0x04, 0x05]);
  writeBytes(bytes);
  // len=0x05 => zig-zag + var encoding => 0x0A
  assert(data == [0x04, 0x04, 0x05, 0x0A, 0x01, 0x02, 0x03, 0x04, 0x05]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeEnum(0, "ham");
  assert(data == [0x00]);
  writeEnum(3, "bird");
  assert(data == [0x00, 0x06]);
}

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  writeArrayStart();
  setItemCount(2);
  startItem();
  writeLong(5L);
  writeBoolean(true);
  startItem();
  writeLong(-8L);
  writeBoolean(false);
  writeArrayEnd();
}
//              len-1 item1       item2       len-2
assert(data == [0x04, 0x0A, 0x01, 0x0F, 0x00, 0x00]);

Example

import std.array : appender;
ubyte[] data;
auto encoder = binaryEncoder(appender(&data));
with (encoder) {
  // A map of records, each of which has a Long and a Boolean.
  writeMapStart();
  setItemCount(2);
  // Item 1
  startItem();
  writeString("ham");
  writeLong(3);
  writeBoolean(true);
  // Item 2
  startItem();
  writeString("cat");
  writeLong(-3);
  writeBoolean(false);
  // End
  writeMapEnd();
}
assert(data == [
     // blk1
        0x04,
     // sLen     h     a     m  long  bool
        0x06, 0x68, 0x61, 0x6d, 0x06, 0x01,
     // sLen     c     a     t  long  bool
        0x06, 0x63, 0x61, 0x74, 0x05, 0x00,
     // blk2
        0x00]);