checked_yaml 1.0.2 checked_yaml: ^1.0.2 copied to clipboard
Generate more helpful exceptions when decoding YAML documents using package:json_serializable and package:yaml.
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:checked_yaml/checked_yaml.dart';
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable(
anyMap: true,
checked: true,
disallowUnrecognizedKeys: true,
nullable: false,
)
class Configuration {
@JsonKey(required: true)
final String name;
@JsonKey(required: true)
final int count;
Configuration({this.name, this.count}) {
if (name.isEmpty) {
throw ArgumentError.value(name, 'name', 'Cannot be empty.');
}
}
factory Configuration.fromJson(Map json) => _$ConfigurationFromJson(json);
Map<String, dynamic> toJson() => _$ConfigurationToJson(this);
@override
String toString() => 'Configuration: ${toJson()}';
}
void main(List<String> arguments) {
var sourcePathOrYaml = arguments.single;
String yamlContent;
if (FileSystemEntity.isFileSync(sourcePathOrYaml)) {
yamlContent = File(sourcePathOrYaml).readAsStringSync();
} else {
yamlContent = sourcePathOrYaml;
sourcePathOrYaml = null;
}
final config = checkedYamlDecode(
yamlContent, (m) => Configuration.fromJson(m),
sourceUrl: sourcePathOrYaml);
print(config);
}