Giter Club home page Giter Club logo

json's Introduction

@stoplight/json

Maintainability Test Coverage

Useful functions when working with JSON.

Installation

Supported in modern browsers and node.

# latest stable version
yarn add @stoplight/json

Usage

  • parseWithPointers: Like JSON.parse(val) but also returns parsing errors as well as full ast with line information.
  • pathToPointer: Turns an array of path segments into a json pointer IE ['paths', '/user', 'get'] -> #/paths/~1user/get.
  • pointerToPath: Turns a json pointer into an array of path segments IE #/paths/~1user/get -> ['paths', '/user', 'get'].
  • safeParse: Like JSON.parse(val) but does not throw on invalid JSON.
  • safeStringify: Like JSON.stringify(val) but handles circular references.
  • startsWith: Like native JS x.startsWith(y) but works with strings AND arrays.
  • trimStart: Like lodash.startsWith(x, y) but works with strings AND arrays.
  • getJsonPathForPosition: Computes JSON path for given position.
  • getLocationForJsonPath: Retrieves location of node matching given JSON path.

Example parseWithPointers

import { parseWithPointers } from "@stoplight/json";

const result = parseWithPointers('{"foo": "bar"}');

console.log(result.data); // => the {foo: "bar"} JS object
console.log(result.pointers); // => the source map with a single "#/foo" pointer that has position info for the foo property
// basic example of getJsonPathForPosition and getLocationForJsonPath
import { getJsonPathForPosition, getLocationForJsonPath, parseWithPointers } from "@stoplight/json";

const result = parseWithPointers(`{
  "hello": "world",
  "address": {
    "street": 123
  }
}`);

const path = getJsonPathForPosition(result, { line: 3, character: 15 }); // line and character are 0-based
console.log(path); // -> ["address", "street"];

const position = getLocationForJsonPath(result, ["address"]);
console.log(position.range.start); // { line: 2, character: 13 } line and character are 0-based
console.log(position.range.end); // { line: 4, character: 3 } line and character are 0-based

Contributing

  1. Clone repo.
  2. Create / checkout feature/{name}, chore/{name}, or fix/{name} branch.
  3. Install deps: yarn.
  4. Make your changes.
  5. Run tests: yarn test.prod.
  6. Stage relevant files to git.
  7. Commit: yarn commit. NOTE: Commits that don't follow the conventional format will be rejected. yarn commit creates this format for you, or you can put it together manually and then do a regular git commit.
  8. Push: git push.
  9. Open PR targeting the next branch.

json's People

Contributors

bhaskarsontakke avatar billiegoose avatar chohmann avatar dependabot[bot] avatar karol-maciaszek avatar lottamus avatar marbemac avatar p0lip avatar pytlesk4 avatar rainum avatar sb-rohitdesai avatar slavcodev avatar xvincentx avatar

Stargazers

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

Watchers

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

json's Issues

parseWithPointers(): An StackOverflow error occurs when the deeply nested JSON character string is parsed.

parseWithPointers() does not support deeply nested JSON strings.

import { parseWithPointers} from '@stoplight/json'
let json = `{ "a": { "a": ... 1 ... } }`; //  Nesting depth: 9000
parseWithPointers(json);

error log:

    at m (oh_modules/.ohpm/@[email protected]/oh_modules/@stoplight/json/index.es.js:1:8199)
    at onObjectBegin (oh_modules/.ohpm/@[email protected]/oh_modules/@stoplight/json/index.es.js:1:8369)
    at anonymous (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:359:53)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:492:9)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    at parseObject (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:509:18)
    at parseValue (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:560:24)
    at parseProperty (oh_modules/.ohpm/[email protected]/oh_modules/jsonc-parser/lib/esm/impl/parser.js:482:18)
    ......

parseWithPointers undefined pointers

Just putting the demo code after adding the dependency in package.json, result.pointers is undefined.

Context

import { parseWithPointers, safeParse, safeStringify } from "@stoplight/json";

const result = parseWithPointers(`{
    "hello": "world",
    "address": {
      "street": 123
    }
  }`);

console.log(result.data);
console.log(result.pointers);
(node:44456) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{ hello: 'world', address: { street: 123 } }
undefined

Current Behavior

It gives undefined

Expected Behavior

It should have a source map with multiple paths

Possible Workaround/Solution

Steps to Reproduce

  1. Install package @stoplight/json
  2. Mix first example code with second example code
  3. Run node ./sample.mjs

Environment

{ //... other settings
  "dependencies": {
    "@apidevtools/swagger-parser": "^10.1.0",
    "@openapi-contrib/json-schema-to-openapi-schema": "^2.2.4",
    "@openapitools/openapi-generator-cli": "^2.5.2",
    "@stoplight/json": "^3.20.1",
    "@stoplight/json-schema-merge-allof": "^0.7.8",
    "@stoplight/json-schema-ref-parser": "^9.2.2",
    "fs-extra": "^11.1.0",
    "json-schema-merge-allof": "^0.8.1",
    "json-schema-to-jsdoc": "^1.1.0",
    "node-glob": "^1.2.0",
    "openapi-types": "^12.1.0",
    "uuid": "^9.0.0"
  },
  "devDependencies": {
    "@types/node": "^18.11.18"
  }
}

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Invalid npm token.

The npm token configured in the NPM_TOKEN environment variable must be a valid token allowing to publish to the registry https://registry.npmjs.org/.

If you are using Two Factor Authentication for your account, set its level to "Authorization only" in your account settings. semantic-release cannot publish with the default "
Authorization and writes" level.

Please make sure to set the NPM_TOKEN environment variable in your CI with the exact value of the npm token.


Good luck with your project ✨

Your semantic-release bot 📦🚀

move general functions over from platform

Function to get the outer Location for a JsonPath

User story.

As a developer, I can get the outer Location for a JsonPath, so that I can copy to clipboard, delete it or insert as a sibling.

Is your feature request related to a problem?

getLocationForJsonPath gives the inner boundaries of a JsonPath so is great to update json content, but not so much for deleting o inserting into a json file (without editing unnecesary portions of text).

Calculate the outter location in the yml version of this library is somehow easy but not in the json version.

Describe the solution you'd like

Maybe a new function getOuterLocationForJsonPath

Additional context

Add any other context or screenshots about the feature request here.

Syntax error: Unexpected token 'export'

We're getting some issues in our CI using prism. Looks like it's related to this release ( https://github.com/stoplightio/json/releases/tag/v3.21.1) ?

Context

It's preventing us from running integration tests atm.

Current Behavior

On running prism we see:

/home/circleci/.local/lib/node_modules/@stoplight/prism-cli/node_modules/@stoplight/json/index.js:1
export * from './bundle';
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:128:18)
    at wrapSafe (node:internal/modules/cjs/loader:1280:20)
    at Module._compile (node:internal/modules/cjs/loader:1332:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1427:10)
    at Module.load (node:internal/modules/cjs/loader:1206:32)
    at Module._load (node:internal/modules/cjs/loader:1022:12)
    at Module.require (node:internal/modules/cjs/loader:1231:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/home/circleci/.local/lib/node_modules/@stoplight/prism-cli/node_modules/@stoplight/prism-http/dist/utils/runtimeExpression.js:8:16)
    at Module._compile (node:internal/modules/cjs/loader:1369:14)

Node.js v20.12.1

Expected Behavior

It should run :-)

Steps to Reproduce

We're using the cimg/python:3.12.3-node docker image, installing prism with:

npm install --prefix=$HOME/.local -g @stoplight/prism-cli

The error raises when we run prism mock.

## Workarounds

You can manually pin the json version to a working version on install as a temporary workaround:

npm install --prefix=$HOME/.local -g @stoplight/[email protected] @stoplight/prism-cli

Environment

  • Version used:
  • Environment name and version (e.g. Chrome 39, node.js 5.4):
  • Operating System and version (desktop or mobile):
  • Link to your environment/workspace/project:

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.