搜索插件、代码仓与 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');
  });
}

所属分类

源码仓库

使用此插件的项目