json_pointer 0.1.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new57

JSON Pointer #

A simple library for creating and using json-pointer (See rfc6901).

Examples #

  // Json Pointer Manipulation
  compose(['one', 'two']); // '/one/two'
  split('/one/two'); // ['one', 'two']
  parent('/one/two'); // '/one'
  append('/one', 'two'); // '/one/two'
  isValid('/one/two'); // true

  // Json Pointer Part manipulation
  escape('one/two'); // 'one~1two'
  unescape('one~1two'); // 'one/two'

  // Get and Set values in a json object
  var obj = [
    {'foo': 'bar'}
  ]

  get$('/0/foo', obj); // 'bar'
  set$('/0/foo', obj, 'baz'); // [{'foo': 'baz'}]
  contains('/0/foo', obj); // true

Escaping Examples #

  // Compose escapes parts
  compose(['one/two', 'three']); // '/one~1two/three'

  // Append escapes the added part
  append('/one', 'two/three'); // '/one/two~1three'

  // Split unescapes parts
  split('/one~1two/three'); // ['one/two', 'three']

Traversal #

The library is only able to traverse through objects of type Map and List. Functions will generally throw BadRouteError when attempting to traverse other types. contains will return false.

class Point {
  Point(this.x, this.y);
  int x;
  int y;
}

contains('/x', Point(0, 0)) // false
contains('', Point(0, 0)) // true, this is ok as Point is never traversed 

0.0.0 #

Initial version

0.0.1 #

Documentation and project fomatting changes

0.0.2 #

Documentation updates

0.1.0 #

Documentation updates

example/example.dart

import 'package:json_pointer/json_pointer.dart';

main() {
  
  compose(['one', 'two']); // '/one/two'
  compose(['one/two']); // '/one~1two'
  compose([]); // ''
  compose(['', '', '']); // '///'

  // Split is the opposite of compose
  split('/one/two'); // ['one', 'two']
  split('/one~1two'); // ['one/two'] 
  split(''); // []
  split('///'); // ['', '', '']

  // Other helpers
  parent('/one/two'); // '/one'
  parent('/one'); // ''
  parent(''); // null

  append('/one', 'two'); // '/one/two'
  append('/one', '~');   // '/one/~0'  append escapes the appended item 


  // Get and Set values
  get$('', [{'foo': 'bar'}]); // [{'foo': 'bar'}]
  get$('/0', [{'foo': 'bar'}]); // {'foo': 'bar'}
  get$('/0/foo', [{'foo': 'bar'}]); // 'bar'

  set$('', [{'foo': 'bar'}], 'baz'); // 'baz'
  set$('/0', [{'foo': 'bar'}], 'baz'); // ['baz']
  set$('/0/foo', [{'foo': 'bar'}], 'baz'); // [{'foo': 'baz'}]

  // Contains
  contains('', [{'foo': 'bar'}]); // true
  contains('/0', [{'foo': 'bar'}]); // true
  contains('/0/foo', [{'foo': 'bar'}]); // true

  contains('/a/5', [{'foo': 'bar'}]); // false
  contains('/1', [{'foo': 'bar'}]); // false
  contains('/0/foo/baz', [{'foo': 'bar'}]); // false

  // Escape part of a path
  escape('one/two'); // 'one~1two'
  escape('name'); // 'name'

  unescape('name'); // 'name'
  unescape('one~1two'); // 'one/two'

  // Validation
  isValid('/one/two'); // true
  isValid(''); // true
  isValid('/one~1two'); // true

  isValid(null); // false: null pointer
  isValid('/~2'); // false: '~' must be followed by either '0' or '1'
  isValid('0/foo'); // false: should start with '/'
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_pointer: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_pointer/json_pointer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
14
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Oct 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:json_pointer/json_pointer.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
Dev dependencies
pedantic ^1.7.0
test ^1.6.0