Socket
Socket
Sign inDemoInstall

@codemirror/lang-json

Package Overview
Dependencies
9
Maintainers
2
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @codemirror/lang-json

JSON language support for the CodeMirror code editor


Version published
Weekly downloads
636K
increased by4.45%
Maintainers
2
Created
Weekly downloads
 

Package description

What is @codemirror/lang-json?

@codemirror/lang-json is a language support package for CodeMirror 6 that provides syntax highlighting, parsing, and other language-related features for JSON. It is part of the CodeMirror 6 ecosystem, which is a versatile text editor implemented in JavaScript for the browser.

What are @codemirror/lang-json's main functionalities?

Syntax Highlighting

This code sets up a CodeMirror editor with JSON syntax highlighting. The `json()` function from `@codemirror/lang-json` is used to enable JSON language support.

import { json } from '@codemirror/lang-json';
import { EditorState } from '@codemirror/state';
import { EditorView, basicSetup } from '@codemirror/basic-setup';

const state = EditorState.create({
  doc: '{ "key": "value" }',
  extensions: [basicSetup, json()]
});

const view = new EditorView({
  state,
  parent: document.body
});

JSON Parsing

This code demonstrates how to parse a JSON document using the `@codemirror/lang-json` package. The `syntaxTree` function is used to get the syntax tree of the current document.

import { json } from '@codemirror/lang-json';
import { syntaxTree } from '@codemirror/language';
import { EditorState } from '@codemirror/state';

const state = EditorState.create({
  doc: '{ "key": "value" }',
  extensions: [json()]
});

const tree = syntaxTree(state);
console.log(tree);

Error Highlighting

This code sets up a CodeMirror editor with JSON syntax highlighting and error detection. The trailing comma in the JSON document will be highlighted as an error.

import { json } from '@codemirror/lang-json';
import { EditorState } from '@codemirror/state';
import { EditorView, basicSetup } from '@codemirror/basic-setup';

const state = EditorState.create({
  doc: '{ "key": "value", }', // Note the trailing comma, which is an error
  extensions: [basicSetup, json()]
});

const view = new EditorView({
  state,
  parent: document.body
});

Other packages similar to @codemirror/lang-json

Changelog

Source

6.0.0 (2022-06-08)

Breaking changes

Update dependencies to 6.0.0

Readme

Source

@codemirror/lang-json NPM version

[ WEBSITE | ISSUES | FORUM | CHANGELOG ]

This package implements JSON language support for the CodeMirror code editor.

The project page has more information, a number of examples and the documentation.

This code is released under an MIT license.

We aim to be an inclusive, welcoming community. To make that explicit, we have a code of conduct that applies to communication around the project.

API Reference

json() → LanguageSupport

JSON language support.

jsonLanguage: LezerLanguage

A language provider that provides JSON parsing.

jsonParseLinter() → fn(viewEditorView) → Diagnostic[]

Calls JSON.parse on the document and, if that throws an error, reports it as a single diagnostic.

Keywords

FAQs

Last updated on 08 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc