www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Reflection/Introspection?

reply "DDD" <dcb854d0bfb1 f98b7c56a69c.anonbox.net> writes:
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?
Oct 16 2013
next sibling parent reply "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
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 ); }
Oct 16 2013
parent "Craig Dillabaugh" <cdillaba cg.scs.carleton.ca> writes:
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
Oct 16 2013
prev sibling next sibling parent Manu <turkeyman gmail.com> writes:
On 17 October 2013 11:47, DDD <dcb854d0bfb1 f98b7c56a69c.anonbox.net> 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?
Yes, there is quite thorough reflection in D. Serialisers are plentiful.
Oct 16 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-10-17 03:47, 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?
If you need something more advanced you can try Orange: https://github.com/jacob-carlborg/orange Soon (hopefully) to be integrated to Phobos as std.serialization. -- /Jacob Carlborg
Oct 17 2013