Giter Club home page Giter Club logo

json-pointer's Introduction

json-pointer

Build Status npm version Coverage Status

Some utilities for JSON pointers described by RFC 6901

Provides some additional stuff i needed but is not included in node-jsonpointer

Installation

node.js

$ npm install json-pointer

API

var pointer = require('json-pointer');

.get(object, pointer)

Looks up a JSON pointer in an object.

Array of reference tokens, e.g. returned by api.parse, can be passed as a pointer to .get, .set and .remove methods.

var obj = {
    example: {
        bla: 'hello'
    }
};
pointer.get(obj, '/example/bla');

.set(object, pointer, value)

Sets a new value on object at the location described by pointer.

var obj = {};
pointer.set(obj, '/example/bla', 'hello');

.remove(object, pointer)

Removes an attribute of object referenced by pointer.

var obj = {
    example: 'hello'
};
pointer.remove(obj, '/example');
// obj -> {}

.dict(object)

Creates a dictionary object (pointer -> value).

var obj = {
    hello: {bla: 'example'}
};
pointer.dict(obj);

// Returns:
// {
//    '/hello/bla': 'example'
// }

.walk(object, iterator)

Just like:

each(pointer.dict(obj), iterator);

.has(object, pointer)

Tests if an object has a value for a JSON pointer.

var obj = {
    bla: 'hello'
};

pointer.has(obj, '/bla');               // -> true
pointer.has(obj, '/non/existing');      // -> false

.escape(str)

Escapes a reference token.

pointer.escape('hello~bla');            // -> 'hello~0bla'
pointer.escape('hello/bla');            // -> 'hello~1bla'

.unescape(str)

Unescape a reference token.

pointer.unescape('hello~0bla');         // -> 'hello~bla'
pointer.unescape('hello~1bla');         // -> 'hello/bla'

.parse(str)

Converts a JSON pointer into an array of reference tokens.

pointer.parse('/hello/bla');            // -> ['hello', 'bla']

.compile(array)

Builds a json pointer from an array of reference tokens.

pointer.compile(['hello', 'bla']);      // -> '/hello/bla'

pointer(object, [pointer, [value]])

Convenience wrapper around the api.

pointer(object)                 // bind object
pointer(object, pointer)        // get
pointer(object, pointer, value) // set

The wrapper supports chainable object oriented style.

var obj = {anything: 'bla'};
var objPointer = pointer(obj);
objPointer.set('/example', 'bla').dict();

json-pointer's People

Contributors

adamgold avatar epoberezkin avatar hhomar avatar ivangoncharov avatar jauco avatar manuelstofer avatar mike-marcacci avatar mlippert avatar pigulla avatar rkusa avatar timoxley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

json-pointer's Issues

The pointer.dict function crashes when object contains an array

Hey

it seem like json-pointer can't handle arrays inside objects very well. Using the dict method, it produces an error when trying to replace array keys.

var pointer = require('json-pointer');

var obj = {
    bla : {
        bli : [4, 5, 6]
    }
};

console.log(pointer.dict(obj));

I got the following error:

/tmp/pntr/node_modules/json-pointer/index.js:143
    return str.replace(/~/g, '~0').replace(/\//g, '~1');
               ^
TypeError: Object 0 has no method 'replace'
    at escape (/tmp/pntr/node_modules/json-pointer/index.js:143:16)
    at Array.map (native)
    at Function.compile (/tmp/pntr/node_modules/json-pointer/index.js:175:28)
    at mapObj (/tmp/pntr/node_modules/json-pointer/index.js:101:29)
    at /tmp/pntr/node_modules/json-pointer/index.js:96:21
    at forEach (/tmp/pntr/node_modules/json-pointer/node_modules/foreach/index.js:12:16)
    at mapObj (/tmp/pntr/node_modules/json-pointer/index.js:94:17)
    at /tmp/pntr/node_modules/json-pointer/index.js:96:21
    at forEach (/tmp/pntr/node_modules/json-pointer/node_modules/foreach/index.js:17:20)
    at mapObj (/tmp/pntr/node_modules/json-pointer/index.js:94:17)

Got a license for this?

Very nice implementation, clean, useful methods, well documented.

I was wondering if you had a license in mind because I'd like to copy this code into my client side google closure library and tweak it to match the style and namespacing there, if it'd be OK. I would of course leave a comment in the file back to this repo as the original source.

Add thrown exceptions in documentation

Can you improve the documentation and add what exceptions are thrown and why?
For example if you use get with a non existing reference token, the function will throw an Error the a message saying "Invalid reference token: foo".

It's too bad I have to browse the code or the unit tests to figure out what is the basic behavior of the functions.

CVE-2021-23820

+--------------+------------------+----------+-------------------+---------------+---------------------------------------+
|   LIBRARY    | VULNERABILITY ID | SEVERITY | INSTALLED VERSION | FIXED VERSION |                 TITLE                 |
+--------------+------------------+----------+-------------------+---------------+---------------------------------------+
| json-pointer | CVE-2021-23820   | CRITICAL | 0.6.1             |               | Prototype Pollution in json-pointer   |
|              |                  |          |                   |               | -->avd.aquasec.com/nvd/cve-2021-23820 |
+--------------+------------------+----------+-------------------+---------------+---------------------------------------+

GHSA-v5vg-g7rq-363w

compile([]) returns "/"

I believe it should return the empty string instead.

parse() behaves correctly:

var jsonPointer = require('json-pointer');
jsonPointer.parse('');  // []
jsonPointer.parse('/'); // ['']

But they don't match up:

jsonPointer.compile(jsonPointer.parse('')) !== '';

#dict is not working with empty arrays

dict doesn't return property if its value is an empty array, but #has checks that it exists.

jsp = require "json-pointer"
console.dir jsp.has  { a: "x", b: [] }, "/b"
console.dir jsp.dict { a: "x", b: [] }

returns:

true
{ '/a': 'x' }

Missing remove()

Hi - I just started using your library for something, and I realised I can get(), and I can set(), but I'd like to be able to remove().

Any chance of putting that in?

Funky whitespace in api.walk

There's some odd whitespace in line 130 that happens to work fine in Node but breaks when run in the browser via browserify.

Selector support and docs

Pointer arguments should be called Selectors, to remove ambiguity between the method and the arguments.

Also on readme/documentation, would be nice to list the rules of these selectors, so we know how to query and what possibilities we have

Support relative pointers?

https://tools.ietf.org/html/draft-handrews-relative-json-pointer-01

I have an implementation here. https://github.com/mokkabonna/json-hyper-schema/blob/master/src/relative-json-pointer.js complete with tests: https://github.com/mokkabonna/json-hyper-schema/blob/master/test/specs/relative-json-pointer.spec.js

Would you be willing to implement that in this library? If so I can make a PR.

If not I will publish my implementation as a separate library. I anyway depend on your implementation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.