How to use json-ptr - 10 common examples

To help you get started, we’ve selected a few json-ptr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github IBM / report-toolkit / packages / diff / src / index.js View on Github external
) {
                  /**
                   * @type {Array}
                   */
                  const nextPathParts = patchObj.path.split('/');
                  const nextPathIdx = Number(nextPathParts.pop()) + offset;
                  const nextPath = nextPathParts.concat(nextPathIdx).join('/');
                  oldValue = ptr.get(reportA, nextPath);
                  offset++;
                  nextValue = {...patchObj, oldValue, path: nextPath};
                } else {
                  // there's no old value to look up for 'add' operations.
                  if (patchObj.op === 'add') {
                    nextValue = patchObj;
                  } else {
                    oldValue = ptr.get(reportA, patchObj.path);
                    nextValue = {...patchObj, oldValue};
                  }
                  offset = 1;
                }
                lastPatchObj = patchObj;
                observer.next(nextValue);
              }, patch);
            } catch (err) {
github IBM / report-toolkit / packages / diff / src / index.js View on Github external
// is repeated 3 times with the same path. to get the old value--in other words,
                // *what* was removed--we must increment the index at the end of the path
                // and use that to retrieve the value from reportA.
                if (
                  lastPatchObj &&
                  patchObj.op === 'remove' &&
                  patchObj.path === lastPatchObj.path &&
                  patchObj.op === lastPatchObj.op
                ) {
                  /**
                   * @type {Array}
                   */
                  const nextPathParts = patchObj.path.split('/');
                  const nextPathIdx = Number(nextPathParts.pop()) + offset;
                  const nextPath = nextPathParts.concat(nextPathIdx).join('/');
                  oldValue = ptr.get(reportA, nextPath);
                  offset++;
                  nextValue = {...patchObj, oldValue, path: nextPath};
                } else {
                  // there's no old value to look up for 'add' operations.
                  if (patchObj.op === 'add') {
                    nextValue = patchObj;
                  } else {
                    oldValue = ptr.get(reportA, patchObj.path);
                    nextValue = {...patchObj, oldValue};
                  }
                  offset = 1;
                }
                lastPatchObj = patchObj;
                observer.next(nextValue);
              }, patch);
            } catch (err) {
github exegesis-js / exegesis / src / utils / jsonSchema.ts View on Github external
if(ctx.replaced[schema.$ref]) {
                schema.$ref = ctx.replaced[schema.$ref];
            } else if(jsonPaths.jsonPointerStartsWith(schema.$ref, ctx.rootSubtreeRef + '/')) {
                ctx.replaced[schema.$ref] = jsonPaths.jsonPointerStripPrefix(schema.$ref, ctx.rootSubtreeRef);
                schema.$ref = ctx.replaced[schema.$ref];
            } else if(!refResolver(schema.$ref)) {
                // Don't know how to resolve this ref
                if(!options.skipUnknownRefs) {
                    throw new Error(`Can't find ref ${schema.$ref}`);
                }
            } else {
                ctx.result.definitions = ctx.result.definitions || {};

                // Find a name to store this under in 'definitions'.
                const origRef = schema.$ref;
                const jsonPath = jsonPtr.decode(schema.$ref);
                let newRefSuffix : string | undefined = jsonPath.length > 0 ? jsonPath[jsonPath.length - 1] : undefined;
                while(!newRefSuffix || ctx.result.definitions[newRefSuffix]) {
                    newRefSuffix = `schema${ctx.schemaCount++}`;
                }

                // Do the replacement.
                schema.$ref = ctx.replaced[schema.$ref] = `#/definitions/${newRefSuffix}`;
                ctx.result.definitions[newRefSuffix] = extractSchemaPriv(origRef, refResolver, options, ctx);
            }
        }
    });
github exegesis-js / exegesis / src / oas3 / urlEncodedBodyParser.ts View on Github external
throw new Error(`Media Type Object ${context.jsonPointer} with 'content' must have a 'schema'`);
        }

        // Find the schema object for the mediaType.
        const schema = resolveRef(context.openApiDoc, `${context.jsonPointer}/schema`);

        // The encoding object describes how parameters should be parsed from the document.
        for(const parameterName of Object.keys(mediaType.encoding)) {
            const encoding: oas3.EncodingPropertyObject = mediaType.encoding[parameterName];

            const parameterSchemaPath = findProperty([], schema, parameterName);
            if(!parameterSchemaPath) {
                throw new Error(`Cannot find parameter ${parameterName} in schema for ${context.jsonPointer}`);
            }

            const parameterSchema = extractSchema(context.openApiDoc, jsonPtr.encodePointer(parameterSchemaPath));

            let parameterDescriptor: ParameterDescriptor;
            if(encoding.contentType) {
                const parser = context.options.parameterParsers.get(encoding.contentType);
                if(!parser) {
                    throw new Error(`No string parser found for ${encoding.contentType} in ${context.jsonPointer}`);
                }
                parameterDescriptor = {
                    contentType: encoding.contentType,
                    parser,
                    uriEncoded: true,
                    schema: parameterSchema
                };
            } else {
                parameterDescriptor = {
                    style: encoding.style || 'form',
github sonnyp / JSON8 / packages / pointer / benchmark.js View on Github external
foo: [1, 2, 3, 4], // eslint-disable-line no-magic-numbers
  baz: [
    {
      qux: "hello",
    },
  ],
};

const pointer = "/baz/0/qux";
const compiled = ooPointer.compile(pointer);

const janlCompiled = jsonpointer.compile(pointer);

const evaluate = jsonpointerjs.get(doc);

const ptr = flitbit.create(pointer);

const suite = benchmark.Suite("pointer");

suite
  .add("find", function() {
    ooPointer.find(doc, pointer);
  })
  .add("compiled", function() {
    compiled(doc);
  })
  .add("compile + compiled", function() {
    ooPointer.compile(pointer)(doc);
  })
  .add("janl/node-jsonpointer get", function() {
    jsonpointer.get(doc, pointer);
  })
github boschni / json-merger / src / operations / MatchOperation.ts View on Github external
if (keywordValue.index !== undefined) {
            matchedResultArrayIndex = keywordValue.index === "-" ? resultArray.length - 1 : keywordValue.index;
        }

        // Handle $match.query
        else if (keywordValue.query !== undefined) {
            // Try to find a matching item in the result
            const path = jsonpath.paths(resultArray, keywordValue.query)[0];
            matchedResultArrayIndex = path !== undefined ? path[1] as number : undefined;
        }

        // Handle $match.path
        else if (keywordValue.path !== undefined) {
            // Try to find a matching item in the result
            if (jsonPtr.get(resultArray, keywordValue.path) !== undefined) {
                [matchedResultArrayIndex] = jsonPtr.decodePointer(keywordValue.path)[0];
            }
        }

        // Ignore the item if no match found
        if (matchedResultArrayIndex === undefined || resultArray[matchedResultArrayIndex] === undefined) {
            return {resultArray, resultArrayIndex};
        }

        // Process result array item
        const result = this._processor.processArrayItem(keywordValue.value, sourceArray, sourceArrayIndex, resultArray, matchedResultArrayIndex, target);

        // Check if an array item has been inserted or removed below or at the current array item
        if (matchedResultArrayIndex <= resultArrayIndex) {
            resultArrayIndex += result.resultArrayIndex - matchedResultArrayIndex;
        }
github exegesis-js / exegesis / test / fixtures / index.ts View on Github external
export function makeContext(
    openApiDoc: oas3.OpenAPIObject,
    jsonPointer: string,
    options?: Partial
) {
    return new Oas3CompileContext(
        openApiDoc,
        jsonPtr.decode(jsonPointer),
        Object.assign({}, defaultCompiledOptions, options || {})
    );
}
github exegesis-js / exegesis / src / utils / jsonPaths.ts View on Github external
function normalize(path: string) : string {
    return jsonPtr.encodePointer(jsonPtr.decode(path));
}
github exegesis-js / exegesis / src / utils / json-schema-resolve-ref.ts View on Github external
function resolveRefPriv(document: any, ref: string) : any {
    if(!ref.startsWith('#/') && !ref.startsWith('/') && ref !== '') {
        throw new Error(`Cannot resolve non-local ref ${ref}`);
    }

    const path = jsonPtr.decode(ref);
    let currentDoc = document;
    while(path.length > 0) {
        const pathname = path.shift() as string;
        currentDoc = currentDoc && currentDoc[pathname];
        while(currentDoc && currentDoc.$ref) {
            currentDoc = resolveRefPriv(document, currentDoc.$ref);
        }
    }

    return currentDoc;
}
github nodecg / nodecg / lib / replicant / schema-hacks.js View on Github external
function resolvePointerReference(obj, ref) {
	return ptr.get(obj, ref);
}

json-ptr

A complete implementation of JSON Pointer (RFC 6901) for nodejs and modern browsers.

MIT
Latest version published 2 years ago

Package Health Score

65 / 100
Full package analysis