Skip to content

DateTime type and nested member not decoded correctly. #258

Closed
@jaggerwang

Description

@jaggerwang

Problem

code

import 'package:meta/meta.dart';
import 'package:json_annotation/json_annotation.dart';

part 'tumblr_post.g.dart';

@JsonSerializable()
@immutable
class TumblrPostEntity extends Object with _$TumblrPostEntitySerializerMixin {
  final int id;
  final String url;
  @JsonKey(name: 'short_url')
  final String shortUrl;
  final String type;
  final String format;
  final List<String> tags;
  @JsonKey(name: 'reblog_key')
  final String reblogKey;
  final _TumblrPostSource source;
  @JsonKey(name: 'is_liked')
  final bool isLiked;
  final String creator;
  @JsonKey(name: 'post_at')
  final DateTime postAt;

  TumblrPostEntity({
    this.id = 0,
    this.url = '',
    this.shortUrl = '',
    this.type = '',
    this.format = '',
    this.tags = const [],
    this.reblogKey = '',
    source,
    this.isLiked = false,
    this.creator = '',
    postAt,
  })  : this.source = source ?? _TumblrPostSource(),
        this.postAt = postAt ?? DateTime.fromMillisecondsSinceEpoch(0);

  factory TumblrPostEntity.fromJson(Map<String, dynamic> json) {
    json['source'] = _TumblrPostSource.fromJson(json['source']);
    return _$TumblrPostEntityFromJson(json);
  }

  @override
  String toString() => 'TumblrPostEntity${toJson()}';
}

@JsonSerializable()
@immutable
class _TumblrPostSource extends Object with _$_TumblrPostSourceSerializerMixin {
  final String title;
  final String url;

  _TumblrPostSource({
    this.title = '',
    this.url = '',
  });

  factory _TumblrPostSource.fromJson(Map<String, dynamic> json) =>
      _$_TumblrPostSourceFromJson(json);

  @override
  String toString() => '_TumblrPostSource${toJson()}';
}

generated

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'tumblr_post.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

TumblrPostEntity _$TumblrPostEntityFromJson(Map<String, dynamic> json) {
  return new TumblrPostEntity(
      id: json['id'] as int,
      url: json['url'] as String,
      shortUrl: json['short_url'] as String,
      type: json['type'] as String,
      format: json['format'] as String,
      tags: (json['tags'] as List)?.map((e) => e as String)?.toList(),
      reblogKey: json['reblog_key'] as String,
      source: json['source'],
      isLiked: json['is_liked'] as bool,
      creator: json['creator'] as String,
      postAt: json['post_at']);
}

abstract class _$TumblrPostEntitySerializerMixin {
  int get id;
  String get url;
  String get shortUrl;
  String get type;
  String get format;
  List<String> get tags;
  String get reblogKey;
  _TumblrPostSource get source;
  bool get isLiked;
  String get creator;
  DateTime get postAt;
  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'url': url,
        'short_url': shortUrl,
        'type': type,
        'format': format,
        'tags': tags,
        'reblog_key': reblogKey,
        'source': source,
        'is_liked': isLiked,
        'creator': creator,
        'post_at': postAt?.toIso8601String()
      };
}

_TumblrPostSource _$_TumblrPostSourceFromJson(Map<String, dynamic> json) {
  return new _TumblrPostSource(
      title: json['title'] as String, url: json['url'] as String);
}

abstract class _$_TumblrPostSourceSerializerMixin {
  String get title;
  String get url;
  Map<String, dynamic> toJson() =>
      <String, dynamic>{'title': title, 'url': url};
}

There are a few problems:

  1. TumblrPostEntity.postAt is a DateTime, it was decoded as postAt: json['post_at']) in generated code, this is not correct.
  2. TumblrPostEntity.source is a nested Object, it was decoded simply as source: json['source'], So I have to handle the fromJson method manually.

I'm already using the latest version of json_serializable.

pubspec.yaml

name: tangbole
description: A tumblr client app.

dependencies:
  cookie_jar: ^0.0.4
  cupertino_icons: ^0.1.2
  english_words: ^3.1.0
  dio: ^0.0.14
  flutter:
    sdk: flutter
  flutter_redux: ^0.5.1
  flutter_webview_plugin: ^0.1.6
  json_annotation: ^0.2.9
  logging: ^0.11.3+1
  meta: ^1.1.5
  path_provider: ^0.4.1
  uri: ^0.11.2
  redux: ^3.0.0
  redux_logging: ^0.3.0
  redux_persist: ^0.7.0-rc.2
  redux_persist_flutter: ^0.6.0-rc.1
  validate: ^1.6.0

dev_dependencies:
  build_runner: ^0.9.1
  flutter_driver:
    sdk: flutter
  flutter_test:
    sdk: flutter
  json_serializable: ^0.5.8
  mockito: ^2.2.3

dependency_overrides:
  analyzer: ^0.31.2-alpha.2

flutter:
  uses-material-design: true
  assets:
    - images/lake.jpg

Notice To solve the dependency conflict, I added the dependency_overrides block.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions