Skip to content

Commit 36d03c2

Browse files
authored
Use generator for json serialization (#911)
1 parent 8aad9b4 commit 36d03c2

14 files changed

+570
-114
lines changed

.changes/json-serializable

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
patch type="changed" "Migrate manual json serialization to json_serializable code generation"

build.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2024 LiveKit, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
targets:
16+
$default:
17+
sources:
18+
exclude:
19+
- example/**
20+
builders:
21+
json_serializable:
22+
options:
23+
include_if_null: false
24+
explicit_to_json: true

lib/src/token_source/caching.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
import 'dart:async';
1616

17+
import 'package:json_annotation/json_annotation.dart';
18+
1719
import '../support/reusable_completer.dart';
1820
import 'jwt.dart';
1921
import 'token_source.dart';
2022

23+
part 'caching.g.dart';
24+
2125
/// A validator function that determines if cached credentials are still valid.
2226
///
2327
/// The validator receives the original request options and cached response, and should
@@ -27,6 +31,7 @@ import 'token_source.dart';
2731
typedef TokenValidator = bool Function(TokenRequestOptions options, TokenSourceResponse response);
2832

2933
/// A tuple containing the request options and response that were cached.
34+
@JsonSerializable()
3035
class TokenStoreItem {
3136
final TokenRequestOptions options;
3237
final TokenSourceResponse response;
@@ -35,6 +40,9 @@ class TokenStoreItem {
3540
required this.options,
3641
required this.response,
3742
});
43+
44+
factory TokenStoreItem.fromJson(Map<String, dynamic> json) => _$TokenStoreItemFromJson(json);
45+
Map<String, dynamic> toJson() => _$TokenStoreItemToJson(this);
3846
}
3947

4048
/// Protocol for storing and retrieving cached token credentials.

lib/src/token_source/caching.g.dart

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/token_source/jwt.dart

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
// limitations under the License.
1414

1515
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
16+
import 'package:json_annotation/json_annotation.dart';
1617

1718
import 'token_source.dart';
1819

20+
part 'jwt.g.dart';
21+
1922
/// Parsed payload for a LiveKit-issued JWT.
2023
class LiveKitJwtPayload {
2124
LiveKitJwtPayload._(this._claims);
@@ -114,17 +117,37 @@ class LiveKitJwtPayload {
114117
}
115118

116119
/// LiveKit-specific video grants embedded within a JWT.
120+
@JsonSerializable()
117121
class LiveKitVideoGrant {
118122
final String? room;
123+
124+
@JsonKey(name: 'room_create')
119125
final bool? roomCreate;
126+
127+
@JsonKey(name: 'room_join')
120128
final bool? roomJoin;
129+
130+
@JsonKey(name: 'room_list')
121131
final bool? roomList;
132+
133+
@JsonKey(name: 'room_record')
122134
final bool? roomRecord;
135+
136+
@JsonKey(name: 'room_admin')
123137
final bool? roomAdmin;
138+
139+
@JsonKey(name: 'can_publish')
124140
final bool? canPublish;
141+
142+
@JsonKey(name: 'can_subscribe')
125143
final bool? canSubscribe;
144+
145+
@JsonKey(name: 'can_publish_data')
126146
final bool? canPublishData;
147+
148+
@JsonKey(name: 'can_publish_sources')
127149
final List<String>? canPublishSources;
150+
128151
final bool? hidden;
129152
final bool? recorder;
130153

@@ -143,20 +166,8 @@ class LiveKitVideoGrant {
143166
this.recorder,
144167
});
145168

146-
factory LiveKitVideoGrant.fromJson(Map<String, dynamic> json) => LiveKitVideoGrant(
147-
room: json['room'] as String?,
148-
roomCreate: json['room_create'] as bool?,
149-
roomJoin: json['room_join'] as bool?,
150-
roomList: json['room_list'] as bool?,
151-
roomRecord: json['room_record'] as bool?,
152-
roomAdmin: json['room_admin'] as bool?,
153-
canPublish: json['can_publish'] as bool?,
154-
canSubscribe: json['can_subscribe'] as bool?,
155-
canPublishData: json['can_publish_data'] as bool?,
156-
canPublishSources: (json['can_publish_sources'] as List?)?.map((dynamic item) => item.toString()).toList(),
157-
hidden: json['hidden'] as bool?,
158-
recorder: json['recorder'] as bool?,
159-
);
169+
factory LiveKitVideoGrant.fromJson(Map<String, dynamic> json) => _$LiveKitVideoGrantFromJson(json);
170+
Map<String, dynamic> toJson() => _$LiveKitVideoGrantToJson(this);
160171
}
161172

162173
extension TokenSourceJwt on TokenSourceResponse {

lib/src/token_source/jwt.g.dart

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/token_source/room_configuration.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import 'package:json_annotation/json_annotation.dart';
16+
17+
part 'room_configuration.g.dart';
18+
1519
/// Configuration for dispatching an agent to a room.
20+
@JsonSerializable()
1621
class RoomAgentDispatch {
1722
/// Name of the agent to dispatch.
23+
@JsonKey(name: 'agent_name')
1824
final String? agentName;
1925

2026
/// Metadata for the agent.
@@ -25,40 +31,45 @@ class RoomAgentDispatch {
2531
this.metadata,
2632
});
2733

28-
Map<String, dynamic> toJson() => {
29-
if (agentName != null) 'agent_name': agentName,
30-
if (metadata != null) 'metadata': metadata,
31-
};
34+
factory RoomAgentDispatch.fromJson(Map<String, dynamic> json) => _$RoomAgentDispatchFromJson(json);
35+
Map<String, dynamic> toJson() => _$RoomAgentDispatchToJson(this);
3236
}
3337

3438
/// Configuration for a LiveKit room.
3539
///
3640
/// This class contains various settings that control room behavior such as timeouts,
3741
/// participant limits, and agent dispatching.
42+
@JsonSerializable()
3843
class RoomConfiguration {
3944
/// Room name, used as ID, must be unique.
4045
final String? name;
4146

4247
/// Number of seconds to keep the room open if no one joins.
48+
@JsonKey(name: 'empty_timeout')
4349
final int? emptyTimeout;
4450

4551
/// Number of seconds to keep the room open after everyone leaves.
52+
@JsonKey(name: 'departure_timeout')
4653
final int? departureTimeout;
4754

4855
/// Limit number of participants that can be in a room, excluding Egress and Ingress participants.
56+
@JsonKey(name: 'max_participants')
4957
final int? maxParticipants;
5058

5159
/// Metadata of room.
5260
final String? metadata;
5361

5462
/// Minimum playout delay of subscriber.
63+
@JsonKey(name: 'min_playout_delay')
5564
final int? minPlayoutDelay;
5665

5766
/// Maximum playout delay of subscriber.
67+
@JsonKey(name: 'max_playout_delay')
5868
final int? maxPlayoutDelay;
5969

6070
/// Improves A/V sync when playout delay set to a value larger than 200ms.
6171
/// It will disable transceiver re-use so not recommended for rooms with frequent subscription changes.
72+
@JsonKey(name: 'sync_streams')
6273
final bool? syncStreams;
6374

6475
/// Define agents that should be dispatched to this room.
@@ -76,15 +87,6 @@ class RoomConfiguration {
7687
this.agents,
7788
});
7889

79-
Map<String, dynamic> toJson() => {
80-
if (name != null) 'name': name,
81-
if (emptyTimeout != null) 'empty_timeout': emptyTimeout,
82-
if (departureTimeout != null) 'departure_timeout': departureTimeout,
83-
if (maxParticipants != null) 'max_participants': maxParticipants,
84-
if (metadata != null) 'metadata': metadata,
85-
if (minPlayoutDelay != null) 'min_playout_delay': minPlayoutDelay,
86-
if (maxPlayoutDelay != null) 'max_playout_delay': maxPlayoutDelay,
87-
if (syncStreams != null) 'sync_streams': syncStreams,
88-
if (agents != null) 'agents': agents!.map((a) => a.toJson()).toList(),
89-
};
90+
factory RoomConfiguration.fromJson(Map<String, dynamic> json) => _$RoomConfigurationFromJson(json);
91+
Map<String, dynamic> toJson() => _$RoomConfigurationToJson(this);
9092
}

lib/src/token_source/room_configuration.g.dart

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)