0

I have an Aqueduct project using the ORM, with a data model as follows:

class _Thing {
  @primaryKey
  int id;

  String first;

  String second;
}

class Thing extends ManagedObject<_Thing> implements _Thing {

  @Serialize()
  OtherThing get firstAndSecond() {
    // return some value computed from first and second
  }

  @Serialize()
  set firstAndSecond(OtherThing firstAndSecond) {
    // set first and second based on some computation
  }
}

According to the docs for transient properties, annotating with @Serialize() should enable this model to be serialized/deserialized. It also says that properties in ManagedObjects are not persisted, but when I run the server, I get the error:

Data Model Error: Property 'firstAndSecond' on 'Thing' has an unsupported type.

If I remove the @Serialize(), it doesn't try to persist it, but I can't serialize/deserialize this object.

Any suggestions as to why this is happening or how I can control this behaviour?

Vedavyas Bhat
  • 2,068
  • 1
  • 21
  • 31

1 Answers1

1

This should be in the docs -

A Serializable property must be a primitive type (e.g. String, int, double, bool or a Map or List containing these types). Serializable values are passed directly to the codec that is reading from a request body or writing to a response body (by default, this codec is JSON). In the case of a custom type like OtherThing, the codec doesn't know how to encode or decode that type.

For complex types, you might use a map:

@Serialize()
Map<String, dynanic> get firstAndSecond() {
   return {"first": first, "second": second};
}

You might also use CSV-like data:

@Serialize()
String get firstAndSecond() {
   return "$first,$second";
}
Joe Conway
  • 1,566
  • 9
  • 8
  • I don't want to use loose types (like Maps or CSVs) because I want the strong typing for my models. Is there no way for me to use custom types, and maybe specify that they are serializable as well? – Vedavyas Bhat Aug 22 '19 at 09:36
  • 1
    Sure. ```FirstAndSecond mySerializable; @Serialize() Map get firstAndSecond { return mySerializable.asMap(); } ``` *can't really do a code snippet well in the comment box. Basically you just need to have a property for your Serializable, and then have your `Serialize` property derive the primitive type from it. – Joe Conway Aug 22 '19 at 16:39