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 Next 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
<script setup lang="ts">
import { JsonViewer } from '@fibrous/semi-ui-vue';
</script>
Basic Usage
Basic usage of JsonViewer. Pass in the height and width parameters to set the height and width of the component, and the JSON string through modelValue as the initial value. When the content changes, update:modelValue and change are emitted with the editor's current text, so v-model works. The editor is re-created only when modelValue becomes a value different from the editor's current content, so v-model echoing the same content keeps the caret; with a one-way :modelValue the editor keeps what the user typed instead of being pulled back. To read the value, call getValue() through a template ref, please refer to Methods for details.
Code
<script setup lang="ts">
import { JsonViewer } from '@fibrous/semi-ui-vue';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
</script>
<template>
<div :style="{ marginBottom: '16px' }">
<JsonViewer :height="100" :modelValue="data" :width="700" />
</div>
</template>Different lineHeight
Configure the lineHeight parameter of options to set a fixed line height (unit: px, default 20).
Code
<script setup lang="ts">
import { JsonViewer } from '@fibrous/semi-ui-vue';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
const wrapperStyle = { marginBottom: '12px', overflow: 'hidden' };
</script>
<template>
<div>
<div :style="wrapperStyle">
<JsonViewer :height="100" :modelValue="data" :options="{ lineHeight: 20 }" :width="700" />
</div>
<div :style="wrapperStyle">
<JsonViewer :height="120" :modelValue="data" :options="{ lineHeight: 24 }" :width="700" />
</div>
<div :style="wrapperStyle">
<JsonViewer :height="120" :modelValue="data" :options="{ lineHeight: 26 }" :width="700" />
</div>
</div>
</template>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.
Code
<script setup lang="ts">
import { JsonViewer } from '@fibrous/semi-ui-vue';
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."
}`;
const options = { autoWrap: true };
</script>
<template>
<div :style="{ marginBottom: '16px' }">
<JsonViewer :height="120" :modelValue="data" :options="options" :width="700" />
</div>
</template>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,
Code
<script setup lang="ts">
import { useTemplateRef } from 'vue';
import { Button, JsonViewer, type JsonViewerHandle } from '@fibrous/semi-ui-vue';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
const options = { formatOptions: { tabSize: 4, insertSpaces: true, eol: '\n' } };
const jsonviewer = useTemplateRef<JsonViewerHandle>('jsonviewer');
</script>
<template>
<div>
<Button @click="jsonviewer?.format()">格式化</Button>
<div :style="{ marginBottom: '16px', marginTop: '16px' }">
<JsonViewer ref="jsonviewer" :height="100" :modelValue="data" :options="options" :width="700" />
</div>
</div>
</template>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 notes
pathincustomRenderRuleis computed more accurately: the key token and the value token of the same key-value pair share the samepath(they both map to the property path, e.g.root.<key>).As a result, rules that only rely on
path(such aspath === '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
tokenTypeof function match:const targetPath = 'root.<key>'; // Key only match: (_value, path, tokenType) => tokenType === 'key' && path === targetPath; // Value only match: (value, path, tokenType) => tokenType === 'value' && path === targetPath;
customRenderRule is an array of rules that hold data only: match is the matching condition, and the optional name tells rules apart in the #customRender slot. The content of a matched token is drawn by the #customRender="{ item, index, content }" slot, where item is the matched rule and index is its position in customRenderRule.
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, path: string, tokenType: 'key' | 'value') => booleanvalue: Value to match (JSON key or value). Whenmatchis a function,valuewill be passed as the parsed primitive when possible (number / boolean / null / string), so strict comparisons like===work; whenmatchis a string or RegExp, matching is still based on textual content (string tokens are unquoted)path: Current matching path, format isroot.key1.key2.key3[0].key4tokenType: Current token type,'key'for JSON property name,'value'for JSON value. Useful for distinguishing between key and value matching
name: Optional rule name; useitem.namein the#customRenderslot to tell rules apart#customRenderslot: scope parameters areitem,indexandcontentcontent: Matched content. For string values, includes double quotes (e.g.,"name","Semi")- Without the slot, or when the slot renders nothing, custom rendering is skipped and the token keeps its text
Code
<script setup lang="ts">
import { Image, JsonViewer, Popover, Rating, Tag, type JsonViewerOptions } from '@fibrous/semi-ui-vue';
const data = `{
"name": "Semi",
"version": "2.7.4",
"rating": 5,
"tags": ["design", "react", "ui"],
"image": "/assets/root-web-sites/abstract.jpg"
}`;
// A rule holds data only; the #customRender slot draws the token and tells the rules apart by name
const options: JsonViewerOptions = {
formatOptions: { tabSize: 4, insertSpaces: true, eol: '\n' },
customRenderRule: [
{ name: 'popover', match: 'Semi' },
{ name: 'rating', match: (value) => value == 5 },
{
name: 'tag',
match: (_value, path) => path === 'root.tags[0]' || path === 'root.tags[1]' || path === 'root.tags[2]',
},
{ name: 'image', match: /^http/ },
],
readOnly: true,
autoWrap: true,
};
// content is the raw string token with its quotes; without them it is a valid URL
const unquote = (content: string) => content.replace(/^"|"$/g, '');
</script>
<template>
<div>
<div :style="{ marginBottom: '16px', marginTop: '16px' }">
<JsonViewer :height="200" :modelValue="data" :options="options" :showSearch="false" :width="600">
<template #customRender="{ item, content }">
<Popover v-if="item.name === 'popover'" showArrow trigger="hover">
<span>{{ content }}</span>
<template #content>我是用户自定义的渲染</template>
</Popover>
<Rating v-else-if="item.name === 'rating'" :defaultValue="Number(content)" disabled :size="10" />
<Tag v-else-if="item.name === 'tag'" shape="circle" size="small">{{ content }}</Tag>
<Popover v-else showArrow trigger="hover">
<span>{{ content }}</span>
<template #content>
<Image :height="100" :src="unquote(content)" :width="100" />
</template>
</Popover>
</template>
</JsonViewer>
</div>
</div>
</template>Custom Search Button
Use the #searchButton slot to customize the rendering of the search button, enabling fixed positioning, custom styles, and more. Providing the slot replaces the default button as a whole; defaultButton is the default button's VNode, rendered with <component :is>. When showSearch is false, neither is rendered.
Code
<script setup lang="ts">
import { IconSearch } from '@fibrous/semi-icons-vue';
import { Button, JsonViewer } from '@fibrous/semi-ui-vue';
const data = `{
"name": "Semi",
"version": "0.0.0"
}`;
</script>
<template>
<div :style="{ marginBottom: '16px' }">
<JsonViewer :height="200" :modelValue="data" :width="700">
<template #searchButton="{ defaultButton, showSearchBar, onToggleSearchBar }">
<div :style="{ position: 'absolute', top: '10px', right: '10px', zIndex: 10 }">
<Button v-if="!showSearchBar" @click="onToggleSearchBar">
<template #icon>
<IconSearch />
</template>
搜索
</Button>
<component :is="defaultButton" v-else />
</div>
</template>
</JsonViewer>
</div>
</template>API Reference
JsonViewer
Props
| Property | Description | Type | Default |
|---|---|---|---|
| modelValue | Text of the editor, used with v-model. A value different from the editor's current content re-creates the editor; echoing the current content back keeps the caret. Without it the editor starts empty. | string | undefined |
| width | Width. A number is taken as px; a string is used as is. | string | number | semiDefault("JsonViewer", "width", 400) |
| height | Height. A number is taken as px; a string is used as is. | string | number | semiDefault("JsonViewer", "height", 400) |
| showSearch | Whether to show the search button. | boolean | semiDefault("JsonViewer", "showSearch", true) |
| options | Editor options; see JsonViewerOptions for the fields. The defaults of ariaLabel, readOnly and autoWrap merge per key, so passing some fields keeps the defaults of the others. A deep change re-creates the editor; mutating a nested object such as formatOptions or customRenderRule in place goes unnoticed, so replace it with a new object. | JsonViewerOptions | semiGlobalDefault("JsonViewer", "options") |
| limitSearchButtonBounds | Whether to limit the search button drag bounds within the component container. | boolean | semiDefault("JsonViewer", "limitSearchButtonBounds", false) |
Events
| Event | Description | Callback parameters |
|---|---|---|
| update:modelValue | Emitted when the content changes, for v-model to write back, with the editor's current text. | [value: string] |
| change | Emitted when the content changes, with the editor's current text. It is a component event, so @change does not receive the native change bubbling up from the search bar inputs. | [value: string] |
Slots
| Slot | Description | Scope parameters |
|---|---|---|
| customRender | Replaces the content of a token matched by options.customRenderRule, with the matched rule item, its index and the token text content as scope parameters. Without it, or when empty, the token keeps its text. Only takes effect in read-only mode. | JsonViewerCustomRenderSlotProps |
| searchButton | Replaces the default search button as a whole. defaultButton is the default button's VNode, rendered with <component :is>; the other parameters build a custom search UI. Not rendered when showSearch is false. | JsonViewerSearchButtonSlotProps |
Exposes
| Name | Description | Type |
|---|---|---|
| getValue | Returns the editor's current text. | Type() => string |
| format | Formats the content with formatOptions. | Type() => void |
| search | Searches for text; the optional parameters control case sensitivity, whole word and regular expression. | Type(searchText: string, caseSensitive?: boolean | undefined, wholeWord?: boolean | undefined, regex?: boolean | undefined) => void |
| getSearchResults | Returns the current search results. | Type() => FindMatch[] | null | undefined |
| prevSearch | Moves to the previous search result, with an optional step. | Type(step?: number | undefined) => void |
| nextSearch | Moves to the next search result, with an optional step. | Type(step?: number | undefined) => void |
| replace | Replaces the current search match. An empty string deletes the match; without a match it does nothing. | Type(replaceText: string) => void |
| replaceAll | Replaces all search matches. An empty string deletes them. | Type(replaceText: string) => void |
| getStyle | Returns the current width and height. | Type() => { width: string | number; height: string | number; } |
class, style and native attributes fall on the root element. When the root has no role and aria-label or aria-labelledby is passed, the root gets role="group"; an explicit role wins.
JsonViewerOptions
| Attribute | Description | Type | Default |
|---|---|---|---|
| lineHeight | Height of each line of content, unit:px | number | 20 |
| autoWrap | Whether to wrap lines automatically. | boolean | true |
| readOnly | Whether to be read-only. | boolean | false |
| customRenderRule | Custom render rules | CustomRenderRule[] | - |
| formatOptions | Content format setting | FormattingOptions | - |
| completionOptions | Auto-completion setting | CompletionOptions | - |
| ariaLabel | Accessible name of the content area (the read-only scroll region, or the editable textbox) | string | locale JsonViewer.content |
CustomRenderRule
The type is named JsonViewerCustomRenderRule in Vue. A rule holds data only; the content of a matched token is drawn by the #customRender slot.
| Attribute | Description | Type | Default |
|---|---|---|---|
| match | Matching rule | string | RegExp | (value: string | number | boolean | null, path: string, tokenType: 'key' | 'value') => boolean | - |
| name | Rule name, to tell rules apart in the #customRender slot | string | - |
FormattingOptions
| Attribute | Description | Type | Default |
|---|---|---|---|
| tabSize | Indent size | number | 4 |
| insertSpaces | Whether to use spaces for indentation | boolean | true |
| eol | Line break character | string | '\n' |
CompletionOptions
| Attribute | Description | Type | Default |
|---|---|---|---|
| staticCompletions | Static completion items. They are appended only after the candidates inferred from the document structure, and are not offered when nothing can be inferred; an item whose label matches an existing candidate is not added again. Only label, insertText (falling back to label) and documentation of each item are read | CompletionItem[] | - |
SearchControls
With the #searchButton slot, the scope parameters (type JsonViewerSearchButtonSlotProps) contain the following properties besides defaultButton:
| Attribute | Description | Type |
|---|---|---|
| showSearchBar | Whether the search bar is currently visible | boolean |
| onToggleSearchBar | Toggle search bar visibility | () => void |
| onSearch | Execute search | (text: string, caseSensitive?: boolean, wholeWord?: boolean, regex?: boolean) => void |
| onPrevSearch | Navigate to previous search result | () => void |
| onNextSearch | Navigate to next search result | () => void |
| onReplace | Replace current search match | (text: string) => void |
| onReplaceAll | Replace all search matches | (text: string) => void |
Methods
Methods exposed by the component can be called through a template ref (type JsonViewerHandle) to achieve certain special interactions.
| Method | Description |
|---|---|
| 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 |
| getStyle() | Get the current width and height |
Performance
Bundle Size
| Libs Name | Size | Size (Gzip) |
|---|---|---|
| JsonViewer | 203.14kb | 51.23kb |
| MonacoEditor | 5102.0 KB | 1322.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 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% | - | - | - |