Reflection/Introspection?

Craig Dillabaugh cdillaba at cg.scs.carleton.ca
Wed Oct 16 19:28:01 PDT 2013


On Thursday, 17 October 2013 at 02:03:28 UTC, Craig Dillabaugh
wrote:
> On Thursday, 17 October 2013 at 01:47:41 UTC, DDD wrote:
>> Does D have any reflection/introspection?
>>
>> I'd like to do something like this
>>
>> class Contact
>> {
>> string first, last
>> int number
>> }
>>
>> contact = json_deserialized!Contact(json_string);
>>
>> Is something like that possible?
>
> Check out Vibe.D
>
> http://vibed.org/api/vibe.data.json/deserializeJson
>
> This is exactly what you are looking for I believe.
>
> For example I have the following code which works:
>
> import vibe.d;
>
> struct Foo {
>    string bar;
>    int	 num;
> }
>
>
> void deserializeExample() {
>        Json js = Json.emptyObject;
>        js.bar = "Hello world!";
>        js.num = 42;
>
>        Foo afoo = deserializeJson!Foo( js );
>
>        assert( afoo.bar == "Hello world!");
>        assert( afoo.num == 42 );
> }

Vibe source code for deserializeJson can be viewed at:

https://github.com/rejectedsoftware/vibe.d/blob/master/source/vibe/data/json.d



More information about the Digitalmars-d mailing list