Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not serialize Map<String, T> #46

Closed
hazzo opened this issue Mar 8, 2020 · 12 comments
Closed

Can not serialize Map<String, T> #46

hazzo opened this issue Mar 8, 2020 · 12 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@hazzo
Copy link

hazzo commented Mar 8, 2020

Hi is possible to serialize a map of a type?
For example:

@jsonSerializable
class Person {
   final int age;
   Person({this.age});
}

Map<String, Person> foo = Map();
foo['bar'] = Person(age: 30);
final json = JsonMapper.serialize(foo);

I have been trying this in several ways and I'm not able to get it, but yes I was able to do it with the library json_serializable. I thought this one could do the same stuff with less boilerplate.

Thanks!

Edit:
I get this error It seems your class '_InternalLinkedHashMap<String, Person>' has not been annotated with @jsonSerializable

@k-paxian k-paxian self-assigned this Mar 8, 2020
@k-paxian k-paxian added bug Something isn't working good first issue Good for newcomers labels Mar 8, 2020
k-paxian pushed a commit that referenced this issue Mar 8, 2020
#46, Introduce default converter for Map<K, V>.
@k-paxian
Copy link
Owner

k-paxian commented Mar 8, 2020

Thank you for vanishing it out, please try new version 1.5.4.
I'm sure you'll not regret about switching from json_serializable

@k-paxian k-paxian closed this as completed Mar 8, 2020
@hazzo
Copy link
Author

hazzo commented Mar 8, 2020

I’ll test it as soon as possible. I’m sure you are right. It’s inevitable to tackle every use cases without a bug.

Thanks for the speed 🚀

@hazzo
Copy link
Author

hazzo commented Mar 8, 2020

Works like charm! Thnks again

@hazzo
Copy link
Author

hazzo commented Mar 14, 2020

Hi again @k-paxian. I'm sorry to bother you again. But even the fix was working perfectly once I started using mobx-adapter it started bugging again.

If the mobx adapter is not set (JsonMapper().useAdapter(mobXAdapter);) works perfect.

So debugged the lib for a couple of minutes and (not sure if this is the problem) but I discovered that in this line:

  TypeInfo getTypeInfo(Type type) {
    var result = TypeInfo(type);
    typeInfoDecorators.values.forEach((ITypeInfoDecorator decorator) {
      result = decorator.decorate(result);
    });
    return result;
  }

The mobx adapter appears here (as MobxTypeInfoDecorator) so got me thinking that maybe the issue is around that method. Even though the TypeInfo has the property isMap = true so I'm not completely sure that the problem is here.

If I can help with more info let me know. Thanks!

@k-paxian
Copy link
Owner

Could you please provide an code snippet which is not working for you?

It should have ObservableMap in it, I guess.

@hazzo
Copy link
Author

hazzo commented Mar 14, 2020

Sure but the problem is when serializing a map that is not in mobx. But if I add the adapter to use it with the mobx store the original map which is not in the store breaks with the same error as the one in the beginning of this issue.
Maybe I not explaining my self right.

@hazzo
Copy link
Author

hazzo commented Mar 14, 2020

This works

@jsonSerializable
class Person {
  final int age;
  Person({this.age});
}

void main() {
  initializeReflectable();
//  JsonMapper().useAdapter(mobXAdapter);

  Map<String, Person> foo = Map();
  foo['bar'] = Person(age: 30);
  final json = JsonMapper.serialize(foo);
  print(json);
}

This does not

@jsonSerializable
class Person {
  final int age;
  Person({this.age});
}

void main() {
  initializeReflectable();
  JsonMapper().useAdapter(mobXAdapter);

  Map<String, Person> foo = Map();
  foo['bar'] = Person(age: 30);
  final json = JsonMapper.serialize(foo);
  print(json);
}

@hazzo
Copy link
Author

hazzo commented Mar 14, 2020

I discover why it happens, maybe you too already as you wrote de lib 😝
The issue is in getTypeInfo returning a genericType of ObservableMap even though is not an observable map. Right?
So this part of code:

if (result == null &&
        converters[typeInfo.genericType] != null &&
        getValueDecorator(jsonProperty, typeInfo.type) == null) {
      result = converters[typeInfo.genericType];
    }

Does not find the new Map adapter you wrote.

@hazzo
Copy link
Author

hazzo commented Mar 14, 2020

This solved for me

TypeInfo getTypeInfo(Type type) {
    var result = TypeInfo(type);
    typeInfoDecorators.values.forEach((ITypeInfoDecorator decorator) {
      if (result.genericType == null) {
        result = decorator.decorate(result);
      }
    });
    return result;
  }

Don't know if checking if having or not a genericType breaks other use cases. But this works for a generic map and also the mobx adpater serializes the store correctly.

@k-paxian
Copy link
Owner

Thanks for investigation! With the updated versions it should work now.

@hazzo
Copy link
Author

hazzo commented Mar 16, 2020

Cool, thanks!

@hazzo
Copy link
Author

hazzo commented Mar 18, 2020

By the way, tried it and works great. Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants