yaml2podo 0.1.20

yaml2podo #

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

Version 0.1.20

Example of use. #

Declarations (simple enough and informative).

example/json_objects.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.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.dart

// Generated by 'yaml2podo'
// Version: 0.1.20
// https://pub.dev/packages/yaml2podo

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

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

  factory Order.fromJson(Map<String, dynamic> 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, (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<String, dynamic> 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<String, dynamic> json) {
    return Product(
      id: json['id'] as int,
      name: json['name'] as String,
    );
  }

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

String _fromDateTime(data) {
  if (data == null) {
    return null;
  }
  if (data is DateTime) {
    return data.toIso8601String();
  }
  return data as String;
}

List _fromList(data, Function(dynamic) toJson) {
  if (data == null) {
    return null;
  }
  var result = [];
  for (var element in data) {
    var value;
    if (element != null) {
      value = toJson(element);
    }
    result.add(value);
  }
  return result;
}

DateTime _toDateTime(data) {
  if (data == null) {
    return null;
  }
  if (data is String) {
    return DateTime.parse(data);
  }
  return data as DateTime;
}

double _toDouble(data) {
  if (data == null) {
    return null;
  }
  if (data is int) {
    return data.toDouble();
  }
  return data as double;
}

T _toObject<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  return fromJson(data as Map<String, dynamic>);
}

List<T> _toObjectList<T>(data, T Function(Map<String, dynamic>) fromJson) {
  if (data == null) {
    return null;
  }
  var result = <T>[];
  for (var element in data) {
    T value;
    if (element != null) {
      value = fromJson(element as Map<String, dynamic>);
    }
    result.add(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.dart';

void main() {
  var products = _getProducts();
  var 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);
}

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

  return result;
}

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

  return result;
}

List<Product> _getProducts() {
  var result = <Product>[];
  for (var i = 0; i < 2; i++) {
    var 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

0.1.20 #

  • Fixed bug in DartCodeGenerator (properties were declared as 'final' regardless of the isFinal flag)

0.1.19 #

  • Minor improvements in Yaml2PodoGenerator (improved quality of generated code)

0.1.18 #

  • The source code of the method template toJson has been simplified.
  • The source code of example/example.dart has been simplified.

0.1.17 #

  • Added option to Yaml2PodoGenerator to format generated code (default: on)
  • Rewritten generator source code Yaml2PodoGenerator

0.1.16 #

Fixed bug in bin/resp2yaml.dart (incorrect generation of a comments in the file header)

0.1.15 #

  • Fixed bug in Yaml2PodoGenerator (incorrect constructor code was generated for a class with no properties)

0.1.14 #

  • Added generator Resp2YamlGenerator
  • Added utilty bin/resp2yaml.dart

0.1.12 #

  • Added option to Yaml2PodoGenerator to store model prototypes as comments within the generated code (default: on)
  • Fixed minor bug in Yaml2PodoGenerator (first character of property identifiers has not been converted to lowercase)

0.1.11 #

  • Restored code formatting of generated code in bin/yaml2podo.dart

0.1.10 #

  • Added option to Yaml2PodoGenerator to generate immutable fields (default: on)

0.1.9 #

  • Minor changes in Yaml2PodoGenerator (converter methods have been converted to static methods)
  • Minor improvements in Yaml2PodoGenerator (unused converter methods are not generated)

0.1.8 #

  • Minor improvements in Yaml2PodoGenerator (removed unnecessary checks and wrappers in generated code for save calls)

0.1.7 #

  • Fixed bug in Yaml2PodoGenerator

0.1.6 #

  • Minor changes in convertToIdentifier()

0.1.5 #

  • Fixed bugs in Yaml2PodoGenerator
  • Fixed bugs in camelizeIdentifier()
  • Fixed bugs in convertToIdentifier()

0.1.4 #

  • Minor improvements in source code

0.1.3 #

  • Updated description for a more accurate understanding of the purpose of this software

0.1.2 #

  • Minor improvements in _JsonConverter

0.1.1 #

  • Fixed bugs in Yaml2PodoGenerator

0.1.0 #

  • Initial release

example/example.dart

import 'json_objects.dart';

void main() {
  var products = _getProducts();
  var 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);
}

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

  return result;
}

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

  return result;
}

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

  return result;
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  yaml2podo: ^0.1.20

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:yaml2podo/dart_code_generator.dart';
import 'package:yaml2podo/resp2yaml.dart';
import 'package:yaml2podo/type_system.dart';
import 'package:yaml2podo/version.dart';
import 'package:yaml2podo/yaml2podo_generator.dart';
import 'package:yaml2podo/_type_parser.dart';
import 'package:yaml2podo/_utils.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health issues and suggestions

Document public APIs. (-1 points)

62 out of 62 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
args ^1.5.1 1.5.2
dart_style ^1.2.7 1.2.9
path ^1.6.2 1.6.2
yaml ^2.1.15 2.1.16
Transitive dependencies
analyzer 0.37.0
async 2.3.0
charcode 1.1.2
collection 1.14.11
convert 2.1.1
crypto 2.0.6
csslib 0.16.1
front_end 0.1.20
glob 1.1.7
html 0.14.0+2
kernel 0.3.20
meta 1.1.7
package_config 1.0.5
pub_semver 1.4.2
source_span 1.5.5
string_scanner 1.0.4
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+12
Dev dependencies
http ^0.12.0+2
pedantic ^1.7.0 1.8.0+1
test ^1.6.3

Admin