Skip to content
YAJL binding for D
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

YAJL binding for D

yajl-d is a YAJL binding for D.

yajl-d is based on YAJL and work on 2.1.0 or later.


Add yajl to dependencies of package.json.

  "dependencies": {
    "yajl": ">=0.2.0"

Run example

Need to link yajl library

dmd -Isrc src/**/*.d -L-lyajl -run example/encode_bench.d



  • yajl.encode(value) / yajl.encode(value, opt)
import yajl;

struct Hoge
    ulong id;
    string word;
    bool yes; 

// {"id":100,"word":"hey!","yes":true}
string json = encode(Hoge(100, "hey!", true));


  • yajl.decode(value) / yajl.decode(value, opt)
import yajl;

Hoge hoge = decode!Hoge(`{"id":100,"word":"hey!","yes":true}`);
  • yajl.decoder.Decoder

Use decode and decodedValue methods.

import yajl.decoder;

Decoder decoder;
if (decoder.decode(`{"id":100,"word":"hey!","yes":true}`) {
    Hoge hoge = decoder.decodedValue!Hoge;
    // ...

Decoder#decode is a straming decoder, so you can pass the insufficient json to this method. If Decoder#decode can't parse completely, Decoder#decode returns false.

Encoder.Option and Decoder.Option

encode and decode can take each Option argument. If you want to know more details, see unittest of yajl.encoder / yajl.decoder.

Set callback to detect missing field

You can set own callback to Decoder.Option.missingHandler. Here is an example:

import yajl;
import std.stdio;

struct Test
  string name;
  int age;

void main()
  auto test = Test("Bob", 20);
  auto encoded = `{ "name": "Bob", "age": 20}`;
  auto missing = `{ "name": "Bob"}`;

  Decoder.Option opt;
  opt.missingHandler = (string field) { writeln(field); };
  writeln(decode!Test(encoded, opt));
  writeln(decode!Test(missing, opt)); // Callback called with missing field

Using a D keyword in JSON field names

Since a field name cannot be a D keyword, for example body or out, the variable and JSON field must have separate names. For this, use the @JSONName("name") attribute:

import yajl;

struct Hoge
    ulong id;
    @JSONName("body") string _body;
    bool yes; 

// {"id":100,"body":"hey!","yes":true}
string json = encode(Hoge(100, "hey!", true));


D: dmd 2.066.0
yajl: 2.1.0
OS: Mac OS X ver 10.9
CPU: 2.6 GHz Intel Core i7

encode(QPS)multi encode(QPS)decode(QPS)multi decode(QPS)
std.json262272Not supported287072Not supported

Benchmark code can be found in the example directory.


  • Limited direct conversion decoding
  • Test on Windows



AuthorMasahiro Nakagawa
CopyrightCopyright (c) 2013- Masahiro Nakagawa
LicenseBoost Software License, Version 1.0
You can’t perform that action at this time.