パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細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');
  });
}

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト