jsonSerializable function

Builder jsonSerializable(
  1. BuilderOptions options
)

Supports package:build_runner creation and configuration of json_serializable.

Not meant to be invoked by hand-authored code.

Implementation

Builder jsonSerializable(BuilderOptions options) {
  try {
    final config = JsonSerializable.fromJson(options.config);
    return jsonPartBuilder(config: config);
  } on CheckedFromJsonException catch (e) {
    final lines = <String>[
      'Could not parse the options provided for `json_serializable`.'
    ];

    if (e.key != null) {
      lines.add('There is a problem with "${e.key}".');
    }
    if (e.message != null) {
      lines.add(e.message!);
    } else if (e.innerError != null) {
      lines.add(e.innerError.toString());
    }

    throw StateError(
      lines
          .join('\n')
          // TODO(kevmoo) remove when dart-lang/sdk#50756 is fixed!
          .replaceAll(" of ' in type cast'", ' in type cast'),
    );
  }
}