| Thread overview | |||||||
|---|---|---|---|---|---|---|---|
|
October 17, 2013 Reflection/Introspection? | ||||
|---|---|---|---|---|
| ||||
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?
| ||||
October 17, 2013 Re: Reflection/Introspection? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DDD | 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 ); } | |||
October 17, 2013 Re: Reflection/Introspection? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | 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 | |||
October 17, 2013 Re: Reflection/Introspection? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DDD Attachments:
| 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. | |||
October 17, 2013 Re: Reflection/Introspection? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DDD | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply