eredo / dartson

Dartson is a Dart library that can be used to convert Dart objects into a JSON string.
MIT License
79 stars 29 forks source link

subtype error #36

Closed mnordine closed 8 years ago

mnordine commented 8 years ago

I'm getting the following error when using the transformer:

Exception: type 'MyClass' is not a subtype of type 'StaticEntity' of 'clazz' where
  MyClass is from package:entities/src/entities_base.dart
  StaticEntity is from package:dartson/src/static_entity.dart

  Dartson.map   
  Dartson.decode    
  main  
eredo commented 8 years ago

Did you add the @Entity() annotation to your object classes (MyClass) which are serialized / deserialized?

mnordine commented 8 years ago

Yes, here's my entity:

import 'package:dartson/dartson.dart';

@Entity()
class MyClass
{
  int id;
}

And in the main package, main.dart:

import 'package:dartson/dartson.dart';
import 'dart:convert' show JSON;
import 'package:entities/entities.dart';

main()
{
  final dartson = new Dartson.JSON();

  final map = {'id': 0};

  MyClass decoded = dartson.decode(JSON.encode(map), new MyClass());
  print(decoded.id);
}

and pubspec.yaml:

name: 'web_dartson_test'
version: 0.0.1
description: An absolute bare-bones web app.

environment:
  sdk: '>=1.0.0 <2.0.0'

dependencies:
  browser: '>=0.10.0 <0.11.0'
  dart_to_js_script_rewriter: '^1.0.1'
  dartson: ^0.2.5
  entities:
    path: ../entities

transformers:
- dartson
- dart_to_js_script_rewriter
eredo commented 8 years ago

How does your pubspec.yaml look like?

mnordine commented 8 years ago

It's there in the previous comment

mnordine commented 8 years ago

Also, here's the pubspec.yaml from the entities library:

name: entities
description: A starting point for Dart libraries or applications.
version: 0.0.1

environment:
  sdk: '>=1.0.0 <2.0.0'

dependencies:
  dartson: any

dev_dependencies:
  test: '>=0.12.0 <0.13.0'
eredo commented 8 years ago

Dartson currently doesn't support entities in dependencies, because transformers do not have access to those source files. Not sure if it works if you place the transformer also in your entities project. Haven't had the chance to test that yet.

mnordine commented 8 years ago

What's interesting is that if I add dartson transformer to the entities library in my simple test case presented here, it works. However, it doesn't work for my much more complex project. I'll try to nail down a simple test case that fails.

mnordine commented 8 years ago

I figured it out. In my entity class file, I was using the following line for importing dartson:

import 'package:dartson/dartson.dart' show Entity;

I needed to remove

show Entity

since the transformer adds other classes, such as

implements StaticEntity

hence the error you see above.