title: Json serialization of nested objects dart
body: <p>I am trying to generate fromJSON and toJson functions for  my nested objects using the json_serializable library. But when I tried to use the generated functions, I got an error with deserialization of the nested object.</p>
  <p>I think I understand how to write toJSON and FromJSON functions. But I want to figure out how to work with the json_serializable library and work with the generated code.</p>
  <p>Class with models:</p>
  <pre><code>import 'package:json_annotation/json_annotation.dart';
  part 'models.g.dart';
  
  @JsonSerializable()
  class OfficesList {
    List&lt;Office&gt; offices;
    OfficesList({required this.offices});
  
    factory OfficesList.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$OfficesListFromJson(json);
  
    Map&lt;String, dynamic&gt; toJson() =&gt; _$OfficesListToJson(this);
  }
  
  @JsonSerializable()
  class Office {
    final String name;
    final String address;
    final String image;
  
    Office({required this.name, required this.address, required this.image});
  
    factory Office.fromJson(Map&lt;String, dynamic&gt; json) =&gt; _$OfficeFromJson(json);
  
    Map&lt;String, dynamic&gt; toJson() =&gt; _$OfficeToJson(this);
  }
  </code></pre>
  <p>Generated class:</p>
  <pre><code>// GENERATED CODE - DO NOT MODIFY BY HAND
  
  part of 'models.dart';
  
  // **************************************************************************
  // JsonSerializableGenerator
  // **************************************************************************
  
  OfficesList _$OfficesListFromJson(Map&lt;String, dynamic&gt; json) =&gt; OfficesList(
        offices: (json['offices'] as List&lt;dynamic&gt;)
            .map((e) =&gt; Office.fromJson(e as Map&lt;String, dynamic&gt;))
            .toList(),
      );
  
  Map&lt;String, dynamic&gt; _$OfficesListToJson(OfficesList instance) =&gt;
      &lt;String, dynamic&gt;{
        'offices': instance.offices,
      };
  
  Office _$OfficeFromJson(Map&lt;String, dynamic&gt; json) =&gt; Office(
        name: json['name'] as String,
        address: json['address'] as String,
        image: json['image'] as String,
      );
  
  Map&lt;String, dynamic&gt; _$OfficeToJson(Office instance) =&gt; &lt;String, dynamic&gt;{
        'name': instance.name,
        'address': instance.address,
        'image': instance.image,
      };
  </code></pre>
  <pre><code>void main(){
    Office of1 = Office(name: &quot;pupa&quot;, address: &quot;lupa&quot;, image: &quot;image&quot;);
    Office of2 = Office(name: &quot;lupa&quot;, address: &quot;pupa&quot;, image: &quot;image&quot;);
  
    OfficesList list1 = OfficesList(offices: [of1,of2]);
    
    OfficesList list2 = OfficesList.fromJson(list1.toJson());
  
    print(list2.toJson());
  }
  </code></pre>
  <p>The exception in these lines</p>
  <pre><code>OfficesList list2 = OfficesList.fromJson(list1.toJson())
  </code></pre>
  <pre><code>.map((e) =&gt; Office.fromJson(e as Map&lt;String, dynamic&gt;))
  </code></pre>
  <pre><code>Unhandled exception:
  type 'Office' is not a subtype of type 'Map&lt;String, dynamic&gt;' in type cast
  #0      _$OfficesListFromJson.&lt;anonymous closure&gt; (package:jsom_test/models.g.dart:11:41)
  #1      MappedListIterable.elementAt (dart:_internal/iterable.dart:413:31)
  #2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
  #3      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
  #4      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
  #5      new List.of (dart:core-patch/array_patch.dart:51:28)
  #6      ListIterable.toList (dart:_internal/iterable.dart:213:44)
  #7      _$OfficesListFromJson (package:jsom_test/models.g.dart:12:12)
  #8      new OfficesList.fromJson (package:jsom_test/models.dart:9:62)
  #9      main (package:jsom_test/json_test.dart:9:35)
  #10     _delayEntrypointInvocation.&lt;anonymous closure&gt; (dart:isolate-patch/isolate_patch.dart:297:19)
  #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
  </code></pre>