对Flutter中的对象进行JSON序列化和反序列化

前端之家收集整理的这篇文章主要介绍了对Flutter中的对象进行JSON序列化和反序列化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于Flutter从它的SDK中取出了dart:mirror,因此不再可能像dartson这样的库使用 JSON来对象序列化/反序列化.但是我读过build_value是达到类似目的的另一种方式.我找不到任何关于如何实施它的好例子,因为它包含大量的锅炉板代码.有人能举个例子吗?例如,这是我正在尝试序列化到对象的JSON:
{
"name":"John","age":30,"cars": [
    { "name":"Ford","models":[ "Fiesta","Focus","Mustang" ] },{ "name":"BMW","models":[ "320","X3","X5" ] },{ "name":"Fiat","models":[ "500","Panda" ] }
]

}

解决方法

我希望从提供的答案中获得更多细节.尽管它们是很好的建议,但它们对我来说太过普遍了解.所以在做了我自己的研究之后,我将把我的实现分享给我提供的上述JSON示例,希望它可以节省别人的时间.以下是我遵循的步骤:

>在我的Flutter项目中,首先我导入了以下库:

dependencies:

built_value: ^1.0.1
built_collection: ^1.0.0

dev_dependencies:

build_runner: ^0.3.0
built_value_generator:^1.0.1

>我创建了一个名为tool的文件夹.在其中,我放了2个文件:build.dart和watch.dart.这些文件的实现如下所示

build.dart

// Copyright (c) 2015,Google Inc. 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:async';

import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';

/// Example of how to use source_gen with [BuiltValueGenerator].
///
/// Import the generators you want and pass them to [build] as shown,/// specifying which files in which packages you want to run against.
Future main(List<String> args) async {
  await build(
      new PhaseGroup.singleAction(
          new GeneratorBuilder([new BuiltValueGenerator()]),new InputSet('built_value_example',const [
            'lib/model/*.dart','lib/*.dart',])),deleteFilesByDefault: true);
}

watch.dart

// Copyright (c) 2016,Google Inc. 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:async';

import 'package:build_runner/build_runner.dart';
import 'package:built_value_generator/built_value_generator.dart';
import 'package:source_gen/source_gen.dart';

/// Example of how to use source_gen with [BuiltValueGenerator].
///
/// This script runs a watcher that continuously rebuilds generated source.
///
/// Import the generators you want and pass them to [watch] as shown,/// specifying which files in which packages you want to run against.
Future main(List<String> args) async {
  watch(
      new PhaseGroup.singleAction(
          new GeneratorBuilder([new BuiltValueGenerator()]),'lib/*.dart'])),deleteFilesByDefault: true);
}

>我创建了一个serializers.dart文件,它将我的json字符串序列化为我的自定义dart对象,以及我的模型对象person.dart

serializers.dart

library serializers;

import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'model/person.dart';

part 'serializers.g.dart';

Serializers serializers = (
    _$serializers.toBuilder()..addPlugin(new StandardJsonPlugin())
).build();

person.dart

library person;

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

part 'person.g.dart';

abstract class Person implements Built<Person,PersonBuilder> {
  String get name;
  int get age;
  BuiltList<Car> get cars;

  Person._();
  factory Person([updates(PersonBuilder b)]) = _$Person;
  static Serializer<Person> get serializer => _$personSerializer;
}

abstract class Car implements Built<Car,CarBuilder> {
  String get name;
  BuiltList<String> get models;

  Car._();
  factory Car([updates(CarBuilder b)]) = _$Car;
  static Serializer<Car> get serializer => _$carSerializer;
}

>创建上面的4个文件后,它将显示一些编译器错误.不要介意他们.这是因为build.dart文件尚未运行.因此,在此步骤中,运行build.dart.如果您正在使用Webstorm,只需右键单击build.dart并点击“Run build.dart”即可.这将创建2个文件:“person.g.dart”和“serializers.g.dart”.如果您仔细注意,在我们的build.dart文件中,我们将’lib / model / .dart’和’lib / .dart’放在一起.构建通过遍历指定的路径知道在哪里查找这些文件,并查找包含部分“内容”的文件.因此,在运行build.dart文件之前,将该行保留在这些文件中非常重要
>最后,现在我可以在main.dart文件中使用序列化程序将json字符串序列化为我的自定义dart对象类Person.在我的main.dart中,我在initState()中添加了以下代码

main.dart

Person _person;

  @override
  void initState() {
    super.initState();
    String json = "{"
        "\"name\":\"John\",\"age\":30,\"cars\": "
        "["
        "{ \"name\":\"Ford\",\"models\":[ \"Fiesta\",\"Focus\",\"Mustang\" ] },"
        "{ \"name\":\"BMW\",\"models\":[ \"320\",\"X3\",\"X5\" ] },"
        "{ \"name\":\"Fiat\",\"models\":[ \"500\",\"Panda\" ] }"
        "]}";

    setState(() {
      _person = serializers.deserializeWith(
          Person.serializer,JSON.decode(json));
    });
  }

我的示例项目也可以在Github Built value sample project上获得

猜你在找的JavaScript相关文章