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

套件詳情v0.2.1

rfc_6901

JSON 指針(RFC 6901)。讀取/寫入 JSON 文件中的參考值。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
89.8萬
喜歡數
32
Pub 分數
160

安裝套件

flutter pub add rfc_6901

套件介紹

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

rfc_6901 套件為 Dart 與 Flutter 實作 JSON 指標(RFC 6901),支援安全導航與操作 JSON 文件。它允許使用指標語法(例如 /foo/0/bar)讀取與寫入巢狀 JSON 結構中的值。這在 API、設定檔或狀態管理中處理動態資料時特別實用,可在不解析整個結構的情況下精確存取 JSON 欄位。此套件支援所有主要平台,包含行動裝置、Web 與桌面。

適用情境

  • 動態訪問嵌套的 JSON 值
  • 更新 JSON 文件中的特定欄位
  • 解析具有可變結構的 API 回應
  • 在狀態管理中實現部分更新
  • 處理使用嵌套鍵的設定檔

主要特色

  • 支援 RFC 6901 JSON 指針標準
  • 安全的值取得與指派
  • 統一處理陣列和物件
  • 跨平台相容性
  • 最小依賴足跡

適合對象

  • 處理動態 JSON 數據的開發人員
  • 需要選擇性欄位存取的 API 客戶端
  • 需要補丁操作的狀態管理系統
  • 具有深層嵌套的配置解析器

使用建議

  • 需要有效的 JSON 指針語法
  • 執行時不會驗證目標路徑
  • 限制為對現有 JSON 的讀/寫操作
  • 不適用於複雜的 JSON 轉換

原始 README

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

RFC 6901 JSON Pointer

JSON Pointer (RFC 6901) implementation in Dart.

Features:

  • Reads, writes, removes values
  • Fully implements the standard, including the - character
  • Can build the entire path, e.g. /foo/bar/-/baz
  • No external dependencies
  • 100% test coverage

Constructing pointers

A JSON Pointer object can be parsed from a string expression. The expression MUST be a valid non-empty JSON Pointer expression with "/" and "~" escaped.

final pointer = JsonPointer('/foo/0/bar'); // a valid non-empty pointer
final alsoPointer = JsonPointer('/foo/-'); // a valid non-empty pointer with a special "-" reference
final emptyPointer = JsonPointer(); // an empty pointer

JsonPointer('oops'); // throws a FormatException: expression doesn't start with "/"
JsonPointer('/foo/~'); // throws a FormatException: unescaped "~"

It can also be built from individual reference tokens. In such case, it will escape the tokens automatically.

final pointer = JsonPointer.build(['foo', '0', 'bar/']); // Makes "/foo/0/bar~1"
final longerPointer = JsonPointerSegment('-', pointer); // Makes "/foo/0/bar~1/-"
final evenLongerPointer = JsonPointerSegment('wow', longerPointer); // Makes "/foo/0/bar~1/-/wow"

Reading and writing values

The read() method returns the referenced value. The write() method returns a copy of the document with the referenced value replaced by the new value.

import 'dart:convert';

import 'package:rfc_6901/rfc_6901.dart';

void main() {
  const json = '{"foo": [{"bar": 42}]}';
  final document = jsonDecode(json);
  print('Original document: $document');

  [
    '/foo', // reads the array
    '/foo/0', // reads the first element of the array
    '/foo/0/bar', // reads 42
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    print('Pointer "$pointer" reads ${pointer.read(document)}');
  });

  [
    '/foo/0/bar', // {foo: [{bar: banana}]}
    '/foo/-', // {foo: [{bar: 42}, banana]}
    '/a/b/-/c/d', // {foo: [{bar: 42}], a: {b: [{c: {d: banana}}]}}
  ].forEach((expression) {
    final pointer = JsonPointer(expression);
    final d = pointer.write(document, 'banana');
    print('Add a banana at "$pointer": $d');
  });
}

所屬分類

原始碼儲存庫

使用此套件的專案