Git Product home page Git Product logo

Comments (3)

DanTup avatar DanTup commented on May 5, 2024

Even trying to build the rules manually; I can't make this work :(

Here's my rule:

class AdvertiserRule extends CustomRule {
  bool appliesTo(instance, Writer w) => instance.runtimeType == Advertiser;
  create(state) => new Advertiser();

  getState(Advertiser a) =>
  {
    key: a.key,
    name: a.name,
    contactName: a.contactName,
    contactTelephone: a.contactTelephone,
    notes: a.notes
  };

  setState(Advertiser a, Map state) {
    a.key = state['key'];
    a.name = state['name'];
    a.contactName = state['contactName'];
    a.contactTelephone = state['contactTelephone'];
    a.notes = state['notes'];
  }
}

But I can't figure out how to add it. When I do:

var s = new Serialization()..addRule(AdvertiserRule);

I get:

Unhandled exception:
type '_Type' is not a subtype of type 'SerializationRule' of 'rule'.
#0      Serialization.addRule (package:serialization/serialization.dart:322:47)
#1      main (file:///C:/Work/Source/BLAH/test/test_serialisation.dart:8:14)
#2      _startIsolate (dart:isolate-patch/isolate_patch.dart:239)
#3      _startMainIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:192)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

I've tried changing CustomRule to SerializationRule but the error doesn't change. I also tried doing new AdvertiserRule() but then I get noSuchMethod key on AdvertiserRule when trying to write, which suggests getState is being passed is being passed an AdvertiserRule and not the Advertiser?

A full working example that just serialises a basic type (ideally with property that is another type) to/from json would be a huge help.

from serialization.dart.

alan-knight avatar alan-knight commented on May 5, 2024

The filename is the same as what you would put to import the original file,
but with "_serialization_rules" after the main part of the name. So in
your case it would be
import 'package:<your_package>/src/blah/types_serialization_rules.dart';

Added some text to the README to make it clearer.

Pub build normally does not write files to disk that aren't part of the
final result, so a straight pub build will generate the file but compile ot
JS, and the final output is just the JS. To see the intermediate files, run
it in debug mode. So, for example, in the serialization package.
pub build --mode=debug test/transformer
will create the files, and also print what they are on output, e.g.

Generated serialization rules in
serialization|test/transformer/test_models_serialization_rules.dart

The serialization takes instances, not classes. So "..addRule(new
AdvertiserRule())" is the right thing. In the transformer case, it
generates a collection called rules that you can use.

The noSuchMethod key is because you didn't put quotes around the keys in
your literal map. In Dart, if I write {key : a.key} it means that the key
is the value of the variable/getter named 'key', not the literal string
'key'. In JS, you can't use variables , so the quotes can be omitted.

For a working example, see test/transformer/transformer_maps_test.dart.
That's really just a shell for transformer_test_core.dart. It imports
test_models_for_maps.dart, there's a line in the pubspec which generates
rules for that, and then transformer_test_core imports those rules and
reads/writes those objects. There is also a generate_standalone.dart in
that directory which exercises the transformer code directly and generates
rules. The checked in version generates the list form rather than the map
form, but you'd just need to change the listFormat: option to false and the
output file name.

On Fri, Oct 31, 2014 at 9:27 AM, Danny Tuppeny [email protected]
wrote:

Even trying to build the rules manually; I can't make this work :(

Here's my rule:

class AdvertiserRule extends CustomRule {
bool appliesTo(instance, Writer w) => instance.runtimeType == Advertiser;
create(state) => new Advertiser();

getState(Advertiser a) =>
{
key: a.key,
name: a.name,
contactName: a.contactName,
contactTelephone: a.contactTelephone,
notes: a.notes
};

setState(Advertiser a, Map state) {
a.key = state['key'];
a.name = state['name'];
a.contactName = state['contactName'];
a.contactTelephone = state['contactTelephone'];
a.notes = state['notes'];
}
}

But I can't figure out how to add it. When I do:

var s = new Serialization()..addRule(AdvertiserRule);

I get:

Unhandled exception:
type '_Type' is not a subtype of type 'SerializationRule' of 'rule'.
#0 Serialization.addRule (package:serialization/serialization.dart:322:47)
#1 main (file:///C:/Work/Source/BLAH/test/test_serialisation.dart:8:14)
#2 _startIsolate (dart:isolate-patch/isolate_patch.dart:239)
#3 _startMainIsolate. (dart:isolate-patch/isolate_patch.dart:192)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:130)

I've tried changing CustomRule to SerializationRule but the error doesn't
change. I also tried doing new AdvertiserRule() but then I get noSuchMethod
key on AdvertiserRule when trying to write, which suggests getState is
being passed is being passed an AdvertiserRule and not the Advertiser?

A full working example that just serialises a basic type (ideally with
property that is another type) to/from json would be a huge help.


Reply to this email directly or view it on GitHub
#1 (comment)
.

from serialization.dart.

DanTup avatar DanTup commented on May 5, 2024

Thanks for the info!

Looks like the generated json serialisation might work for us; will see how I get on :-)

(btw, seems a bit weird to specific JSON in the transformer args; then again when doing the serialisation?)

from serialization.dart.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.