yaml2podo 0.1.32 copy "yaml2podo: ^0.1.32" to clipboard
yaml2podo: ^0.1.32 copied to clipboard

discontinued

The `yaml2podo` is a generator and utility (all in one) that generates PODO classes to convert between JSON and Dart objects

yaml2podo #

This package is no longer supported because it was flagged by Google Dart developers as being published by an unknown person.
Publisher Unknown.
As a normal person, I believe that hardly anyone would want to use software from unknown publishers.

Version 0.1.32

The yaml2podo is a generator and utility (all in one) that generates PODO classes to convert between JSON and Dart objects

Example of use. #

Declarations (simple enough and informative).

example/json_objects.yaml2podo.yaml

Order:  
  date: DateTime
  items: List<OrderItem>
  amount: double
OrderItem:
  product: Product
  quantity.qty: int
  price: double  
Product:
  name: String
  id: int

Run utility.

pub global run yaml2podo example/json_objects.yaml2podo.yaml

Generated code does not contain dependencies and does not import anything. The same source code that you would write with your hands. Or at least very close to such a code.

example/json_objects.yaml2podo.dart

// Generated by 'yaml2podo'
// Version: 0.1.31
// https://pub.dev/packages/yaml2podo
// ignore_for_file: unused_element
// @dart = 2.12

class Order {
  final double? amount;
  final DateTime? date;
  final List<OrderItem?>? items;

  Order({this.amount, this.date, this.items});

  factory Order.fromJson(Map json) {
    return Order(
      amount: _toDouble(json['amount']),
      date: _toDateTime(json['date']),
      items: _toObjectList(json['items'], (e) => OrderItem.fromJson(e)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'amount': amount,
      'date': _fromDateTime(date),
      'items': _fromList(items, (OrderItem? e) => e?.toJson()),
    };
  }
}

class OrderItem {
  final double? price;
  final Product? product;
  final int? quantity;

  OrderItem({this.price, this.product, this.quantity});

  factory OrderItem.fromJson(Map json) {
    return OrderItem(
      price: _toDouble(json['price']),
      product: _toObject(json['product'], (e) => Product.fromJson(e)),
      quantity: json['qty'] as int?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'price': price,
      'product': product?.toJson(),
      'qty': quantity,
    };
  }
}

class Product {
  final int? id;
  final String? name;

  Product({this.id, this.name});

  factory Product.fromJson(Map json) {
    return Product(
      id: json['id'] as int?,
      name: json['name'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
    };
  }
}

T _checkType<T>(value) {
  if (value is T) {
    return value;
  }
  throw StateError(
      'Value of type ${value.runtimeType} is not a subtype of \'${T}\'');
}

String? _fromDateTime(data) {
  if (data == null) {
    return null;
  }
  if (data is DateTime) {
    return data.toIso8601String();
  }
  return _checkType<String>(data);
}

String? _fromEnum<T>(T? value) {
  if (value == null) {
    return null;
  }
  final str = '\$value';
  final offset = str.indexOf('.');
  if (offset == -1) {
    throw ArgumentError('The value is not an enum: \$value');
  }
  return str.substring(offset + 1);
}

List<O?>? _fromList<I, O>(List<I?>? data, O Function(I) toJson) {
  if (data == null) {
    return null;
  }
  final result = <O?>[];
  for (final element in data) {
    O? value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

Map<K?, O?>? _fromMap<K, I, O>(Map<K?, I?>? data, O Function(I) toJson) {
  if (data == null) {
    return null;
  }
  final result = <K?, O?>{};
  for (final key in data.keys) {
    O? value;
    final element = data[key];
    if (element != null) {
      value = toJson(element);
    }
    result[key] = value;
  }
  return result;
}

DateTime? _toDateTime(data) {
  if (data == null) {
    return null;
  }
  if (data is String) {
    return DateTime.parse(data);
  }
  return _checkType<DateTime>(data);
}

double? _toDouble(data) {
  if (data == null) {
    return null;
  }
  if (data is int) {
    return data.toDouble();
  }
  return _checkType<double>(data);
}

T? _toEnum<T>(String? name, Iterable<T> values) {
  if (name == null) {
    return null;
  }
  final offset = '\$T.'.length;
  for (final value in values) {
    final key = '\$value'.substring(offset);
    if (name == key) {
      return value;
    }
  }

  throw ArgumentError(
      'The getter \'\$name\' isn\'t defined for the class \'\$T\'');
}

List<T?>? _toList<T>(data, T? Function(dynamic) fromJson) {
  if (data == null) {
    return null;
  }
  final result = <T?>[];
  final sequence = _checkType<List>(data);
  for (final element in sequence) {
    T? value;
    if (element != null) {
      value = fromJson(element);
    }
    result.add(value);
  }
  return result;
}

Map<K?, V?>? _toMap<K, V>(data, V? Function(dynamic) fromJson) {
  if (data == null) {
    return null;
  }
  final result = <K?, V?>{};
  final map = _checkType<Map<K?, dynamic>>(data);
  for (final key in map.keys) {
    V? value;
    final element = map[key];
    if (element != null) {
      value = fromJson(element);
    }
    result[key] = value;
  }
  return result;
}

T? _toObject<T>(data, T Function(Map) fromJson) {
  if (data == null) {
    return null;
  }
  final json = _checkType<Map>(data);
  return fromJson(json);
}

List<T?>? _toObjectList<T>(data, T Function(Map) fromJson) {
  if (data == null) {
    return null;
  }
  final result = <T?>[];
  final sequence = _checkType<Iterable>(data);
  for (final element in sequence) {
    T? value;
    if (element != null) {
      final json = _checkType<Map>(element);
      value = fromJson(json);
    }
    result.add(value);
  }
  return result;
}

Map<K?, V?>? _toObjectMap<K, V>(data, V Function(Map) fromJson) {
  if (data == null) {
    return null;
  }
  final result = <K?, V?>{};
  final map = _checkType<Map<K?, dynamic>>(data);
  for (final key in map.keys) {
    V? value;
    final element = map[key];
    if (element != null) {
      final json = _checkType<Map>(element);
      value = fromJson(json);
    }
    result[key] = value;
  }
  return result;
}

/*
Order:  
  date: DateTime
  items: List<OrderItem>
  amount: double
OrderItem:
  product: Product
  quantity.qty: int
  price: double  
Product:
  name: String
  id: int
*/

And, of course, an example of using code.

example/example.dart

import 'json_objects.yaml2podo.dart';

void main() {
  final products = _getProducts();
  final items = _creataOrderItems(products);
  var order = Order(
      amount: _calculateAmount(items),
      date: DateTime(2019, 05, 31),
      items: items);
  var object = order.toJson();
  print(object);
  order = Order.fromJson(object);
  object = order.toJson();
  print(object);
}

double _calculateAmount(List<OrderItem> items) {
  var result = 0.0;
  for (var item in items) {
    result += item.quantity! * item.price!;
  }

  return result;
}

List<OrderItem> _creataOrderItems(List<Product> products) {
  final result = <OrderItem>[];
  for (var i = 0; i < products.length; i++) {
    final product = products[i];
    final orderItem =
        OrderItem(price: 10.0 + i, product: product, quantity: i + 1);
    result.add(orderItem);
  }

  return result;
}

List<Product> _getProducts() {
  final result = <Product>[];
  for (var i = 0; i < 2; i++) {
    final product = Product(id: i, name: 'Product $i');
    result.add(product);
  }

  return result;
}

Result:

{amount: 32.0, date: 2019-05-31T00:00:00.000, items: [{price: 10.0, product: {id: 0, name: Product 0}, qty: 1}, {price: 11.0, product: {id: 1, name: Product 1}, qty: 2}]}
{amount: 32.0, date: 2019-05-31T00:00:00.000, items: [{price: 10.0, product: {id: 0, name: Product 0}, qty: 1}, {price: 11.0, product: {id: 1, name: Product 1}, qty: 2}]}

How to install utility yaml2podo? #

Run the following command in the terminal

pub global activate yaml2podo
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

The `yaml2podo` is a generator and utility (all in one) that generates PODO classes to convert between JSON and Dart objects

Repository (GitHub)
View/report issues

License

BSD-3-Clause (LICENSE)

Dependencies

args, build, path, yaml

More

Packages that depend on yaml2podo