localeCode: en-US order: 27 category: Plus title: JsonViewer icon: doc-jsonviewer dir: column noInline: true brief: Used for displaying and editing JSON data
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.
JsonViewer
.Monaco Editor
.JsonViewer supported from v2.71.0
import { JsonViewer } from '@douyinfe/semi-ui';
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
.
import React from 'react';
import { JsonViewer } from '@douyinfe/semi-ui';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
class SimpleJsonViewer extends React.Component {
render() {
return (
<div style={{ marginBottom: 16 }}>
<JsonViewer height={100} width={400} value={data} />
</div>
);
}
}
render(SimpleJsonViewer);
Configure the lineHeight
parameter of options
to set a fixed line height (unit: px, default 18).
import React from 'react';
import { JsonViewer, Space } from '@douyinfe/semi-ui';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
class SimpleJsonViewerWithLineHeight extends React.Component {
render() {
return (
<div>
<div style={{ marginBottom: 20 }}>
<JsonViewer height={100} width={320} value={data} options={{ lineHeight: 20 }} />
</div>
<div style={{ marginBottom: 20 }}>
<JsonViewer height={120} width={320} value={data} options={{ lineHeight: 24 }} />
</div>
<div style={{ marginBottom: 20 }}>
<JsonViewer height={120} width={320} value={data} options={{ lineHeight: 26 }} />
</div>
</div>
);
}
}
render(SimpleJsonViewerWithLineHeight);
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.
import React from 'react';
import { JsonViewer } from '@douyinfe/semi-ui';
const data = `{
"name": "Semi",
"version": "0.0.0",
"description": "Semi Design is a design system that defines a set of mid_back design and front_end basic components."
}`;
class SimpleJsonViewerWithAutoWrap extends React.Component {
render() {
return (
<div style={{ marginBottom: 16 }}>
<JsonViewer height={120} width={800} value={data} options={{ autoWrap: true }} />
</div>
);
}
}
render(SimpleJsonViewerWithAutoWrap);
Configure options.formatOptions
to set the formatting configuration of the component.
eol: string,set the line break character, which can be \n
,\r\n
,
import React, { useRef } from 'react';
import { JsonViewer, Button } from '@douyinfe/semi-ui';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
function FormatJsonComponent() {
const jsonviewerRef = useRef();
return (
<div>
<Button onClick={() => console.log(jsonviewerRef.current.format())}>Manual Format</Button>
<div style={{ marginBottom: 16, marginTop: 16 }}>
<JsonViewer
ref={jsonviewerRef}
height={100}
width={400}
value={data}
options={{ formatOptions: { tabSize: 4, insertSpaces: true, eol: '\n' } }}
/>
</div>
</div>
);
}
render(FormatJsonComponent);
Attribute | Description | Type | Default |
---|---|---|---|
value | Display content | string | - |
height | Height of wrapper DOM | number | - |
width | Width of wrapper DOM | number | - |
className | className of wrapper DOM | string | - |
style | InlineStyle of wrapper DOM | object | - |
options | Formatting configuration | JsonViewerOptions | - |
onChange | Callback for content change | (value: string) => void | - |
Attribute | Description | Type | Default |
---|---|---|---|
lineHeight | Height of each line of content, unit:px | number | 20 |
autoWrap | Whether to wrap lines automatically. | boolean | true |
formatOptions | Content format setting | FormattingOptions | - |
Attribute | Description | Type | Default |
---|---|---|---|
tabSize | Indent size. Unit: px | number | 4 |
insertSpaces | Whether to use spaces for indentation | boolean | true |
eol | Line break character | string | '\n' |
Methods bound to the component instance can be called via ref
to achieve certain special interactions.
Method | Description |
---|---|
getValue() | Get current value |
format() | Format current content |
Libs Name | Size | Size (Gzip) |
---|---|---|
JsonViewer | 203.14kb | 51.23kb |
MonacoEditor | 5102.0 KB | 1322.7 KB |
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 Name | 1k lines | 5k lines | 10 thousand lines | 100 thousand lines | 500 thousand lines | 1 million lines | 3 million lines |
---|---|---|---|---|---|---|---|
JsonViewer | 30.42ms | 30.66ms | 36.87ms | 52.73ms | 111.02ms | 178.81ms | 506.25ms |
ReactMonacoEditor | 72.01ms | 73.76ms | 76.64ms | 97.89ms | 133.31ms | 202.79ms | 495.53ms |
Performance improvement | 57.70% | 58.41% | 51.87% | 46.11% | - | - | - |