redstone-dart / redstone_mapper

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

Decoding a List<int> #7

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

I am trying to decode a list of integers, e.g. "[1,2,3,4]", like this

decodeJson(json, new List<int>().runtimeType)

but I am getting this error

MapperException:  UnsupportedType: List<int>. This type wasn't mapped by redstone_mapper's transformer. See http://goo.gl/YYMou2 for more information.

What should I do?

cgarciae commented 9 years ago

If I remove the transform redstone_mapper it seems to work alright. This is very strange since yesterday it was working perfectly.

cgarciae commented 9 years ago

For the moment I am creating my own list class

class ListInt
{
    @Field() List<int> list;
}

but it seems rather unnecessary.

luizmineo commented 9 years ago

There is no need to pass the List type to the decoder function. If the json object is an array, the decode will produce a List automatically. Example:

decodeJson(json, int);

Let me know if that helps.

cgarciae commented 9 years ago

Hi Luiz, thanks for the answer.

I still have two questions:

  1. Nonetheless, is there an issue with Redstone Mapper's transform given that the line of code works without it?
  2. Can you decode lists of custom types the same way?

Really appreciate you support.

luizmineo commented 9 years ago
  1. I don't consider this behavior as an issue, since the transformer's job is to extract the information of all classes that uses the @Field annotation, which is not the case of List. Also, as explained, the function doesn't requires that the users specifies when the encoded object is actually an array or not. Maybe we can improve the documentation to make this clear.
  2. Yes.
cgarciae commented 9 years ago

@luizmineo I continued what I was doing since this post, and now I use

decodeJson(json, int)

as you suggest but now I get this error

MapperException:  UnsupportedType: int. This type wasn't mapped by redstone_mapper's transformer. See http://goo.gl/YYMou2 for more information.

STACKTRACE:
#0      _getOrCreateMapper (package:redstone_mapper/mapper_factory_static.dart:234:9)
#1      _TypeDecoder.convert (package:redstone_mapper/src/mapper_impl.dart:31:35)
#2      GenericTypeCodec.decode (package:redstone_mapper/mapper.dart:321:28)
#3      decodeJson (package:redstone_mapper/mapper.dart:68:26)
...

which again has to do with the mapper. It seems int wasn't registered in the mapper.

BTW: this is client code.

luizmineo commented 9 years ago

Ok, I'll take a better look later. Meanwhile, you can decode a List with the JSON codec from dart:convert

JSON.decode("[1, 2, 3, 4]");
luizmineo commented 9 years ago

Fixed in redstone_mapper v0.1.11