Dart JSON Decoder

I am surprised that the dart has no built-in-to-json and json-to-object mapper.

I read that we need to manually transfer the display, which is not like it.

In any case, although I did not fully test it for my use case, I found dart-exportable to be very useful for half of my requirement.

Any suggested json decoding package for an object?

+2
source share
3 answers

Your best option is to use the Smoke library .

Mirrors, , Codegen. PolymerDart, "", .

/ Mirrors; , .

, :

abstract class Serializable {
  static fromJson(Type t, Map json) {
    var typeMirror = reflectType(t);
    T obj = typeMirror.newInstance(new Symbol(""), const[]).reflectee;
    json.forEach((k, v) {
      if (v is Map) {
        var d = smoke.getDeclaration(t, smoke.nameToSymbol(k));
        smoke.write(obj, smoke.nameToSymbol(k), Serializable.fromJson(d.type, v));
      } else {
        smoke.write(obj, smoke.nameToSymbol(k), v);
      }
    });
    return obj;
  }

  Map toJson() {
    var options = new smoke.QueryOptions(includeProperties: false);
    var res = smoke.query(runtimeType, options);
    var map = {};
    res.forEach((r) => map[smoke.symbolToName(r.name)] = smoke.read(this, r.name));
    return map;
  }
}

(, ) Smoke; :

https://code.google.com/p/dart/issues/detail?id=20584

, "" , , ; , ; - , JSON-, !

+2
+2

/ Json - .

( "yaml")

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

( "yaml2podo.dart")

// Generated by tool.

import 'package:marshalling/json_serializer.dart';

final json = JsonSerializer()
  ..addType(() => Order())
  ..addType(() => OrderItem())
  ..addType(() => Product())
  ..addIterableType<List<OrderItem>, OrderItem>(() => <OrderItem>[])
  ..addAccessor('amount', (o) => o.amount, (o, v) => o.amount = v)
  ..addAccessor('date', (o) => o.date, (o, v) => o.date = v)
  ..addAccessor('id', (o) => o.id, (o, v) => o.id = v)
  ..addAccessor('items', (o) => o.items, (o, v) => o.items = v)
  ..addAccessor('name', (o) => o.name, (o, v) => o.name = v)
  ..addAccessor('price', (o) => o.price, (o, v) => o.price = v)
  ..addAccessor('product', (o) => o.product, (o, v) => o.product = v)
  ..addAccessor('quantity', (o) => o.quantity, (o, v) => o.quantity = v)
  ..addProperty<Order, double>('amount')
  ..addProperty<Order, DateTime>('date')
  ..addProperty<Order, List<OrderItem>>('items')
  ..addProperty<OrderItem, int>('quantity')
  ..addProperty<OrderItem, double>('price')
  ..addProperty<OrderItem, Product>('product')
  ..addProperty<Product, String>('name')
  ..addProperty<Product, String>('id');

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

  Order();

  factory Order.fromJson(Map map) {
    return json.unmarshal<Order>(map);
  }

  Map<String, dynamic> toJson() {
    return json.marshal(this) as Map<String, dynamic>;
  }
}

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

  OrderItem();

  factory OrderItem.fromJson(Map map) {
    return json.unmarshal<OrderItem>(map);
  }

  Map<String, dynamic> toJson() {
    return json.marshal(this) as Map<String, dynamic>;
  }
}

class Product {
  String name;
  String id;

  Product();

  factory Product.fromJson(Map map) {
    return json.unmarshal<Product>(map);
  }

  Map<String, dynamic> toJson() {
    return json.marshal(this) as Map<String, dynamic>;
  }
}

( / )

import 'json_objects.dart';

void main() {
  var products = <Product>[];
  for (var i = 0; i < 3; i++) {
    var product = Product();
    product.id = '$i';
    product.name = 'Product $i';
    products.add(product);
  }

  var order = Order();
  order.date = DateTime.now();
  order.items = [];
  order.amount = 0;
  for (var product in products) {
    var item = OrderItem();
    item.product = product;
    item.quantity = 5;
    item.price = 10;
    order.items.add(item);
    order.amount += item.quantity * item.price;
  }

  var jsonOrder = order.toJson();
  print(jsonOrder);
  var newOrder = Order.fromJson(jsonOrder);
  for (var item in newOrder.items) {
    print(item.product.name);
    print(item.price);
  }
}

:

{amount: 150.0, date: 2019-05-25T00:58:24.799475, items: [{quantity: 5, price: 10.0, product: {name: Product 0, id: 0}}, {quantity: 5, price: 10.0, product: {name: Product 1, id: 1}}, {quantity: 5, price: 10.0, product: {name: Product 2, id: 2}}]} Product 0 10.0 Product 1 10.0 Product 2 10.0

0

All Articles