搜索插件、代码仓与 APP 分类

GitHub 代码仓f3ath/rfc-6901-dart

rfc-6901-dart

Dart 中的 JSON 指针 (RFC 6901) 实现

rfc-6901-dart 项目封面
星标
4
复刻
2
最近推送(UTC)
2025年10月17日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

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');
  });
}
Alexey GitHub avatar
GitHub 作者

Alexey ↗

If I had ever learnt, I should have been a great proficient.

Salesforce Silicon Valley, CA
语言Dart

此仓库发布的插件

使用的依赖

依赖清单 3 项

  • lints开发依赖^6.0.0
  • test开发依赖^1.20.1
  • check_coverage开发依赖^0.0.4

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗