diff --git a/lib/src/geojson.dart b/lib/src/geojson.dart index 92c2bcc..6d72f69 100644 --- a/lib/src/geojson.dart +++ b/lib/src/geojson.dart @@ -434,7 +434,7 @@ abstract class GeometryType extends GeometryObject { } /// Point, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.2 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class Point extends GeometryType { Point({BBox? bbox, required Position coordinates}) : super.withType(coordinates, GeoJSONObjectType.point, bbox: bbox); @@ -460,7 +460,7 @@ class Point extends GeometryType { } /// MultiPoint, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.3 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class MultiPoint extends GeometryType> { MultiPoint({BBox? bbox, List coordinates = const []}) : super.withType(coordinates, GeoJSONObjectType.multiPoint, bbox: bbox); @@ -485,7 +485,7 @@ class MultiPoint extends GeometryType> { } /// LineString, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.4 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class LineString extends GeometryType> { LineString({BBox? bbox, List coordinates = const []}) : super.withType(coordinates, GeoJSONObjectType.lineString, bbox: bbox); @@ -509,7 +509,7 @@ class LineString extends GeometryType> { } /// MultiLineString, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.5 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class MultiLineString extends GeometryType>> { MultiLineString({BBox? bbox, List> coordinates = const []}) : super.withType(coordinates, GeoJSONObjectType.multiLineString, @@ -538,7 +538,7 @@ class MultiLineString extends GeometryType>> { } /// Polygon, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.6 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class Polygon extends GeometryType>> { Polygon({BBox? bbox, List> coordinates = const []}) : super.withType(coordinates, GeoJSONObjectType.polygon, bbox: bbox); @@ -565,7 +565,7 @@ class Polygon extends GeometryType>> { } /// MultiPolygon, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.7 -@JsonSerializable(explicitToJson: true) +@JsonSerializable(explicitToJson: true, includeIfNull: false) class MultiPolygon extends GeometryType>>> { MultiPolygon({BBox? bbox, List>> coordinates = const []}) : super.withType(coordinates, GeoJSONObjectType.multiPolygon, bbox: bbox); @@ -592,7 +592,8 @@ class MultiPolygon extends GeometryType>>> { } /// GeometryCollection, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.8 -@JsonSerializable(explicitToJson: true, createFactory: false) +@JsonSerializable( + explicitToJson: true, createFactory: false, includeIfNull: false) class GeometryCollection extends GeometryObject { List geometries; diff --git a/lib/src/geojson.g.dart b/lib/src/geojson.g.dart index 7a07462..5e1a9d4 100644 --- a/lib/src/geojson.g.dart +++ b/lib/src/geojson.g.dart @@ -15,10 +15,19 @@ Point _$PointFromJson(Map json) => Point( (json['coordinates'] as List).map((e) => e as num).toList()), ); -Map _$PointToJson(Point instance) => { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates.toJson(), - }; +Map _$PointToJson(Point instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates.toJson(); + return val; +} MultiPoint _$MultiPointFromJson(Map json) => MultiPoint( bbox: json['bbox'] == null @@ -32,11 +41,19 @@ MultiPoint _$MultiPointFromJson(Map json) => MultiPoint( const [], ); -Map _$MultiPointToJson(MultiPoint instance) => - { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates.map((e) => e.toJson()).toList(), - }; +Map _$MultiPointToJson(MultiPoint instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates.map((e) => e.toJson()).toList(); + return val; +} LineString _$LineStringFromJson(Map json) => LineString( bbox: json['bbox'] == null @@ -50,11 +67,19 @@ LineString _$LineStringFromJson(Map json) => LineString( const [], ); -Map _$LineStringToJson(LineString instance) => - { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates.map((e) => e.toJson()).toList(), - }; +Map _$LineStringToJson(LineString instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates.map((e) => e.toJson()).toList(); + return val; +} MultiLineString _$MultiLineStringFromJson(Map json) => MultiLineString( @@ -71,13 +96,21 @@ MultiLineString _$MultiLineStringFromJson(Map json) => const [], ); -Map _$MultiLineStringToJson(MultiLineString instance) => - { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates - .map((e) => e.map((e) => e.toJson()).toList()) - .toList(), - }; +Map _$MultiLineStringToJson(MultiLineString instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates + .map((e) => e.map((e) => e.toJson()).toList()) + .toList(); + return val; +} Polygon _$PolygonFromJson(Map json) => Polygon( bbox: json['bbox'] == null @@ -93,12 +126,21 @@ Polygon _$PolygonFromJson(Map json) => Polygon( const [], ); -Map _$PolygonToJson(Polygon instance) => { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates - .map((e) => e.map((e) => e.toJson()).toList()) - .toList(), - }; +Map _$PolygonToJson(Polygon instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates + .map((e) => e.map((e) => e.toJson()).toList()) + .toList(); + return val; +} MultiPolygon _$MultiPolygonFromJson(Map json) => MultiPolygon( bbox: json['bbox'] == null @@ -116,20 +158,37 @@ MultiPolygon _$MultiPolygonFromJson(Map json) => MultiPolygon( const [], ); -Map _$MultiPolygonToJson(MultiPolygon instance) => - { - 'bbox': instance.bbox?.toJson(), - 'coordinates': instance.coordinates - .map((e) => e.map((e) => e.map((e) => e.toJson()).toList()).toList()) - .toList(), - }; - -Map _$GeometryCollectionToJson(GeometryCollection instance) => - { - 'type': _$GeoJSONObjectTypeEnumMap[instance.type]!, - 'bbox': instance.bbox?.toJson(), - 'geometries': instance.geometries.map((e) => e.toJson()).toList(), - }; +Map _$MultiPolygonToJson(MultiPolygon instance) { + final val = {}; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['coordinates'] = instance.coordinates + .map((e) => e.map((e) => e.map((e) => e.toJson()).toList()).toList()) + .toList(); + return val; +} + +Map _$GeometryCollectionToJson(GeometryCollection instance) { + final val = { + 'type': _$GeoJSONObjectTypeEnumMap[instance.type]!, + }; + + void writeNotNull(String key, dynamic value) { + if (value != null) { + val[key] = value; + } + } + + writeNotNull('bbox', instance.bbox?.toJson()); + val['geometries'] = instance.geometries.map((e) => e.toJson()).toList(); + return val; +} const _$GeoJSONObjectTypeEnumMap = { GeoJSONObjectType.point: 'Point',