decodeEnum

Helper to decode a JSON style enum string (ENTRY_NAME) as a DStyle enum (entryName).

Use like so: alias decode = decodeEnum!EnumType; when forming your encode overload.

template decodeEnum(T)
U
decodeEnum
(
U : T
)
(
const string text
)
if (
is(T == enum)
)

Members

Functions

decodeEnum
U decodeEnum(string text)
Undocumented in source. Be warned that the author may not have intended to support it.

Throws

JSONException if the input text does not represent an enum member.

Examples

ditto

import text.json.Decode : decodeJson;

enum Enum
{
    testValue,
    isHttp,
}

alias decode = decodeEnum!Enum;

decodeJson!(Enum, decode)(JSONValue("TEST_VALUE")).should.be(Enum.testValue);
decodeJson!(Enum, decode)(JSONValue("IS_HTTP")).should.be(Enum.isHttp);
decodeJson!(Enum, decode)(JSONValue("")).should.throwA!JSONException;
decodeJson!(Enum, decode)(JSONValue("ISNT_HTTP")).should.throwA!JSONException(
    "expected member of Enum, not ISNT_HTTP (or in D, 'isntHttp')");

Meta