1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00

Adding JSON example. (#1)

This commit is contained in:
Andrew Brogdon
2018-06-27 15:03:58 -07:00
committed by GitHub
parent 05319d3e8c
commit f0fe2289e6
76 changed files with 3487 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
import 'package:jsonexample/built_value/built_simple_object.dart';
part 'built_complex_object.g.dart';
abstract class BuiltComplexObject
implements Built<BuiltComplexObject, BuiltComplexObjectBuilder> {
static Serializer<BuiltComplexObject> get serializer =>
_$builtComplexObjectSerializer;
@nullable
String get aString;
@nullable
int get anInt;
@nullable
double get aDouble;
BuiltSimpleObject get anObject;
BuiltList<String> get aListOfStrings;
BuiltList<int> get aListOfInts;
BuiltList<double> get aListOfDoubles;
BuiltList<BuiltSimpleObject> get aListOfObjects;
BuiltComplexObject._();
factory BuiltComplexObject([updates(BuiltComplexObjectBuilder b)]) =
_$BuiltComplexObject;
}

View File

@@ -0,0 +1,345 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'built_complex_object.dart';
// **************************************************************************
// Generator: BuiltValueGenerator
// **************************************************************************
// ignore_for_file: always_put_control_body_on_new_line
// ignore_for_file: annotate_overrides
// ignore_for_file: avoid_annotating_with_dynamic
// ignore_for_file: avoid_returning_this
// ignore_for_file: omit_local_variable_types
// ignore_for_file: prefer_expression_function_bodies
// ignore_for_file: sort_constructors_first
Serializer<BuiltComplexObject> _$builtComplexObjectSerializer =
new _$BuiltComplexObjectSerializer();
class _$BuiltComplexObjectSerializer
implements StructuredSerializer<BuiltComplexObject> {
@override
final Iterable<Type> types = const [BuiltComplexObject, _$BuiltComplexObject];
@override
final String wireName = 'BuiltComplexObject';
@override
Iterable serialize(Serializers serializers, BuiltComplexObject object,
{FullType specifiedType: FullType.unspecified}) {
final result = <Object>[
'anObject',
serializers.serialize(object.anObject,
specifiedType: const FullType(BuiltSimpleObject)),
'aListOfStrings',
serializers.serialize(object.aListOfStrings,
specifiedType:
const FullType(BuiltList, const [const FullType(String)])),
'aListOfInts',
serializers.serialize(object.aListOfInts,
specifiedType:
const FullType(BuiltList, const [const FullType(int)])),
'aListOfDoubles',
serializers.serialize(object.aListOfDoubles,
specifiedType:
const FullType(BuiltList, const [const FullType(double)])),
'aListOfObjects',
serializers.serialize(object.aListOfObjects,
specifiedType: const FullType(
BuiltList, const [const FullType(BuiltSimpleObject)])),
];
if (object.aString != null) {
result
..add('aString')
..add(serializers.serialize(object.aString,
specifiedType: const FullType(String)));
}
if (object.anInt != null) {
result
..add('anInt')
..add(serializers.serialize(object.anInt,
specifiedType: const FullType(int)));
}
if (object.aDouble != null) {
result
..add('aDouble')
..add(serializers.serialize(object.aDouble,
specifiedType: const FullType(double)));
}
return result;
}
@override
BuiltComplexObject deserialize(Serializers serializers, Iterable serialized,
{FullType specifiedType: FullType.unspecified}) {
final result = new BuiltComplexObjectBuilder();
final iterator = serialized.iterator;
while (iterator.moveNext()) {
final key = iterator.current as String;
iterator.moveNext();
final dynamic value = iterator.current;
switch (key) {
case 'aString':
result.aString = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
break;
case 'anInt':
result.anInt = serializers.deserialize(value,
specifiedType: const FullType(int)) as int;
break;
case 'aDouble':
result.aDouble = serializers.deserialize(value,
specifiedType: const FullType(double)) as double;
break;
case 'anObject':
result.anObject.replace(serializers.deserialize(value,
specifiedType: const FullType(BuiltSimpleObject))
as BuiltSimpleObject);
break;
case 'aListOfStrings':
result.aListOfStrings.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(String)]))
as BuiltList);
break;
case 'aListOfInts':
result.aListOfInts.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(int)]))
as BuiltList);
break;
case 'aListOfDoubles':
result.aListOfDoubles.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(double)]))
as BuiltList);
break;
case 'aListOfObjects':
result.aListOfObjects.replace(serializers.deserialize(value,
specifiedType: const FullType(
BuiltList, const [const FullType(BuiltSimpleObject)]))
as BuiltList);
break;
}
}
return result.build();
}
}
class _$BuiltComplexObject extends BuiltComplexObject {
@override
final String aString;
@override
final int anInt;
@override
final double aDouble;
@override
final BuiltSimpleObject anObject;
@override
final BuiltList<String> aListOfStrings;
@override
final BuiltList<int> aListOfInts;
@override
final BuiltList<double> aListOfDoubles;
@override
final BuiltList<BuiltSimpleObject> aListOfObjects;
factory _$BuiltComplexObject([void updates(BuiltComplexObjectBuilder b)]) =>
(new BuiltComplexObjectBuilder()..update(updates)).build();
_$BuiltComplexObject._(
{this.aString,
this.anInt,
this.aDouble,
this.anObject,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles,
this.aListOfObjects})
: super._() {
if (anObject == null)
throw new BuiltValueNullFieldError('BuiltComplexObject', 'anObject');
if (aListOfStrings == null)
throw new BuiltValueNullFieldError(
'BuiltComplexObject', 'aListOfStrings');
if (aListOfInts == null)
throw new BuiltValueNullFieldError('BuiltComplexObject', 'aListOfInts');
if (aListOfDoubles == null)
throw new BuiltValueNullFieldError(
'BuiltComplexObject', 'aListOfDoubles');
if (aListOfObjects == null)
throw new BuiltValueNullFieldError(
'BuiltComplexObject', 'aListOfObjects');
}
@override
BuiltComplexObject rebuild(void updates(BuiltComplexObjectBuilder b)) =>
(toBuilder()..update(updates)).build();
@override
BuiltComplexObjectBuilder toBuilder() =>
new BuiltComplexObjectBuilder()..replace(this);
@override
bool operator ==(dynamic other) {
if (identical(other, this)) return true;
if (other is! BuiltComplexObject) return false;
return aString == other.aString &&
anInt == other.anInt &&
aDouble == other.aDouble &&
anObject == other.anObject &&
aListOfStrings == other.aListOfStrings &&
aListOfInts == other.aListOfInts &&
aListOfDoubles == other.aListOfDoubles &&
aListOfObjects == other.aListOfObjects;
}
@override
int get hashCode {
return $jf($jc(
$jc(
$jc(
$jc(
$jc(
$jc($jc($jc(0, aString.hashCode), anInt.hashCode),
aDouble.hashCode),
anObject.hashCode),
aListOfStrings.hashCode),
aListOfInts.hashCode),
aListOfDoubles.hashCode),
aListOfObjects.hashCode));
}
@override
String toString() {
return (newBuiltValueToStringHelper('BuiltComplexObject')
..add('aString', aString)
..add('anInt', anInt)
..add('aDouble', aDouble)
..add('anObject', anObject)
..add('aListOfStrings', aListOfStrings)
..add('aListOfInts', aListOfInts)
..add('aListOfDoubles', aListOfDoubles)
..add('aListOfObjects', aListOfObjects))
.toString();
}
}
class BuiltComplexObjectBuilder
implements Builder<BuiltComplexObject, BuiltComplexObjectBuilder> {
_$BuiltComplexObject _$v;
String _aString;
String get aString => _$this._aString;
set aString(String aString) => _$this._aString = aString;
int _anInt;
int get anInt => _$this._anInt;
set anInt(int anInt) => _$this._anInt = anInt;
double _aDouble;
double get aDouble => _$this._aDouble;
set aDouble(double aDouble) => _$this._aDouble = aDouble;
BuiltSimpleObjectBuilder _anObject;
BuiltSimpleObjectBuilder get anObject =>
_$this._anObject ??= new BuiltSimpleObjectBuilder();
set anObject(BuiltSimpleObjectBuilder anObject) =>
_$this._anObject = anObject;
ListBuilder<String> _aListOfStrings;
ListBuilder<String> get aListOfStrings =>
_$this._aListOfStrings ??= new ListBuilder<String>();
set aListOfStrings(ListBuilder<String> aListOfStrings) =>
_$this._aListOfStrings = aListOfStrings;
ListBuilder<int> _aListOfInts;
ListBuilder<int> get aListOfInts =>
_$this._aListOfInts ??= new ListBuilder<int>();
set aListOfInts(ListBuilder<int> aListOfInts) =>
_$this._aListOfInts = aListOfInts;
ListBuilder<double> _aListOfDoubles;
ListBuilder<double> get aListOfDoubles =>
_$this._aListOfDoubles ??= new ListBuilder<double>();
set aListOfDoubles(ListBuilder<double> aListOfDoubles) =>
_$this._aListOfDoubles = aListOfDoubles;
ListBuilder<BuiltSimpleObject> _aListOfObjects;
ListBuilder<BuiltSimpleObject> get aListOfObjects =>
_$this._aListOfObjects ??= new ListBuilder<BuiltSimpleObject>();
set aListOfObjects(ListBuilder<BuiltSimpleObject> aListOfObjects) =>
_$this._aListOfObjects = aListOfObjects;
BuiltComplexObjectBuilder();
BuiltComplexObjectBuilder get _$this {
if (_$v != null) {
_aString = _$v.aString;
_anInt = _$v.anInt;
_aDouble = _$v.aDouble;
_anObject = _$v.anObject?.toBuilder();
_aListOfStrings = _$v.aListOfStrings?.toBuilder();
_aListOfInts = _$v.aListOfInts?.toBuilder();
_aListOfDoubles = _$v.aListOfDoubles?.toBuilder();
_aListOfObjects = _$v.aListOfObjects?.toBuilder();
_$v = null;
}
return this;
}
@override
void replace(BuiltComplexObject other) {
if (other == null) throw new ArgumentError.notNull('other');
_$v = other as _$BuiltComplexObject;
}
@override
void update(void updates(BuiltComplexObjectBuilder b)) {
if (updates != null) updates(this);
}
@override
_$BuiltComplexObject build() {
_$BuiltComplexObject _$result;
try {
_$result = _$v ??
new _$BuiltComplexObject._(
aString: aString,
anInt: anInt,
aDouble: aDouble,
anObject: anObject.build(),
aListOfStrings: aListOfStrings.build(),
aListOfInts: aListOfInts.build(),
aListOfDoubles: aListOfDoubles.build(),
aListOfObjects: aListOfObjects.build());
} catch (_) {
String _$failedField;
try {
_$failedField = 'anObject';
anObject.build();
_$failedField = 'aListOfStrings';
aListOfStrings.build();
_$failedField = 'aListOfInts';
aListOfInts.build();
_$failedField = 'aListOfDoubles';
aListOfDoubles.build();
_$failedField = 'aListOfObjects';
aListOfObjects.build();
} catch (e) {
throw new BuiltValueNestedFieldError(
'BuiltComplexObject', _$failedField, e.toString());
}
rethrow;
}
replace(_$result);
return _$result;
}
}

View File

@@ -0,0 +1,35 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';
part 'built_simple_object.g.dart';
abstract class BuiltSimpleObject
implements Built<BuiltSimpleObject, BuiltSimpleObjectBuilder> {
static Serializer<BuiltSimpleObject> get serializer =>
_$builtSimpleObjectSerializer;
@nullable
String get aString;
@nullable
int get anInt;
@nullable
double get aDouble;
BuiltList<String> get aListOfStrings;
BuiltList<int> get aListOfInts;
BuiltList<double> get aListOfDoubles;
BuiltSimpleObject._();
factory BuiltSimpleObject([updates(BuiltSimpleObjectBuilder b)]) =
_$BuiltSimpleObject;
}

View File

@@ -0,0 +1,286 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'built_simple_object.dart';
// **************************************************************************
// Generator: BuiltValueGenerator
// **************************************************************************
// ignore_for_file: always_put_control_body_on_new_line
// ignore_for_file: annotate_overrides
// ignore_for_file: avoid_annotating_with_dynamic
// ignore_for_file: avoid_returning_this
// ignore_for_file: omit_local_variable_types
// ignore_for_file: prefer_expression_function_bodies
// ignore_for_file: sort_constructors_first
Serializer<BuiltSimpleObject> _$builtSimpleObjectSerializer =
new _$BuiltSimpleObjectSerializer();
class _$BuiltSimpleObjectSerializer
implements StructuredSerializer<BuiltSimpleObject> {
@override
final Iterable<Type> types = const [BuiltSimpleObject, _$BuiltSimpleObject];
@override
final String wireName = 'BuiltSimpleObject';
@override
Iterable serialize(Serializers serializers, BuiltSimpleObject object,
{FullType specifiedType: FullType.unspecified}) {
final result = <Object>[
'aListOfStrings',
serializers.serialize(object.aListOfStrings,
specifiedType:
const FullType(BuiltList, const [const FullType(String)])),
'aListOfInts',
serializers.serialize(object.aListOfInts,
specifiedType:
const FullType(BuiltList, const [const FullType(int)])),
'aListOfDoubles',
serializers.serialize(object.aListOfDoubles,
specifiedType:
const FullType(BuiltList, const [const FullType(double)])),
];
if (object.aString != null) {
result
..add('aString')
..add(serializers.serialize(object.aString,
specifiedType: const FullType(String)));
}
if (object.anInt != null) {
result
..add('anInt')
..add(serializers.serialize(object.anInt,
specifiedType: const FullType(int)));
}
if (object.aDouble != null) {
result
..add('aDouble')
..add(serializers.serialize(object.aDouble,
specifiedType: const FullType(double)));
}
return result;
}
@override
BuiltSimpleObject deserialize(Serializers serializers, Iterable serialized,
{FullType specifiedType: FullType.unspecified}) {
final result = new BuiltSimpleObjectBuilder();
final iterator = serialized.iterator;
while (iterator.moveNext()) {
final key = iterator.current as String;
iterator.moveNext();
final dynamic value = iterator.current;
switch (key) {
case 'aString':
result.aString = serializers.deserialize(value,
specifiedType: const FullType(String)) as String;
break;
case 'anInt':
result.anInt = serializers.deserialize(value,
specifiedType: const FullType(int)) as int;
break;
case 'aDouble':
result.aDouble = serializers.deserialize(value,
specifiedType: const FullType(double)) as double;
break;
case 'aListOfStrings':
result.aListOfStrings.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(String)]))
as BuiltList);
break;
case 'aListOfInts':
result.aListOfInts.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(int)]))
as BuiltList);
break;
case 'aListOfDoubles':
result.aListOfDoubles.replace(serializers.deserialize(value,
specifiedType:
const FullType(BuiltList, const [const FullType(double)]))
as BuiltList);
break;
}
}
return result.build();
}
}
class _$BuiltSimpleObject extends BuiltSimpleObject {
@override
final String aString;
@override
final int anInt;
@override
final double aDouble;
@override
final BuiltList<String> aListOfStrings;
@override
final BuiltList<int> aListOfInts;
@override
final BuiltList<double> aListOfDoubles;
factory _$BuiltSimpleObject([void updates(BuiltSimpleObjectBuilder b)]) =>
(new BuiltSimpleObjectBuilder()..update(updates)).build();
_$BuiltSimpleObject._(
{this.aString,
this.anInt,
this.aDouble,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles})
: super._() {
if (aListOfStrings == null)
throw new BuiltValueNullFieldError('BuiltSimpleObject', 'aListOfStrings');
if (aListOfInts == null)
throw new BuiltValueNullFieldError('BuiltSimpleObject', 'aListOfInts');
if (aListOfDoubles == null)
throw new BuiltValueNullFieldError('BuiltSimpleObject', 'aListOfDoubles');
}
@override
BuiltSimpleObject rebuild(void updates(BuiltSimpleObjectBuilder b)) =>
(toBuilder()..update(updates)).build();
@override
BuiltSimpleObjectBuilder toBuilder() =>
new BuiltSimpleObjectBuilder()..replace(this);
@override
bool operator ==(dynamic other) {
if (identical(other, this)) return true;
if (other is! BuiltSimpleObject) return false;
return aString == other.aString &&
anInt == other.anInt &&
aDouble == other.aDouble &&
aListOfStrings == other.aListOfStrings &&
aListOfInts == other.aListOfInts &&
aListOfDoubles == other.aListOfDoubles;
}
@override
int get hashCode {
return $jf($jc(
$jc(
$jc(
$jc($jc($jc(0, aString.hashCode), anInt.hashCode),
aDouble.hashCode),
aListOfStrings.hashCode),
aListOfInts.hashCode),
aListOfDoubles.hashCode));
}
@override
String toString() {
return (newBuiltValueToStringHelper('BuiltSimpleObject')
..add('aString', aString)
..add('anInt', anInt)
..add('aDouble', aDouble)
..add('aListOfStrings', aListOfStrings)
..add('aListOfInts', aListOfInts)
..add('aListOfDoubles', aListOfDoubles))
.toString();
}
}
class BuiltSimpleObjectBuilder
implements Builder<BuiltSimpleObject, BuiltSimpleObjectBuilder> {
_$BuiltSimpleObject _$v;
String _aString;
String get aString => _$this._aString;
set aString(String aString) => _$this._aString = aString;
int _anInt;
int get anInt => _$this._anInt;
set anInt(int anInt) => _$this._anInt = anInt;
double _aDouble;
double get aDouble => _$this._aDouble;
set aDouble(double aDouble) => _$this._aDouble = aDouble;
ListBuilder<String> _aListOfStrings;
ListBuilder<String> get aListOfStrings =>
_$this._aListOfStrings ??= new ListBuilder<String>();
set aListOfStrings(ListBuilder<String> aListOfStrings) =>
_$this._aListOfStrings = aListOfStrings;
ListBuilder<int> _aListOfInts;
ListBuilder<int> get aListOfInts =>
_$this._aListOfInts ??= new ListBuilder<int>();
set aListOfInts(ListBuilder<int> aListOfInts) =>
_$this._aListOfInts = aListOfInts;
ListBuilder<double> _aListOfDoubles;
ListBuilder<double> get aListOfDoubles =>
_$this._aListOfDoubles ??= new ListBuilder<double>();
set aListOfDoubles(ListBuilder<double> aListOfDoubles) =>
_$this._aListOfDoubles = aListOfDoubles;
BuiltSimpleObjectBuilder();
BuiltSimpleObjectBuilder get _$this {
if (_$v != null) {
_aString = _$v.aString;
_anInt = _$v.anInt;
_aDouble = _$v.aDouble;
_aListOfStrings = _$v.aListOfStrings?.toBuilder();
_aListOfInts = _$v.aListOfInts?.toBuilder();
_aListOfDoubles = _$v.aListOfDoubles?.toBuilder();
_$v = null;
}
return this;
}
@override
void replace(BuiltSimpleObject other) {
if (other == null) throw new ArgumentError.notNull('other');
_$v = other as _$BuiltSimpleObject;
}
@override
void update(void updates(BuiltSimpleObjectBuilder b)) {
if (updates != null) updates(this);
}
@override
_$BuiltSimpleObject build() {
_$BuiltSimpleObject _$result;
try {
_$result = _$v ??
new _$BuiltSimpleObject._(
aString: aString,
anInt: anInt,
aDouble: aDouble,
aListOfStrings: aListOfStrings.build(),
aListOfInts: aListOfInts.build(),
aListOfDoubles: aListOfDoubles.build());
} catch (_) {
String _$failedField;
try {
_$failedField = 'aListOfStrings';
aListOfStrings.build();
_$failedField = 'aListOfInts';
aListOfInts.build();
_$failedField = 'aListOfDoubles';
aListOfDoubles.build();
} catch (e) {
throw new BuiltValueNestedFieldError(
'BuiltSimpleObject', _$failedField, e.toString());
}
rethrow;
}
replace(_$result);
return _$result;
}
}

View File

@@ -0,0 +1,25 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
library serializers;
import 'package:built_collection/built_collection.dart';
import 'package:built_value/serializer.dart';
import 'package:built_value/standard_json_plugin.dart';
import 'package:jsonexample/built_value/built_complex_object.dart';
import 'package:jsonexample/built_value/built_simple_object.dart';
part 'built_value_serializers.g.dart';
@SerializersFor(const [
BuiltSimpleObject,
BuiltComplexObject,
])
// By default, `built_value` serialization uses lists and is not compatible
// with other JSON formats. If you'd like to serialize data using a map-based
// JSON approach (which is what you'll find in the json_strings.dart file in
// this project), you can add the StandardJsonPlugin as you see here.
final Serializers serializers =
(_$serializers.toBuilder()..addPlugin(StandardJsonPlugin())).build();

View File

@@ -0,0 +1,45 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of serializers;
// **************************************************************************
// Generator: BuiltValueGenerator
// **************************************************************************
// ignore_for_file: always_put_control_body_on_new_line
// ignore_for_file: annotate_overrides
// ignore_for_file: avoid_annotating_with_dynamic
// ignore_for_file: avoid_returning_this
// ignore_for_file: omit_local_variable_types
// ignore_for_file: prefer_expression_function_bodies
// ignore_for_file: sort_constructors_first
Serializers _$serializers = (new Serializers().toBuilder()
..add(BuiltComplexObject.serializer)
..add(BuiltSimpleObject.serializer)
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(String)]),
() => new ListBuilder<String>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(int)]),
() => new ListBuilder<int>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(double)]),
() => new ListBuilder<double>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(String)]),
() => new ListBuilder<String>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(int)]),
() => new ListBuilder<int>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(double)]),
() => new ListBuilder<double>())
..addBuilderFactory(
const FullType(BuiltList, const [const FullType(BuiltSimpleObject)]),
() => new ListBuilder<BuiltSimpleObject>()))
.build();

View File

@@ -0,0 +1,52 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:jsonexample/dart_convert/converted_simple_object.dart';
class ConvertedComplexObject {
const ConvertedComplexObject({
this.aString,
this.anInt,
this.aDouble,
this.anObject,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles,
this.aListOfObjects,
});
final String aString;
final int anInt;
final double aDouble;
final ConvertedSimpleObject anObject;
final List<String> aListOfStrings;
final List<int> aListOfInts;
final List<double> aListOfDoubles;
final List<ConvertedSimpleObject> aListOfObjects;
factory ConvertedComplexObject.fromJson(Map<String, dynamic> json) {
if (json == null) return null;
return ConvertedComplexObject(
aString: json['aString'],
anInt: json['anInt'],
aDouble: json['aDouble'],
anObject: json['anObject'] != null
? ConvertedSimpleObject.fromJson(json['anObject'])
: null,
aListOfStrings: json['aListOfStrings'] != null
? List<String>.from(json['aListOfStrings'])
: null,
aListOfInts: json['aListOfInts'] != null
? List<int>.from(json['aListOfInts'])
: null,
aListOfDoubles: json['aListOfDoubles'] != null
? List<double>.from(json['aListOfDoubles'])
: null,
aListOfObjects: json['aListOfObjects'] != null
? List<ConvertedSimpleObject>.from(json['aListOfObjects']
.map((o) => ConvertedSimpleObject.fromJson(o)))
: null);
}
}

View File

@@ -0,0 +1,40 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class ConvertedSimpleObject {
const ConvertedSimpleObject({
this.aString,
this.anInt,
this.aDouble,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles,
});
final String aString;
final int anInt;
final double aDouble;
final List<String> aListOfStrings;
final List<int> aListOfInts;
final List<double> aListOfDoubles;
factory ConvertedSimpleObject.fromJson(Map<String, dynamic> json) {
if (json == null) return null;
return ConvertedSimpleObject(
aString: json['aString'],
anInt: json['anInt'],
aDouble: json['aDouble'],
aListOfStrings: json['aListOfStrings'] != null
? List<String>.from(json['aListOfStrings'])
: null,
aListOfInts: json['aListOfInts'] != null
? List<int>.from(json['aListOfInts'])
: null,
aListOfDoubles: json['aListOfDoubles'] != null
? List<double>.from(json['aListOfDoubles'])
: null,
);
}
}

View File

@@ -0,0 +1,35 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:json_annotation/json_annotation.dart';
import 'package:jsonexample/json_serializable/serializable_simple_object.dart';
part 'serializable_complex_object.g.dart';
@JsonSerializable()
class SerializableComplexObject extends Object
with _$SerializableComplexObjectSerializerMixin {
SerializableComplexObject({
this.aString,
this.anInt,
this.aDouble,
this.anObject,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles,
this.aListOfObjects,
});
final String aString;
final int anInt;
final double aDouble;
final SerializableSimpleObject anObject;
final List<String> aListOfStrings;
final List<int> aListOfInts;
final List<double> aListOfDoubles;
final List<SerializableSimpleObject> aListOfObjects;
factory SerializableComplexObject.fromJson(Map<String, dynamic> json) =>
_$SerializableComplexObjectFromJson(json);
}

View File

@@ -0,0 +1,56 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'serializable_complex_object.dart';
// **************************************************************************
// Generator: JsonSerializableGenerator
// **************************************************************************
SerializableComplexObject _$SerializableComplexObjectFromJson(
Map<String, dynamic> json) =>
new SerializableComplexObject(
aString: json['aString'] as String,
anInt: json['anInt'] as int,
aDouble: (json['aDouble'] as num)?.toDouble(),
anObject: json['anObject'] == null
? null
: new SerializableSimpleObject.fromJson(
json['anObject'] as Map<String, dynamic>),
aListOfStrings:
(json['aListOfStrings'] as List)?.map((e) => e as String)?.toList(),
aListOfInts:
(json['aListOfInts'] as List)?.map((e) => e as int)?.toList(),
aListOfDoubles: (json['aListOfDoubles'] as List)
?.map((e) => (e as num)?.toDouble())
?.toList(),
aListOfObjects: (json['aListOfObjects'] as List)
?.map((e) => e == null
? null
: new SerializableSimpleObject.fromJson(
e as Map<String, dynamic>))
?.toList());
abstract class _$SerializableComplexObjectSerializerMixin {
String get aString;
int get anInt;
double get aDouble;
SerializableSimpleObject get anObject;
List<String> get aListOfStrings;
List<int> get aListOfInts;
List<double> get aListOfDoubles;
List<SerializableSimpleObject> get aListOfObjects;
Map<String, dynamic> toJson() => <String, dynamic>{
'aString': aString,
'anInt': anInt,
'aDouble': aDouble,
'anObject': anObject,
'aListOfStrings': aListOfStrings,
'aListOfInts': aListOfInts,
'aListOfDoubles': aListOfDoubles,
'aListOfObjects': aListOfObjects
};
}

View File

@@ -0,0 +1,32 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:json_annotation/json_annotation.dart';
part 'serializable_simple_object.g.dart';
/// An annotation for the code generator to know that this class needs the
/// JSON serialization logic to be generated.
@JsonSerializable()
class SerializableSimpleObject extends Object
with _$SerializableSimpleObjectSerializerMixin {
SerializableSimpleObject({
this.aString,
this.anInt,
this.aDouble,
this.aListOfStrings,
this.aListOfInts,
this.aListOfDoubles,
});
final String aString;
final int anInt;
final double aDouble;
final List<String> aListOfStrings;
final List<int> aListOfInts;
final List<double> aListOfDoubles;
factory SerializableSimpleObject.fromJson(Map<String, dynamic> json) =>
_$SerializableSimpleObjectFromJson(json);
}

View File

@@ -0,0 +1,42 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'serializable_simple_object.dart';
// **************************************************************************
// Generator: JsonSerializableGenerator
// **************************************************************************
SerializableSimpleObject _$SerializableSimpleObjectFromJson(
Map<String, dynamic> json) =>
new SerializableSimpleObject(
aString: json['aString'] as String,
anInt: json['anInt'] as int,
aDouble: (json['aDouble'] as num)?.toDouble(),
aListOfStrings:
(json['aListOfStrings'] as List)?.map((e) => e as String)?.toList(),
aListOfInts:
(json['aListOfInts'] as List)?.map((e) => e as int)?.toList(),
aListOfDoubles: (json['aListOfDoubles'] as List)
?.map((e) => (e as num)?.toDouble())
?.toList());
abstract class _$SerializableSimpleObjectSerializerMixin {
String get aString;
int get anInt;
double get aDouble;
List<String> get aListOfStrings;
List<int> get aListOfInts;
List<double> get aListOfDoubles;
Map<String, dynamic> toJson() => <String, dynamic>{
'aString': aString,
'anInt': anInt,
'aDouble': aDouble,
'aListOfStrings': aListOfStrings,
'aListOfInts': aListOfInts,
'aListOfDoubles': aListOfDoubles
};
}

View File

@@ -0,0 +1,267 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
abstract class JsonStrings {
static final String listOfInts = '[1, 2, 3]';
static final String listOfDoubles = '[1.0, 2.0, 3.0]';
static final String listOfStrings = '["one", "two", "three"]';
static final String listOfDynamics = '[1, "two", 3.0]';
static final String mapOfDynamics = '''
{
"anInt": 1,
"aString": "Blah, blah, blah.",
"aDouble": 1.0
}''';
static final String listOfSimpleObjects = '''
[
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": [],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": []
}
]
''';
static final List<String> simpleObjects = [
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": [],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": []
}''',
];
static final List<String> complexObjects = [
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"anObject": {
"anInt": 1,
"aString": "Blah, blah, blah.",
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0],
"aListOfObjects": [
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 2,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
{
"aString": "Blah, blah, blah.",
"anInt": 3,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}
]
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"anObject": {
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0],
"aListOfObjects": []
}''',
'''
{
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"anObject": {
"aString": "Blah, blah, blah.",
"anInt": 1,
"aDouble": 1.0,
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
},
"aListOfStrings": ["one", "two", "three"],
"aListOfInts": [1, 2, 3],
"aListOfDoubles": [1.0, 2.0, 3.0]
}''',
];
}

69
jsonexample/lib/main.dart Normal file
View File

@@ -0,0 +1,69 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:jsonexample/tab_pages.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 10,
child: Scaffold(
appBar: AppBar(
title: const Text('Let\'s parse some JSON'),
bottom: const TabBar(
isScrollable: true,
tabs: [
Tab(text: 'Basics'),
Tab(text: 'Conv. Simple'),
Tab(text: 'Conv. Complex'),
Tab(text: 'Conv. List'),
Tab(text: 'Ser. Simple'),
Tab(text: 'Ser. Complex'),
Tab(text: 'Ser. List'),
Tab(text: 'Built Simple'),
Tab(text: 'Built Complex'),
Tab(text: 'Built List'),
],
),
),
body: new SafeArea(
bottom: false,
child: TabBarView(
children: [
BasicsPage(),
ConvertedSimplePage(),
ConvertedComplexPage(),
ConvertedListPage(),
SerializableSimplePage(),
SerializableComplexPage(),
SerializableListPage(),
BuiltSimplePage(),
BuiltComplexPage(),
BuiltListPage(),
],
),
),
),
);
}
}

View File

@@ -0,0 +1,334 @@
// Copyright 2018 The Chromium Authors. 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:convert';
import 'package:flutter/material.dart';
import 'package:jsonexample/built_value/built_complex_object.dart';
import 'package:jsonexample/built_value/built_simple_object.dart';
import 'package:jsonexample/built_value/built_value_serializers.dart';
import 'package:jsonexample/dart_convert/converted_complex_object.dart';
import 'package:jsonexample/dart_convert/converted_simple_object.dart';
import 'package:jsonexample/json_serializable/serializable_complex_object.dart';
import 'package:jsonexample/json_serializable/serializable_simple_object.dart';
import 'package:jsonexample/json_strings.dart';
import 'package:jsonexample/utils.dart';
import 'package:jsonexample/widgets.dart';
class BasicsPage extends StatelessWidget {
List<TableRow> createMapRows(
Map<String, dynamic> values, TextStyle normalStyle, TextStyle boldStyle) {
return values.keys.map((k) {
return TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text(k, style: boldStyle),
),
Text(
values[k] is String ? '"${values[k]}"' : '${values[k]}',
style: normalStyle,
),
],
);
}).toList();
}
@override
Widget build(BuildContext context) {
final localTheme = Theme.of(context).textTheme;
final boldStyle = localTheme.body1.copyWith(fontWeight: FontWeight.w600);
final dynamicListOfInts = json.decode(JsonStrings.listOfInts);
final strongListOfInts = List<int>.from(dynamicListOfInts);
final dynamicListOfStrings = json.decode(JsonStrings.listOfStrings);
final strongListOfStrings = List<String>.from(dynamicListOfStrings);
final dynamicListOfDoubles = json.decode(JsonStrings.listOfDoubles);
final strongListOfDoubles = List<double>.from(dynamicListOfDoubles);
final dynamicListOfDynamics = json.decode(JsonStrings.listOfDynamics);
final strongListOfDynamics = List<dynamic>.from(dynamicListOfDynamics);
final dynamicMapOfDynamics = json.decode(JsonStrings.mapOfDynamics);
final strongMapOfDynamics = Map<String, dynamic>.from(dynamicMapOfDynamics);
return ListView(
padding: const EdgeInsets.all(16.0),
children: [
Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(1.0),
},
children: [
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text('List of ints:', style: boldStyle),
),
Text(
prettyPrintList(strongListOfInts),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text('List of strings:', style: boldStyle),
),
Text(
prettyPrintList(strongListOfStrings),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text('List of doubles:', style: boldStyle),
),
Text(
prettyPrintList(strongListOfDoubles),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 4.0),
child: Text('List of dynamic:', style: boldStyle),
),
Text(
prettyPrintList(strongListOfDynamics),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0, bottom: 8.0),
child: Text('Map of dynamics:', style: boldStyle),
),
Container(),
],
),
],
),
Padding(
padding: const EdgeInsets.only(left: 24.0),
child: Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(1.0),
},
children: createMapRows(
strongMapOfDynamics,
localTheme.body1,
boldStyle,
),
),
),
],
);
}
}
class ConvertedSimplePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<ConvertedSimpleObject> objects = JsonStrings.simpleObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return ConvertedSimpleObject.fromJson(parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class ConvertedComplexPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<ConvertedComplexObject> objects = JsonStrings.complexObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return ConvertedComplexObject.fromJson(parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
ComplexObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class ConvertedListPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final parsedJson = json.decode(JsonStrings.listOfSimpleObjects);
final deserializedObjects =
parsedJson.map((o) => ConvertedComplexObject.fromJson(o));
final listOfObjects = deserializedObjects.toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(listOfObjects),
const SizedBox(height: 16.0),
],
);
}
}
class SerializableSimplePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<SerializableSimpleObject> objects = JsonStrings.simpleObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return SerializableSimpleObject.fromJson(parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class SerializableComplexPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<SerializableComplexObject> objects = JsonStrings.complexObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return SerializableComplexObject.fromJson(parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
ComplexObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class SerializableListPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final parsedJson = json.decode(JsonStrings.listOfSimpleObjects);
final deserializedObjects =
parsedJson.map((o) => SerializableSimpleObject.fromJson(o));
final listOfObjects = deserializedObjects.toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(listOfObjects),
const SizedBox(height: 16.0),
],
);
}
}
class BuiltSimplePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<BuiltSimpleObject> objects = JsonStrings.simpleObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return serializers.deserializeWith(
BuiltSimpleObject.serializer, parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class BuiltComplexPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
List<BuiltComplexObject> objects = JsonStrings.complexObjects.map(
(jsonString) {
final parsedJson = json.decode(jsonString);
return serializers.deserializeWith(
BuiltComplexObject.serializer, parsedJson);
},
).toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
ComplexObjectViewList(objects),
const SizedBox(height: 16.0),
],
);
}
}
class BuiltListPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final parsedJson = json.decode(JsonStrings.listOfSimpleObjects);
final deserializedObjects = parsedJson.map(
(o) => serializers.deserializeWith(BuiltComplexObject.serializer, o));
final listOfObjects = deserializedObjects.toList();
return ListView(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
children: [
const SizedBox(height: 16.0),
SimpleObjectViewList(listOfObjects),
const SizedBox(height: 16.0),
],
);
}
}

View File

@@ -0,0 +1,26 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
String prettyPrintList(Iterable iter) {
if (iter == null) return 'NULL';
final buff = StringBuffer();
var isFirst = true;
buff.write('[');
for (final val in iter) {
if (!isFirst) buff.write(', ');
isFirst = false;
if (val is String) {
buff.write('"$val"');
} else {
buff.write(val.toString());
}
}
buff.write(']');
return buff.toString();
}

View File

@@ -0,0 +1,291 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:jsonexample/utils.dart';
class SimpleObjectView extends StatelessWidget {
SimpleObjectView(dynamic obj) : simpleObject = obj;
final dynamic simpleObject;
@override
Widget build(BuildContext context) {
final localTheme = Theme.of(context).textTheme;
final boldStyle = localTheme.body1.copyWith(fontWeight: FontWeight.w600);
if (simpleObject == null) return Text('NULL', style: localTheme.body1);
return Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(1.0),
},
children: [
TableRow(
children: [
Text(
'aString:',
style: boldStyle,
),
Text(
simpleObject.aString != null
? '"${simpleObject.aString}"'
: 'NULL',
style: localTheme.body1,
),
],
),
TableRow(
children: [
Text('anInt:', style: boldStyle),
Text(
simpleObject.anInt?.toString() ?? 'NULL',
style: localTheme.body1,
),
],
),
TableRow(children: [
Text('aDouble:', style: boldStyle),
Text(
simpleObject.aDouble?.toString() ?? 'NULL',
style: localTheme.body1,
),
]),
TableRow(
children: [
Text('aListOfStrings:', style: boldStyle),
Text(
prettyPrintList(
simpleObject.aListOfStrings,
),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Text('aListOfInts:', style: boldStyle),
Text(
prettyPrintList(simpleObject.aListOfInts),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Text('aListOfDoubles:', style: boldStyle),
),
Text(
prettyPrintList(simpleObject.aListOfDoubles),
style: localTheme.body1,
),
],
),
],
);
}
}
class SimpleObjectViewList extends StatelessWidget {
SimpleObjectViewList(List<dynamic> objects) : simpleObjects = objects;
final List<dynamic> simpleObjects;
@override
Widget build(BuildContext context) {
final widgets = <Widget>[];
for (int i = 0; i < simpleObjects.length; i++) {
widgets.addAll([
Text(
'SimpleObject $i:',
style: Theme.of(context).textTheme.subhead,
),
const SizedBox(height: 4.0),
SimpleObjectView(simpleObjects[i]),
const SizedBox(height: 24.0),
]);
}
widgets.removeLast();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: widgets,
);
}
}
class ComplexObjectView extends StatelessWidget {
final dynamic complexObject;
ComplexObjectView(dynamic obj) : complexObject = obj;
List<Widget> _generateSimpleObjectWidgets(Iterable<dynamic> simpleObjects) {
if (simpleObjects == null) {
return [
const Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text('NULL'),
),
];
}
if (simpleObjects.length == 0) {
return [
const Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Text('[]'),
),
];
}
return simpleObjects
.expand((o) => [
const SizedBox(height: 4.0),
SimpleObjectView(o),
const SizedBox(height: 4.0),
])
.toList();
}
@override
Widget build(BuildContext context) {
final localTheme = Theme.of(context).textTheme;
final boldStyle = localTheme.body1.copyWith(fontWeight: FontWeight.w600);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(1.0),
},
children: [
TableRow(
children: [
Text('aString:', style: boldStyle),
Text(
complexObject.aString != null
? '"${complexObject.aString}"'
: 'NULL',
style: localTheme.body1),
],
),
TableRow(
children: [
Text('anInt:', style: boldStyle),
Text(complexObject.anInt?.toString() ?? 'NULL',
style: localTheme.body1),
],
),
TableRow(
children: [
Text('aDouble:', style: boldStyle),
Text(complexObject.aDouble?.toString() ?? 'NULL',
style: localTheme.body1),
],
),
TableRow(
children: [
Text('anObject:', style: boldStyle),
Container(),
],
),
],
),
Padding(
padding: const EdgeInsets.fromLTRB(24.0, 4.0, 0.0, 4.0),
child: SimpleObjectView(complexObject.anObject),
),
Table(
columnWidths: const {
0: IntrinsicColumnWidth(),
1: FlexColumnWidth(1.0),
},
children: [
TableRow(
children: [
Text('aListOfStrings:', style: boldStyle),
Text(
prettyPrintList(complexObject.aListOfStrings),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Text('aListOfInts:', style: boldStyle),
Text(
prettyPrintList(complexObject.aListOfInts),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Padding(
padding: const EdgeInsets.only(right: 8.0),
child: Text('aListOfDoubles:', style: boldStyle),
),
Text(
prettyPrintList(complexObject.aListOfDoubles),
style: localTheme.body1,
),
],
),
TableRow(
children: [
Text('aListOfObjects:', style: boldStyle),
Container()
],
),
],
),
Padding(
padding: const EdgeInsets.only(left: 24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children:
_generateSimpleObjectWidgets(complexObject.aListOfObjects),
),
),
],
);
}
}
class ComplexObjectViewList extends StatelessWidget {
ComplexObjectViewList(List<dynamic> objects) : complexObjects = objects;
final List<dynamic> complexObjects;
@override
Widget build(BuildContext context) {
final widgets = <Widget>[];
for (int i = 0; i < complexObjects.length; i++) {
widgets.addAll([
Text(
'Complex Object $i:',
style: Theme.of(context).textTheme.subhead,
),
const SizedBox(height: 4.0),
ComplexObjectView(complexObjects[i]),
const SizedBox(height: 24.0),
]);
}
widgets.removeLast();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: widgets,
);
}
}