January 31, 2016
Hi!
 I need to read bad-formed json files in type-tolerant mode, and my idea is to make JSONValue-specific 'to' method which will convert it to target type if applicable, else throw exception. Like this:

import std.json;

unittest
{
    JSONValue s="123";
    JSONValue i=123;
    JSONValue f=123.0f;
    JSONValue a=[1,2,3];

    float result;

    result = s.to!float;
    assert(result == 123.);

    result = i.to!float;
    assert(result == 123.);

    result = f.to!float;
    assert(result == 123.);

    result = a.to!float; /// <-- exception
}

Naive approach doesn't works:

T to(T)(JSONValue value)
{
	final switch(value.type)
	{
		case JSON_TYPE.ARRAY:
			return value.array.to!T;
		case JSON_TYPE.FALSE:
			return false.to!T;
		case JSON_TYPE.TRUE:
			return true.to!T;
		case JSON_TYPE.FLOAT:
			return value.floating.to!T;
		case JSON_TYPE.INTEGER:
			return value.integer.to!T;
		case JSON_TYPE.NULL:
			return null.to!T;
		case JSON_TYPE.OBJECT:
			return value.object.to!T;
		case JSON_TYPE.STRING:
			return value.string.to!T;
		case JSON_TYPE.UINTEGER:
			return value.uinteger.to!T;
	}
}

So my questions: is this possible to avoid explicit writing all specializations for every combination of JSON_TYPE/T required? How to resolve this task in right way?