6

I have a dart object that includes a field of type Money, which itself is composed of 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 looks something like this:

class Money {
  final int amount;
  final String currency;

  const Money(this.amount, this.currency);
  ...
}

The above is to be mapped for use by sqflite, therefore the target JSON must be a flat JSON, like:

{
  "id": String,
  "type": String,
  "subtype": String,
  "origin": String,
  "name": String,
  "status": String,
  "currency": String,
  "balanceAmount": int;      <== value object
  "balanceCurrency": String; <== value object
}

I understand I can use JsonKey.readValue to extract a composite object from the entire JSON object prior to decoding.

But how can I do the opposite? Grab the two values from the Money instance and map them to balanceAmount and balanceCurrency?

Nothing in the search I did on json_serializable api docs, GitHub issues, or StackOverflow appears to respond to this in particular: how to map one field to two (or more) target keys?

lfreire
  • 103
  • 9

1 Answers1

0

this solution would be better if the generated code used full json in "balance" like this:

Money.fromJson(json)

But I don't know how to do that :(

import 'package:project/test/money.dart';
import 'package:json_annotation/json_annotation.dart';

part 'account.g.dart';

@JsonSerializable()
class Account {
  final String id;
  final String type;
  final String subtype;
  final String origin;
  final String name;
  final String status;
  final String currency;
  @JsonKey(fromJson: _dataFromJson)
  final Money balance; // <== value object

  Account({
    required this.id,
    required this.type,
    required this.subtype,
    required this.origin,
    required this.name,
    required this.status,
    required this.currency,
    required this.balance,
  });

  static Money _dataFromJson(Object json) {
    if (json is Map<String, dynamic>) {
      return Money(amount: json["balanceAmount"], currency: json["balanceCurrency"]);
    }

    throw ArgumentError.value(
      json,
      'json',
      'Cannot convert the provided data.',
    );
    // return Money(amount: 0, currency:"");
  }

  factory Account.fromJson(Map<String, dynamic> json) => Account(
        id: json['id'] as String,
        type: json['type'] as String,
        subtype: json['subtype'] as String,
        origin: json['origin'] as String,
        name: json['name'] as String,
        status: json['status'] as String,
        currency: json['currency'] as String,
        balance: Money.fromJson(json),
      );
  Map<String, dynamic> toJson() => _$AccountToJson(this);
}

// {
//   "id": String,
//   "type": String,
//   "subtype": String,
//   "origin": String,
//   "name": String,
//   "status": String,
//   "currency": String,
//   "balanceAmount": int;      <== value object
//   "balanceCurrency": String; <== value object
// }
Matsui
  • 1