Skip to content

Commit

Permalink
0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
simc committed Dec 29, 2022
1 parent bc6f144 commit 4ca1362
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 104 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.0.5

- Changed annotations again (sorry! this is the last time)
- Added `@JsonName()`, `@jsonIgnore`, `@jsonKebabCase` and `@jsonSnakeCase` annotations annotations
- Added `JsonConverter` interface to allow custom parsing and serialization

# 0.0.4

- Replaced `@json` and `@JsonEnum()` with `@Json()`
Expand Down
106 changes: 96 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,20 @@

## Usage

After adding Crimson to your `pubspec.yaml`, you can start annotating your classes with `@Json()` and optionally `@JsonField()`:
After adding Crimson to your `pubspec.yaml`, you can start annotating your classes with `@json` and optionally `@JsonField()`:

```dart
import 'package:crimson/crimson.dart';
part 'tweet.g.dart';
@Json()
@json
class Tweet {
DateTime? created_at;
DateTime? createdAt;
@JsonField(name: 'text')
String? tweet;
int? reply_count;
int? retweet_count;
int? favorite_count;
int? favoriteCount;
}
```

Expand All @@ -70,6 +65,97 @@ void main() {

That's it! You can now parse and serialize JSON with ease.

### Ignoring fields

Annotate properties with `@jsonIgnore` to ignore them:

```dart
@json
class Tweet {
DateTime? created_at;
String? tweet;
@jsonIgnore
int? favoriteCount;
}
```

### Renaming fields

Use the `@JsonName()` annotation to rename individual fields:

```dart
@json
class Tweet {
@JsonName('created_at')
DateTime? createdAt;
@JsonName('text', aliases: {'alias1', 'alias2'})
String? tweet;
}
```

The same works for enum values:

```dart
@json
enum TweetType {
tweet,
@JsonName('re-tweet')
retweet,
}
```

If you want to rename all fields or enum values, you can use `@jsonKebabCase` and `@jsonSnakeCase`:

```dart
@jsonKebabCase
class Tweet {
DateTime? createdAt; // created-at
String? tweet; // tweet
int? favoriteCount; // favorite-count
}
@jsonSnakeCase
enum PlaceType {
country, // country
largeCity, // large_city
smallCity, // small_city
}
```

### Custom converters

You can use custom converters to convert between JSON and Dart types. Just create a class that implements `JsonConverter<T>`.

```dart
class IntToBoolConverter extends JsonConverter<bool> {
const IntToBoolConverter();
@override
bool fromJson(dynamic json) => json == 1;
@override
dynamic toJson(bool value) => value ? 1 : 0;
}
```

Then you can annotate your properties with the new converter:

```dart
@json
class Tweet {
String? tweet;
@IntToBoolConverter()
bool? isFavorite;
}
```

## Freezed Support

Crimson supports classes annotated with `@freezed` from the [freezed](https://pub.dev/packages/freezed) package.
Expand All @@ -82,7 +168,7 @@ part 'tweet.freezed.dart';
@freezed
class Tweet with _$Tweet {
@Json()
@json
const factory Tweet({
DateTime? created_at,
@JsonField(name: 'text') String? tweet,
Expand Down
4 changes: 2 additions & 2 deletions crimson_test/lib/bench.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dart:convert';
import 'dart:convert' as convert;
import 'dart:io';

import 'package:crimson/crimson.dart';
Expand All @@ -14,7 +14,7 @@ void main() {
s.stop();

final s2 = Stopwatch()..start();
final list = json.fuse(utf8).decode(bytes) as List;
final list = convert.json.fuse(convert.utf8).decode(bytes) as List;
list.map((e) => Tweet.fromJson(e as Map<String, dynamic>)).toList();
s2.stop();

Expand Down
16 changes: 8 additions & 8 deletions crimson_test/lib/twitter/entities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'util.dart';
part 'entities.g.dart';

@JsonSerializable(createToJson: false)
@Json()
@json
class Entities {
Entities();

Expand All @@ -28,7 +28,7 @@ class Entities {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Hashtag {
Hashtag();

Expand All @@ -41,7 +41,7 @@ class Hashtag {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Poll {
Poll();

Expand All @@ -50,14 +50,14 @@ class Poll {
List<Option>? options;

@JsonKey(fromJson: convertTwitterDateTime)
@JsonField(fromJson: convertTwitterDateTime)
@TwitterDateConverter()
DateTime? end_datetime;

String? duration_minutes;
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Option {
Option();

Expand All @@ -69,7 +69,7 @@ class Option {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Symbol {
Symbol();

Expand All @@ -81,7 +81,7 @@ class Symbol {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Url {
Url();

Expand All @@ -97,7 +97,7 @@ class Url {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class UserMention {
UserMention();

Expand Down
6 changes: 3 additions & 3 deletions crimson_test/lib/twitter/geo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:json_annotation/json_annotation.dart';
part 'geo.g.dart';

@JsonSerializable(createToJson: false)
@Json()
@json
class Place {
Place();

Expand All @@ -25,7 +25,7 @@ class Place {
String? country;
}

@Json()
@json
enum PlaceType {
admin,
country,
Expand All @@ -35,7 +35,7 @@ enum PlaceType {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Coordinates {
Coordinates();

Expand Down
12 changes: 6 additions & 6 deletions crimson_test/lib/twitter/media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:json_annotation/json_annotation.dart';
part 'media.g.dart';

@JsonSerializable(createToJson: false)
@Json()
@json
class Media {
Media();

Expand Down Expand Up @@ -36,7 +36,7 @@ class Media {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Sizes {
Sizes();

Expand All @@ -52,7 +52,7 @@ class Sizes {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Size {
Size();

Expand All @@ -66,7 +66,7 @@ class Size {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class AdditionalMediaInfo {
AdditionalMediaInfo();

Expand All @@ -83,7 +83,7 @@ class AdditionalMediaInfo {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class VideoInfo {
VideoInfo();

Expand All @@ -98,7 +98,7 @@ class VideoInfo {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class Variant {
Variant();

Expand Down
8 changes: 4 additions & 4 deletions crimson_test/lib/twitter/tweet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import 'util.dart';
part 'tweet.g.dart';

@JsonSerializable(createToJson: false)
@Json()
@json
class Tweet {
Tweet();

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

@JsonKey(fromJson: convertTwitterDateTime)
@JsonField(fromJson: convertTwitterDateTime)
@TwitterDateConverter()
DateTime? created_at;

String? id_str;
Expand Down Expand Up @@ -79,7 +79,7 @@ class Tweet {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class CurrentUserRetweet {
CurrentUserRetweet();

Expand All @@ -90,7 +90,7 @@ class CurrentUserRetweet {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class QuotedStatusPermalink {
QuotedStatusPermalink();

Expand Down
8 changes: 4 additions & 4 deletions crimson_test/lib/twitter/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'util.dart';
part 'user.g.dart';

@JsonSerializable(createToJson: false)
@Json()
@json
class User {
User();

Expand Down Expand Up @@ -42,7 +42,7 @@ class User {
int? statuses_count;

@JsonKey(fromJson: convertTwitterDateTime)
@JsonField(fromJson: convertTwitterDateTime)
@TwitterDateConverter()
DateTime? createt_at;

String? profile_banner_url;
Expand All @@ -59,7 +59,7 @@ class User {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class UserEntities {
UserEntities();

Expand All @@ -72,7 +72,7 @@ class UserEntities {
}

@JsonSerializable(createToJson: false)
@Json()
@json
class UserEntityUrl {
UserEntityUrl();

Expand Down
Loading

0 comments on commit 4ca1362

Please sign in to comment.