Skip to content

Commit f267771

Browse files
committed
alpha.5
1 parent e16decc commit f267771

17 files changed

+54
-26
lines changed

lib/document.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ library document;
33

44
export 'package:json_api/src/document/error_object.dart';
55
export 'package:json_api/src/document/inbound_document.dart';
6+
export 'package:json_api/src/document/json_encodable.dart';
67
export 'package:json_api/src/document/link.dart';
78
export 'package:json_api/src/document/new_identifier.dart';
89
export 'package:json_api/src/document/new_relationship.dart';

lib/src/client/payload_codec.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dart:async';
22
import 'dart:convert';
33

4-
import 'package:json_api/src/document/document_encoder.dart';
4+
import 'package:json_api/src/document/to_json_encodable.dart';
55

66
/// Encodes/decodes JSON payload.
77
///
@@ -19,5 +19,5 @@ class PayloadCodec {
1919

2020
/// Encodes a JSON:API document into a JSON string.
2121
FutureOr<String> encode(Object document) =>
22-
jsonEncode(document, toEncodable: documentEncoder);
22+
jsonEncode(document, toEncodable: toJsonEncodable);
2323
}

lib/src/document/document_encoder.dart

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/src/document/error_object.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import 'package:json_api/document.dart';
12
import 'package:json_api/src/document/error_source.dart';
2-
import 'package:json_api/src/document/link.dart';
33

44
/// [ErrorObject] represents an error occurred on the server.
55
///
66
/// More on this: https://jsonapi.org/format/#errors
7-
class ErrorObject {
7+
class ErrorObject implements JsonEncodable {
88
/// Creates an instance of a JSON:API Error.
99
/// The [links] map may contain custom links. The about link
1010
/// passed through the [links['about']] argument takes precedence and will overwrite
@@ -43,6 +43,7 @@ class ErrorObject {
4343
/// Meta data.
4444
final meta = <String, Object?>{};
4545

46+
@override
4647
Map<String, Object> toJson() => {
4748
if (id.isNotEmpty) 'id': id,
4849
if (status.isNotEmpty) 'status': status,

lib/src/document/error_source.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'package:json_api/src/document/json_encodable.dart';
2+
13
/// An object containing references to the source of the error.
2-
class ErrorSource {
4+
class ErrorSource implements JsonEncodable {
35
const ErrorSource({this.pointer = '', this.parameter = ''});
46

57
/// A JSON Pointer [RFC6901] to the associated entity in the request document.
@@ -12,6 +14,7 @@ class ErrorSource {
1214

1315
bool get isNotEmpty => !isEmpty;
1416

17+
@override
1518
Map<String, String> toJson() => {
1619
if (parameter.isNotEmpty) 'parameter': parameter,
1720
if (pointer.isNotEmpty) 'pointer': pointer

lib/src/document/json_encodable.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
abstract interface class JsonEncodable {
2+
/// Converts the object to a JSON-encodable object.
3+
Object? toJson();
4+
}

lib/src/document/link.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import 'package:json_api/src/document/json_encodable.dart';
2+
13
/// A JSON:API link
24
/// https://jsonapi.org/format/#document-links
3-
class Link {
5+
class Link implements JsonEncodable {
46
Link(this.uri);
57

68
/// Link URL
@@ -12,6 +14,7 @@ class Link {
1214
@override
1315
String toString() => uri.toString();
1416

17+
@override
1518
Object toJson() =>
1619
meta.isEmpty ? uri.toString() : {'href': uri.toString(), 'meta': meta};
1720
}

lib/src/document/new_identifier.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import 'package:json_api/src/document/json_encodable.dart';
2+
13
/// A new Resource Identifier object, used when creating new resources on the server.
2-
sealed class NewIdentifier {
4+
sealed class NewIdentifier implements JsonEncodable {
35
/// Resource type.
46
String get type;
57

@@ -12,6 +14,7 @@ sealed class NewIdentifier {
1214
/// Identifier meta-data.
1315
Map<String, Object?> get meta;
1416

17+
@override
1518
Map<String, Object> toJson();
1619
}
1720

lib/src/document/new_relationship.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import 'dart:collection';
22

3+
import 'package:json_api/src/document/json_encodable.dart';
34
import 'package:json_api/src/document/link.dart';
45
import 'package:json_api/src/document/new_identifier.dart';
56

6-
class NewRelationship with IterableMixin<NewIdentifier> {
7+
class NewRelationship
8+
with IterableMixin<NewIdentifier>
9+
implements JsonEncodable {
710
final links = <String, Link>{};
811
final meta = <String, Object?>{};
912

13+
@override
1014
Map<String, dynamic> toJson() => {
1115
if (links.isNotEmpty) 'links': links,
1216
if (meta.isNotEmpty) 'meta': meta,

lib/src/document/new_resource.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:json_api/src/document/json_encodable.dart';
12
import 'package:json_api/src/document/new_identifier.dart';
23
import 'package:json_api/src/document/new_relationship.dart';
34
import 'package:json_api/src/document/new_to_many.dart';
@@ -8,7 +9,7 @@ import 'package:json_api/src/document/to_many.dart';
89
import 'package:json_api/src/document/to_one.dart';
910

1011
/// A set of properties for a to-be-created resource which does not have the id yet.
11-
class NewResource {
12+
class NewResource implements JsonEncodable {
1213
NewResource(this.type, {this.id, this.lid});
1314

1415
/// Resource type
@@ -33,6 +34,7 @@ class NewResource {
3334
/// See https://jsonapi.org/format/#document-resource-object-relationships
3435
final relationships = <String, NewRelationship>{};
3536

37+
@override
3638
Map<String, Object> toJson() => {
3739
'type': type,
3840
if (id != null) 'id': id!,

lib/src/document/outbound_document.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import 'package:json_api/src/document/error_object.dart';
2+
import 'package:json_api/src/document/json_encodable.dart';
23
import 'package:json_api/src/document/link.dart';
34
import 'package:json_api/src/document/new_resource.dart';
45
import 'package:json_api/src/document/resource.dart';
56
import 'package:json_api/src/document/to_many.dart';
67
import 'package:json_api/src/document/to_one.dart';
78

89
/// A sever-to-client document.
9-
class OutboundDocument {
10+
class OutboundDocument implements JsonEncodable {
1011
/// The document "meta" object.
1112
final meta = <String, Object?>{};
1213

13-
/// Returns the JSON representation.
14+
@override
1415
Map<String, Object?> toJson() => {'meta': meta};
1516
}
1617

lib/src/document/relationship.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import 'dart:collection';
22

3+
import 'package:json_api/src/document/json_encodable.dart';
34
import 'package:json_api/src/document/link.dart';
45
import 'package:json_api/src/document/new_identifier.dart';
56

6-
class Relationship with IterableMixin<Identifier> {
7+
class Relationship with IterableMixin<Identifier> implements JsonEncodable {
78
final links = <String, Link>{};
89
final meta = <String, Object?>{};
910

10-
Map<String, dynamic> toJson() => {
11+
@override
12+
Map<String, Object?> toJson() => {
1113
if (links.isNotEmpty) 'links': links,
1214
if (meta.isNotEmpty) 'meta': meta,
1315
};

lib/src/document/resource.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import 'package:json_api/src/document/json_encodable.dart';
12
import 'package:json_api/src/document/link.dart';
23
import 'package:json_api/src/document/new_identifier.dart';
34
import 'package:json_api/src/document/relationship.dart';
45
import 'package:json_api/src/document/to_many.dart';
56
import 'package:json_api/src/document/to_one.dart';
67

7-
class Resource {
8+
class Resource implements JsonEncodable {
89
Resource(this.type, this.id);
910

1011
/// Resource type.
@@ -45,6 +46,7 @@ class Resource {
4546
return null;
4647
}
4748

49+
@override
4850
Map<String, Object> toJson() => {
4951
'type': type,
5052
'id': id,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import 'package:json_api/document.dart';
2+
3+
/// A helper function to be used in `toJsonEncodable`
4+
/// parameter of `jsonEncode()`.
5+
Object? toJsonEncodable(Object? v) => switch (v) {
6+
JsonEncodable() => v.toJson(),
7+
DateTime() => v.toIso8601String(),
8+
_ => throw UnsupportedError('Cannot convert to JSON: $v'),
9+
};

lib/src/document/to_many.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ class ToMany extends Relationship {
99
final _ids = <Identifier>[];
1010

1111
@override
12-
Map<String, Object> toJson() => {
12+
Map<String, Object?> toJson() => {
1313
'data': [..._ids],
14-
...super.toJson()
14+
...super.toJson(),
1515
};
1616

1717
@override

lib/src/document/to_one.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class ToOne extends Relationship {
77
ToOne.empty() : this(null);
88

99
@override
10-
Map<String, dynamic> toJson() => {'data': identifier, ...super.toJson()};
10+
Map<String, Object?> toJson() => {'data': identifier, ...super.toJson()};
1111

1212
final Identifier? identifier;
1313

lib/src/server/response.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import 'package:http_interop/http_interop.dart';
22
import 'package:json_api/document.dart';
33
import 'package:json_api/http.dart';
4-
import 'package:json_api/src/document/document_encoder.dart';
4+
import 'package:json_api/src/document/to_json_encodable.dart';
55
import 'package:json_api/src/media_type.dart';
66

77
/// JSON:API response
88
Response response(int statusCode, {OutboundDocument? document}) => Response(
99
statusCode,
1010
document != null
11-
? Body.json(document, toEncodable: documentEncoder)
11+
? Body.json(document, toEncodable: toJsonEncodable)
1212
: Body(),
1313
Headers())
1414
..headers.addAll({

0 commit comments

Comments
 (0)