1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 22:09:06 +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,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
};
}