On Fri, 20 Sep 2013 20:39:00 +0200, Sönke Ludwig wrote:

Am 20.09.2013 19:57, schrieb Craig Dillabaugh:

On Fri, 20 Sep 2013 06:16:03 GMT, Sönke Ludwig wrote:

Thanks. Everything works up to the last step where I get the following:

source/app.d(137): Error: template instance shapes!(map(deserializeBson!(Shape))) shapes is not a template declaration, it is a variable
source/app.d(137): Error: template instance shapes!(map(deserializeBson!(Shape))) shapes is not a template declaration, it is a variable

Where line 137 is the line Shape[] ret = shapes.map!(deserializeBson!Shape).array, which I figure I should be able to fix, but I have no idea where the map!(...).array bit comes from.

Cheers,
Craig

The error message looks strange, but map() is in std.algorithm and
array() in std.array, so if any of those are not imported, maybe
that somehow causes them.

Otherwise this is the equivalent using a loop:

Shape[] ret;
foreach (bs; shapes) ret ~= deserializeBson!Shape(bs);

OK. The loop version worked so I am a happy camper.

However, since the map version looked so elegant I wanted to try and get it to work, but it did not, even with the proper imports.

I tried the following and while I got a different error, I still couldn't get it to compile.

auto bson_shapes = map!(deserializeBson!Shape)(shapes);

In this case I received the error (there were more, but usually the first error reported is the cause of the rest when templates are concerned):

source/app.d(138): Error: template vibe.data.bson.deserializeBson matches more than one template declaration, ../../.dub/packages/vibe-d-0.7.17/source/vibe/data/bson.d(946):deserializeBson(T)(ref T dst, Bson src) and ../../.dub/packages/vibe-d-0.7.17/source/vibe/data/bson.d(951):deserializeBson(T)(Bson src)

It seems it cannot decide which prototype for deserializeBson to choose from, I assume I want the second.

Now, back to the foreach loop version. My structs to read a shape look like follows:

struct Circle {
  int x;
  int y;
  int r;
}

//OK, so every shape is a circle for now .... baby steps ...
struct Shape {
  int 		class_;
  string 	type_;
  Circle 	coords;
  int		id;
}

My Json records for a shape look like follows:

{"class":1,"type":"circle","coords":{"x":3921,"y":2607,"r":4},"id":2}

Originally the Circle struct coords were of type double, but this causes a runtime exception of the form:

object.Exception@../../.dub/packages/vibe-d-0.7.17/source/vibe/data/bson.d(597): BSON value is type 'Int', expected to be one of [Double]

Of course for "x":3921 the value of x is interpreted as an integer and the Bson.Type is set to Int. In this case this isn't a big problem, since having integer coordinates is fine for my needs. But what if, for whatever reason, I could not assume x,y and r were ints, but they could be any value (including an int). Is there any way to tell deserializeBson to interpret all numeric values as doubles (if there is no easy answer no worries, I am just curious)?

Cheers,
Craig