redstone-dart / redstone_mapper

A mapper plugin for Redstone.dart
MIT License
9 stars 10 forks source link

Won't decode on client side as expected. #4

Closed atebitftw closed 10 years ago

atebitftw commented 10 years ago

I'll try a simple example to explain the problem.

given:

 class Bar{
    Bar();

    @Field()
    String hi;
 }

on server side:

 @app.Route('/foo', methods: const[app.POST])
 @Encode()
 getListOfBar(){
    return [new Bar()..hi='1', new Bar()..hi='2', new Bar()..hi='3'];
 }

on client side:

 return HttpRequest
     .postFormData('/foo', {})
     .then((response) => decode(response.response, Bar))
     .then((result) => print(result));

I would expect output to be:

 [Instance of Bar, Instance of Bar, Instance of Bar]

But instead I just get the JSON object.

 [{hi: '1'},{hi: '2'},{hi: '3'}]

Am I overlooking something obvious? Thanks in advance :)

luizmineo commented 10 years ago

You have to use the decode function with the JSON codec (from the dart:convert library):

 return HttpRequest
     .postFormData('/foo', {})
     .then((response) => decode(JSON,decode(response.response), Bar))
     .then((result) => print(result));

You can see a working example here.

luizmineo commented 10 years ago

This API is indeed a bit confusing. I'm thinking about including the encodeJson() and decodeJson() functions in the future releases

atebitftw commented 10 years ago

I tried with JSON.decode() as you illustrated, but it appears to make no difference. I still get a [Map, Map] instead of [Object, Object]. It seems to encode just fine on the server side (in fact all the data is correctly present in each Map), so not sure what's going on. :)

luizmineo commented 10 years ago

Yeah, this is another thing that can be improved. If the class you are trying to decode to wasn't mapped by the transformer, the decode function will return the input. You just have to include your encodable classes in your entry-point (see here for an example).

atebitftw commented 10 years ago

Seems reasonable, once I understand the need to do that. Thanks! On Sep 14, 2014 3:46 PM, "Luiz Henrique Farcic Mineo" < notifications@github.com> wrote:

Yeah, this is another thing that can be improved. If the class you are trying to decode to wasn't mapped by the transformer, the decode function will return the input. You just have to include your encodable classes in your entry-point (see here https://github.com/luizmineo/io_2014_contacts_demo/blob/master/web/index.dart for an example).

— Reply to this email directly or view it on GitHub https://github.com/luizmineo/redstone_mapper/issues/4#issuecomment-55538937 .

atebitftw commented 10 years ago

It's working now. Although I noticed the client-side transformer (I think) doesn't like the, "view" parameter on fields: "@Field(view:'something')". Once I removed those, then it works as expected, otherwise I get compilation errors.

luizmineo commented 10 years ago

Thanks! This is probably a bug in the transformer. I'll take a look at this.

luizmineo commented 10 years ago

Fixed in v0.1.8