Function jsonEncoder

A helper function for constructing a [BinaryEncoder] with inferred template arguments.

auto jsonEncoder(ORangeT) (
  ORangeT oRange
);

Example

import std.stdio;
import std.format;
import std.array : appender;

struct Test {
  void function(Encoder) testOp;
  string expected;
}
auto tests = [
    Test((e) => e.writeNull(), "null"),
    Test((e) => e.writeBoolean(true), "true"),
    Test((e) => e.writeBoolean(false), "false"),
    Test((e) => e.writeInt(-101), "-101"),
    Test((e) => e.writeLong(9223372036854775807L), "9223372036854775807"),
    Test((e) => e.writeFloat(0.002358), "0.002358"),
    Test((e) => e.writeFloat(float.nan), "NaN"),
    Test((e) => e.writeFloat(float.infinity), "Infinity"),
    Test((e) => e.writeFloat(-float.infinity), "-Infinity"),
    Test((e) => e.writeDouble(0.002358), "0.002358"),
    Test((e) => e.writeDouble(double.nan), "NaN"),
    Test((e) => e.writeDouble(double.infinity), "Infinity"),
    Test((e) => e.writeDouble(-double.infinity), "-Infinity"),
    Test((e) => e.writeString("ham"), "\"ham\""),
    Test((e) => e.writeString("a\nb\tc\0"), "\"a\\nb\\tc\\u0000\""),
    Test((e) => e.writeFixed([0x00, 0x01, 0x41, 0x42, 0x7E, 0x22]), "\"\\u0000\\u0001AB~\\\"\""),
    Test((e) => e.writeBytes([0x00, 0x01, 0x41, 0x42, 0x7E, 0x22]), "\"\\u0000\\u0001AB~\\\"\""),
    Test((e) => e.writeEnum(3, "CAT"), "\"CAT\""),
    Test((e) {
          e.writeArrayStart();
          e.setItemCount(2);
          e.startItem();
          e.writeInt(3);
          e.startItem();
          e.writeInt(4);
          e.writeArrayEnd();
        }, "[3, 4]"),
    Test((e) {
          e.writeMapStart();
          e.setItemCount(2);
          e.startItem();
          e.writeMapKey("a");
          e.writeInt(3);
          e.startItem();
          e.writeMapKey("b");
          e.writeInt(4);
          e.writeMapEnd();
        }, `{"a": 3, "b": 4}`),
    Test((e) {
          e.writeRecordStart();
          e.setItemCount(2);
          e.startItem();
          e.writeRecordKey("a");
          e.writeInt(3);
          e.startItem();
          e.writeRecordKey("b");
          e.writeInt(4);
          e.writeRecordEnd();
        }, `{"a": 3, "b": 4}`),
    Test((e) {
          e.writeUnionStart();
          e.writeUnionType(1, "CAT");
          e.writeInt(3);
          e.writeUnionEnd();
        }, `{"CAT": 3}`),
];

foreach (test; tests) {
  string data;
  auto encoder = jsonEncoder(appender(&data));
  test.testOp(encoder);
  string val;
  assert(
      (val = data.map!(to!char).to!string) == test.expected,
      format("Expected '%s' got '%s'.", test.expected, val));
}