Viewing version 0.0.0-nullsafety.2. Open the latest package page.

rfc_6901

0.0.0-nullsafety.2

JSON Pointer (RFC 6901). Reads/writes referred values in JSON documents.

Download archive

Readme

RFC 6901 JSON Pointer

JSON Pointer (RFC 6901) implementation in Dart.

Features:

  • Reads and writes values
  • Fully implements the standard, including the - character
  • Can build an 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

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

It can also be built from individual reference tokens. In such case, it will escape the segments automatically. Note how bar/ gets turned into bar~1.

// Produces "/foo/0/bar~1"
final pointerFromSegments = JsonPointer.fromToken('foo', ['0', 'bar/']);

Reading and writing values

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
  ].map((expression) => JsonPointer(expression)).forEach((pointer) {
    print('Pointer "$pointer" reads ${pointer.read(document)}');
  });

  // Let's replace 42 with 'hello'
  final bar = JsonPointer('/foo/0/bar');
  bar.write(document, 'hello');
  // The document is {foo: [{bar: hello}]}
  print('Pointer "$bar" can replace 42 with "hello": $document');
  
  // Now let's add a new element to the array
  final newElement = JsonPointer('/foo/-');
  newElement.write(document, 'banana');
  // The document is {foo: [{bar: hello}, banana]}
  print('Pointer "$newElement" adds a banana: $document');

  // Now let's add an entire path to the document
  final longPath = JsonPointer('/a/b/-/c/d');
  longPath.write(document, 'wow');
  // The document is {foo: [{bar: hello}, banana], a: {b: [{c: {d: wow}}]}}
  print('Pointer "$longPath" creates a new path: $document');
}
Changelog

Unreleased

Added

  • Initial release

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'rfc_6901:0.0.0-nullsafety.2'
pubspec.yaml
name: rfc_6901
version: 0.0.0-nullsafety.2
description: JSON Pointer (RFC 6901). Reads/writes referred values in JSON documents.
homepage: "https://github.com/f3ath/rfc-6901-dart"

environment:
  sdk: '>=2.12.0-29 <3.0.0'

dev_dependencies:
  pedantic: ^1.10.0-nullsafety
  test: ^1.16.0-nullsafety
  test_coverage: ^0.5.0

Version history

VersionPublishedArchiveSHA-256
0.0.0-nullsafety.0Cached · Upstream SHA-256 matchedDownload62d0a25e…a997ad
0.0.0-nullsafety.1Cached · Upstream SHA-256 matchedDownloaddf232c0d…2ef772
0.0.0-nullsafety.2Cached · Upstream SHA-256 matchedDownload3806f1cb…4fa476
0.0.0-nullsafety.3Cached · Upstream SHA-256 matchedDownloadfe65754a…8c5980
0.0.0-nullsafety.4Cached · Upstream SHA-256 matchedDownloada7b867a5…c7c0ff
0.0.0-nullsafety.5Cached · Upstream SHA-256 matchedDownloada99e5528…ce7b30
0.0.0-nullsafety.6Cached · Upstream SHA-256 matchedDownload145f99b8…8f502d
0.0.0-nullsafety.7Cached · Upstream SHA-256 matchedDownloadc3d251bf…9fff53
0.1.0Cached · Upstream SHA-256 matchedDownload289b27ba…087159
0.1.1Cached · Upstream SHA-256 matchedDownload8d97680d…32b988
0.2.0Cached · Upstream SHA-256 matchedDownloaddf1bbfa3…416820
0.2.1Cached · Upstream SHA-256 matchedDownload6a43b185…018154

Dependencies

No direct dependencies declared.