Plus · JsonViewer
JsonViewer
Used for displaying and editing JSON data

When to use

The JsonViewer component can be used for the display and editing of JSON data.
Semi mainly referred to the design concept of the text-buffer data structure of VS Code, reused some utilities and data type definitions (Token parsing, language services, etc.), and implemented the JsonViewer component in combination with our functional/style customization requirements. Visually, it will be more coordinated with other components within the Semi Design system, and it will be more convenient for customized rendering and customization of specific data types.
Compared with directly using MonacoEditor, Semi JsonViewer has additional processing in engineering construction, is simpler to use, and there is no need to pay attention to complex configurations such as Webpack plugins and worker loaders. At the same time, since we only focus on the JSON data format, it is more lightweight. While being ready to use out of the box, it has a smaller size (📦-96%), a more extreme loading speed (🚀 -53.5%), and less memory occupation (⬇️71.6% reduction). For data with five million lines and below, data loading and parsing can be completed within 1 second.
Detailed comparison data can be referred to in the Performance section.
  • If you only need to preview/edit JSON and don't need to modify other more complex programming languages, we recommend that you choose JsonViewer.
  • If you also need to handle data/code files in other formats and the full capabilities of a code editor (syntax highlighting, code completion, error prompts, complex editing, etc.) are essential and the build product size is not a key concern, we recommend that you choose Monaco Editor.

Demos

How to import

JsonViewer supported from v2.71.0

Basic Usage

Basic usage of JsonViewer. Pass in the height and width parameters to set the height, width and initial value of the component. Pass in the JSON string through the value. Note: JsonViewer is an uncontrolled component. If the value prop is passed as a controlled property (i.e., React State), it is not recommended to modify the value prop (setState) in onChange. If you need to get the component's value, you can get it through ref, please refer to Methods for details.

Differrent lineHeight

Configure the lineHeight parameter of options to set a fixed line height (unit: px, default 18).

Autowrap

Configure the autoWrap parameter of options. When it is set to true, the component will automatically wrap lines according to the length of the content.

Format options

Configure options.formatOptions to set the formatting configuration of the component.
  • tabSize: number,set the indent size to 4, which means each level of indentation is 4 spaces.
  • insertSpaces: boolean,when it is true, it means using spaces for indentation, and when it is false, it means using tabs.
  • eol: string,set the line break character, which can be \n\r\n

Custom Render Rules

By configuring the options.customRenderRule parameter, you can customize how JSON content is rendered (Note: only works in read-only mode).
Behavior change in 2.96.0
Starting from 2.96.0, path in customRenderRule is computed more accurately: the key token and the value token of the same key-value pair share the same path (they both map to the property path, e.g. root.<key>).
As a result, rules that only rely on path (such as path === 'root.<key>') may match both the key and the value, which can change which rule “wins” compared to previous versions.
If you want to match only the key or only the value, use the third argument tokenType of function match:
customRenderRule is an array of rules, where each rule contains two properties:
  • match: Matching condition, can be one of three types:
    • String: Exact match
    • Regular expression: Match by regex
    • Function: Custom matching logic, with signature (value: string | number | boolean | null, pathChain: string, tokenType: 'key' | 'value') => boolean
      • value: Value to match (JSON key or value). When match is a function, value will be passed as the parsed primitive when possible (number / boolean / null / string), so strict comparisons like === work; when match is a string or RegExp, matching is still based on textual content (string tokens are unquoted)
      • path: Current matching path, format is root.key1.key2.key3[0].key4
      • tokenType: Current token type, 'key' for JSON property name, 'value' for JSON value. Useful for distinguishing between key and value matching
  • render: Custom render function, with signature (content: string) => React.ReactNode
    • content: Matched content. For string values, includes double quotes (e.g., "name", "Semi")

Custom Search Button

Use the renderSearchButton property to customize the rendering of the search button, enabling fixed positioning, custom styles, and more.

API Reference

JsonViewer

AttributeDescriptionTypeDefault
valueDisplay contentstring-
heightHeight of wrapper DOMnumber-
widthWidth of wrapper DOMnumber-
classNameclassName of wrapper DOMstring-
styleInlineStyle of wrapper DOMobject-
showSearchWhether to show search iconbooleantrue
limitSearchButtonBoundsWhether to limit the search button drag bounds within the container >=2.94.0booleanfalse
renderSearchButtonCustom render search button >=2.95.0(defaultButton: ReactNode, controls: SearchControls) => ReactNode-
optionsFormatting configurationJsonViewerOptions-
onChangeCallback for content change(value: string) => void-

JsonViewerOptions

AttributeDescriptionTypeDefaultVersion
lineHeightHeight of each line of content, unit:pxnumber20-
autoWrapWhether to wrap lines automatically.booleantrue-
readOnlyWhether to be read-only.booleanfalse-
customRenderRuleCustom render rulesCustomRenderRule[]-2.74.0
formatOptionsContent format settingFormattingOptions--

FormattingOptions

AttributeDescriptionTypeDefault
tabSizeIndent size. Unit: pxnumber4
insertSpacesWhether to use spaces for indentationbooleantrue
eolLine break characterstring'\n'

CustomRenderRule

AttributeDescriptionTypeDefault
matchMatching rulestring | RegExp | (value: string | number | boolean | null, path: string, tokenType: 'key' | 'value') => boolean-
renderRender function(content: string) => React.ReactNode-

SearchControls

When using renderSearchButton, the second parameter controls contains the following properties:
AttributeDescriptionType
showSearchBarWhether the search bar is currently visibleboolean
onToggleSearchBarToggle search bar visibility() => void
onSearchExecute search(text: string, caseSensitive?: boolean, wholeWord?: boolean, regex?: boolean) => void
onPrevSearchNavigate to previous search result() => void
onNextSearchNavigate to next search result() => void
onReplaceReplace current search match(text: string) => void
onReplaceAllReplace all search matches(text: string) => void

Methods

Methods bound to the component instance can be called via ref to achieve certain special interactions.
MethodDescription
getValue()Get current value
format()Format current content
search(searchText: string, caseSensitive?: boolean, wholeWord?: boolean, regex?: boolean)Search for text with optional parameters
getSearchResults()Get current search results
prevSearch(step?: number)Navigate to previous search result, with optional step size
nextSearch(step?: number)Navigate to next search result, with optional step size
replace(replaceText: string)Replace current search match
replaceAll(replaceText: string)Replace all search matches

Performance

Bundle Size

Libs NameSizeSize (Gzip)
JsonViewer203.14kb51.23kb
MonacoEditor5102.0 KB1322.7 KB

Time for rendering data of different magnitudes.

For details on the generation method of the test data, please refer to URL
When the data volume exceeds 500,000 lines, ReactMonacoEditor turns off highlighting and other behaviors by default, and the data comparison does not follow the principle of a single variable.
Libs Name1k lines5k lines10 thousand lines100 thousand lines500 thousand lines1 million lines3 million lines
JsonViewer30.42ms30.66ms36.87ms52.73ms111.02ms178.81ms506.25ms
ReactMonacoEditor72.01ms73.76ms76.64ms97.89ms133.31ms202.79ms495.53ms
Performance improvement57.70%58.41%51.87%46.11%---