我有一个 dart 对象,其中包含一个 type 字段Money
,该字段本身由amount
and组成currency
:
@JsonSerializable()
class Account {
final String id;
final String type;
final String subtype;
final String origin;
final String name;
final String status;
final String currency;
final Money balance; <== value object
...
}
Money
看起来像这样:
class Money {
final int amount;
final String currency;
const Money(this.amount, this.currency);
...
}
以上要被映射以供 使用sqflite
,因此目标 JSON 必须是平面 JSON,例如:
{
"id": String,
"type": String,
"subtype": String,
"origin": String,
"name": String,
"status": String,
"currency": String,
"balanceAmount": int; <== value object
"balanceCurrency": String; <== value object
}
我知道我可以JsonKey.readValue
在解码之前从整个 JSON 对象中提取复合对象。
但是我怎么能做相反的事情呢?从Money
实例中获取两个值并将它们映射到balanceAmount
和balanceCurrency
?
我在json_serializable
api 文档、GitHub 问题或 StackOverflow 上所做的搜索似乎没有特别回应这一点:如何将一个字段映射到两个(或更多)目标键?