JSON generation script - PDFTron/pdftron-flutter GitHub Wiki

JSON Generation Script Guide (Dev)

Library

You could find the information in pubspec.yaml file:

dependencies:
  json_annotation: ^3.1.1

dev_dependencies:
  json_serializable: ^3.5.1
  build_runner: ^1.0.0

Guide

To see how it works, suppose there is a example.dart file under a directory:

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';

// To notify the script to generate an example.g.dart file for example.dart
part 'example.g.dart';

// JSONSerializable tag is necessary, and includeIfNull is used to eliminate keys that lead to null values
@JsonSerializable(includeIfNull: false)
class Example {
  List<String> names;
  int year;
  // Instead of using default fromJSON and toJSON for Color, it is applying customized conversion functions defined in this file
  @JsonKey(fromJson: colorFromJSON, toJson: colorToJSON)
  Color color;

  Example();

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

  Map<String, dynamic> toJson() => _$ExampleToJson(this);
}

// Customized JSON string conversion for Color
Map<String, double> colorToJSON(Color color) {
  if (color == null) {
    return null;
  }

  double red = color.red / 255;
  double green = color.green / 255;
  double blue = color.blue / 255;

  Map<String, double> colorMap = {
    'red': red,
    'green': green,
    'blue': blue,
  };

  return colorMap;
}

Color colorFromJSON(Map<String, double> colorMap) {
  int red = (colorMap['red'] * 255) as int;
  int green = (colorMap['green'] * 255) as int;
  int blue = (colorMap['blue'] * 255) as int;

  return new Color.fromRGBO(red, green, blue, 1.0);
}

Run the shell script JSONGeneration.sh in the root directory of your folder (This would scan through the repo for JSON generation requests and automatically generate .dart files for them):

sh JSONGeration.sh

Now you should be able to see a generated example.g.dart in the same directory as example.dart:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'example.dart';

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

Example _$ExampleFromJson(Map<String, dynamic> json) {
  return Example()
    ..names = (json['names'] as List)?.map((e) => e as String)?.toList()
    ..year = json['year'] as int
    ..color = colorFromJSON(json['color'] as Map<String, double>);
}

Map<String, dynamic> _$ExampleToJson(Example instance) {
  final val = <String, dynamic>{};

  void writeNotNull(String key, dynamic value) {
    if (value != null) {
      val[key] = value;
    }
  }

  writeNotNull('names', instance.names);
  writeNotNull('year', instance.year);
  writeNotNull('color', colorToJSON(instance.color));
  return val;
}

Reason

  • This proves useful in cases where there are plenty of fields in a class. It could even pick up fields from superclasses (see example).
  • No null check on native side is beneficial (less tedious), since setting includeIfNull to false guarantees the JSON to contain only non-null pairs.
⚠️ **GitHub.com Fallback** ⚠️