Type of the struct.
Instance of the object to modify. Classes are edited in place, structs are not.
Function to call if a field doesn't exist. By default, such fields are ignored. The callback is not taken into account if the struct defines fromJSON.
1. If T is a struct, a copy of the given object with updated properties. 2. If T is a class, a reference to the given object. (ret is obj)
JSONException if there's a type mismatch or syntax error.
struct Example { string name; int version_; string[] contents; } auto json = JSONParser(q{ { "name": "rcjson", "version": 123, "contents": ["json-parser"] } }); const obj = json.getStruct!Example; assert(obj.name == "rcjson"); assert(obj.version_ == 123); assert(obj.contents == ["json-parser"]);
Using fallback
struct Table { @JSONName("table-name") string tableName; @JSONExclude string[string] attributes; } auto json = JSONParser(q{ { "table-name": "Player", "id": "PRIMARY KEY INT", "name": "VARCHAR(30)", "xp": "INT", "attributes": "VARCHAR(60)" } }); auto table = json.getStruct!Table((ref Table table, wstring key) { table.attributes[key.to!string] = json.getString.to!string; }); assert(table.tableName == "Player"); assert(table.attributes["id"] == "PRIMARY KEY INT"); assert(table.attributes["xp"] == "INT"); assert(table.attributes["attributes"] == "VARCHAR(60)");
Using fromJSON
static struct Snowflake { ulong id; alias id this; void fromJSON(JSONParser parser) { id = parser.get!string.to!ulong; } } auto json = JSONParser(`"1234567890"`); assert(json.get!Snowflake == 1234567890);
Load given struct or class from JSON.
If the object has a fromJSON method, it will be called to load the data. Otherwise, an attempt will be made to read a JSON object and translate its properties to fields of the struct or class. The object doesn't have to provide all fields defined in the struct or class.
JSON fields that share names with D reserved keywords can be suffixed with _ in code, per the D style specification. Alternatively, @JSONName("keyword") can be used to pick a different name to use for the JSON key.
For getStruct, the struct or class must have a default constructor available.
Inaccessible (eg. private or protected in external access) fields and fields marked with @JSONExclude will not be affected.