搜尋套件、儲存庫與 APP 分類

套件詳情v0.4.0+1

crimson

快速、高效且易用的 Dart JSON 解析器與序列化器。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
72
喜歡數
128
Pub 分數
140

安裝套件

flutter pub add crimson

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Crimson 是一個高效率的 Dart JSON 解析器與序列化工具,專為速度與簡潔性設計。它能以最少的樣板程式碼,高效地在 JSON 資料與 Dart 物件之間轉換。Crimson 以效能為核心建構,支援所有主要平台,包含 Android、iOS、Web、macOS、Windows 與 Linux。Crimson 提供乾淨直覺的 API,讓處理 JSON 變得簡單明確,非常適合需要快速資料處理的應用程式。其輕量級特性確保低額外負荷,適用於小型專案與大型應用。

適用情境

  • 解析 API 的 JSON 回應
  • 將 Dart 物件序列化為 JSON
  • 高效能資料處理
  • 跨平台應用開發
  • 即時資料同步
  • 輕量級資料儲存

主要特色

  • 超快速解析與序列化
  • 最小化樣板程式碼
  • 支援所有主要平台
  • 簡單直覺的 API
  • 低記憶體佔用
  • 內建類型安全性

適合對象

  • 效能關鍵的應用程式
  • 行動與網頁應用程式
  • API 客戶端
  • 資料密集型服務
  • 尋求快速 JSON 處理的開發者
  • 注重效率的專案

使用建議

  • 需要顯式定義模型
  • 有限的內建驗證
  • 不適用於需要自訂的複雜巢狀結構
  • 高級用例可能需要額外的設定
  • 最適合結構良好的 JSON 數據

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Fast, efficient and easy-to-use JSON parser and serializer for Dart.
Crimson does not verify your JSON!

Features

  • 🏎️ Fast: Like really fast. Crimson parses JSON in a single pass.
  • 🌻 Easy to use: Crimson is designed to be easy to learn and use.
  • 💃 Flexible: Crimson can partially parse and serialize JSON.
  • 🥶 Freezed support: Crimson supports freezed classes.
  • 🪶 Lightweight: Crimson is lightweight and has no third-party dependencies.

Usage

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

import 'package:crimson/crimson.dart';

part 'tweet.g.dart';

@json
class Tweet {
  DateTime? createdAt;

  String? tweet;

  int? favoriteCount;
}

Now you just need to run pub run build_runner build to generate the necessary code.

import 'package:crimson/crimson.dart';

void main() {
  final jsonBytes = downloadTweets();

  final crimson = Crimson(jsonBytes);
  final tweets = crimson.readTweetList();

  final writer = CrimsonWriter();
  writer.writeTweetList(tweets);
  final bytes = writer.toBytes();
}

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

Ignoring JSON fields

Annotate properties with @jsonIgnore to ignore them:

@json
class Tweet {
  DateTime? created_at;

  String? tweet;

  @jsonIgnore
  int? favoriteCount;
}

Renaming JSON fields

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

@json
class Tweet {
  @JsonName('created_at')
  DateTime? createdAt;

  @JsonName('text', aliases: {'alias1', 'alias2'})
  String? tweet;
}

The same works for enum values:

@json
enum TweetType {
  tweet,

  @JsonName('re-tweet')
  retweet,
}

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

@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
}

fromJson and toJson methods

Crimson supports generating a fromJson() constructor and a toJson() extension method for your classes use Uint8List.

Just add the following line to your class. The .toJson() method is generated using an extension.

@json
class Tweet {
  // ...

  factory Tweet.fromJson(Uint8List json) => _$TweetFromJson(json);
}

Alternatively you can use fromBytes() and toBytes(). It's equivalent to fromJson() and toJson().

@json
class Tweet {
  // ...

  factory Tweet.fromBytes(Uint8List bytes) => _$TweetFromBytes(bytes);
}

JSON Pointers

Crimson supports RFC 6901 JSON pointers to access nested JSON fields:

{
  "user": {
    "name": "Simon Choi"
  },
  "tweets": [
    {
      "text": "Hello World!"
    },
    {
      "text": "Hello Crimson!"
    }
  ]
}

We can access the name field of the user object and the text field of the second tweet by providing a JSON pointer:

@json
class Tweet {
  @JsonName('/user/name')
  String? userName;

  @JsonName('/tweets/1/text')
  String? secondTweet;
}

JSON pointers are evaluated at compile time and optimized code is generated so there is no runtime overhead.

Fields using JSON pointers are ignored for serialization.

Important: You can only use a pointer prefix once in a class. For example, you can't use /user and /user/name in the same class.

Custom JSON converters

You can use custom JSON converters to convert between JSON and Dart types using the @JsonConvert() annotation:

@json
class Tweet {
  String? tweet;

  @JsonConvert(fromJson: jsonIntToBool, toJson: boolToJsonInt)
  bool? isFavorite;
}

bool jsonIntToBool(int json) => json >= 1;

int boolToJsonInt(bool value) => value ? 1 : 0;

Freezed Support

Crimson supports classes annotated with @freezed from the freezed package.

import 'package:crimson/crimson.dart';

part 'tweet.g.dart';
part 'tweet.freezed.dart';

@freezed
class Tweet with _$Tweet {
  @json
  const factory Tweet({
    DateTime? created_at,
    @JsonField(name: 'text') String? tweet,
    int? reply_count,
    int? retweet_count,
    int? favorite_count,
  }) = _Tweet;
}

JSON Benchmarks

The following benchmarks give you an idea about the JSON parsing performance of Crimson. The benchmarks were run on a MacBook with M1 Pro and 32 GB of RAM using Twitter API data.

Big JSON without whitespace Big JSON with whitespace
Medium JSON without whitespace Small JSON without whitespace
License
Copyright 2023 Simon Choi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。