feat: add "Unicode JSON" format to display JSON string contains unescaped unicode charactor #158

This commit is contained in:
Lykin 2024-02-21 15:07:39 +08:00
parent 1c4462b161
commit 3057012710
10 changed files with 74 additions and 25 deletions

View File

@ -2,6 +2,7 @@ package types
const FORMAT_RAW = "Raw" const FORMAT_RAW = "Raw"
const FORMAT_JSON = "JSON" const FORMAT_JSON = "JSON"
const FORMAT_UNICODE_JSON = "Unicode JSON"
const FORMAT_YAML = "YAML" const FORMAT_YAML = "YAML"
const FORMAT_XML = "XML" const FORMAT_XML = "XML"
const FORMAT_HEX = "Hex" const FORMAT_HEX = "Hex"

View File

@ -15,6 +15,7 @@ type DataConvert interface {
var ( var (
jsonConv JsonConvert jsonConv JsonConvert
uniJsonConv UnicodeJsonConvert
yamlConv YamlConvert yamlConv YamlConvert
xmlConv XmlConvert xmlConv XmlConvert
base64Conv Base64Convert base64Conv Base64Convert
@ -30,11 +31,12 @@ var (
) )
var BuildInFormatters = map[string]DataConvert{ var BuildInFormatters = map[string]DataConvert{
types.FORMAT_JSON: jsonConv, types.FORMAT_JSON: jsonConv,
types.FORMAT_YAML: yamlConv, types.FORMAT_UNICODE_JSON: uniJsonConv,
types.FORMAT_XML: xmlConv, types.FORMAT_YAML: yamlConv,
types.FORMAT_HEX: hexConv, types.FORMAT_XML: xmlConv,
types.FORMAT_BINARY: binaryConv, types.FORMAT_HEX: hexConv,
types.FORMAT_BINARY: binaryConv,
} }
var BuildInDecoders = map[string]DataConvert{ var BuildInDecoders = map[string]DataConvert{

View File

@ -0,0 +1,43 @@
package convutil
import (
"bytes"
"encoding/json"
"strings"
)
type UnicodeJsonConvert struct{}
func (UnicodeJsonConvert) Enable() bool {
return true
}
func (UnicodeJsonConvert) Decode(str string) (string, bool) {
trimedStr := strings.TrimSpace(str)
if strings.HasPrefix(trimedStr, "{") && strings.HasSuffix(trimedStr, "}") {
var obj map[string]any
if err := json.Unmarshal([]byte(trimedStr), &obj); err == nil {
var out []byte
if out, err = json.MarshalIndent(obj, "", " "); err == nil {
return string(out), true
}
}
} else if strings.HasPrefix(trimedStr, "[") && strings.HasSuffix(trimedStr, "]") {
var arr []any
if err := json.Unmarshal([]byte(trimedStr), &arr); err == nil {
var out []byte
if out, err = json.MarshalIndent(arr, "", " "); err == nil {
return string(out), true
}
}
}
return str, false
}
func (UnicodeJsonConvert) Encode(str string) (string, bool) {
var dst bytes.Buffer
if err := json.Compact(&dst, []byte(str)); err != nil {
return str, false
}
return dst.String(), true
}

View File

@ -94,6 +94,7 @@ const enableSave = computed(() => {
const viewLanguage = computed(() => { const viewLanguage = computed(() => {
switch (viewAs.format) { switch (viewAs.format) {
case formatTypes.JSON: case formatTypes.JSON:
case formatTypes.UNICODE_JSON:
return 'json' return 'json'
case formatTypes.YAML: case formatTypes.YAML:
return 'yaml' return 'yaml'

View File

@ -107,17 +107,17 @@ const fieldColumn = computed(() => ({
}, },
})) }))
const displayCode = computed(() => { const isCode = computed(() => {
return props.format === formatTypes.JSON return props.format === formatTypes.JSON || props.format === formatTypes.UNICODE_JSON
}) })
// const valueFilterOption = ref(null) // const valueFilterOption = ref(null)
const valueColumn = computed(() => ({ const valueColumn = computed(() => ({
key: 'value', key: 'value',
title: () => i18n.t('common.value'), title: () => i18n.t('common.value'),
align: displayCode.value ? 'left' : 'center', align: isCode.value ? 'left' : 'center',
titleAlign: 'center', titleAlign: 'center',
resizable: true, resizable: true,
ellipsis: displayCode.value ellipsis: isCode.value
? false ? false
: { : {
tooltip: { tooltip: {
@ -137,7 +137,7 @@ const valueColumn = computed(() => ({
// return !!~row.v.indexOf(value.toString()) // return !!~row.v.indexOf(value.toString())
// }, // },
render: (row) => { render: (row) => {
if (displayCode.value) { if (isCode.value) {
return h('pre', {}, row.dv || row.v) return h('pre', {}, row.dv || row.v)
} }
return row.dv || row.v return row.dv || row.v

View File

@ -77,16 +77,16 @@ const inEdit = computed(() => {
}) })
const fullEdit = ref(false) const fullEdit = ref(false)
const displayCode = computed(() => { const isCode = computed(() => {
return props.format === formatTypes.JSON return props.format === formatTypes.JSON || props.format === formatTypes.UNICODE_JSON
}) })
const valueFilterOption = ref(null) const valueFilterOption = ref(null)
const valueColumn = computed(() => ({ const valueColumn = computed(() => ({
key: 'value', key: 'value',
title: () => i18n.t('common.value'), title: () => i18n.t('common.value'),
align: displayCode.value ? 'left' : 'center', align: isCode.value ? 'left' : 'center',
titleAlign: 'center', titleAlign: 'center',
ellipsis: displayCode.value ellipsis: isCode.value
? false ? false
: { : {
tooltip: { tooltip: {
@ -106,7 +106,7 @@ const valueColumn = computed(() => ({
return !!~row.v.indexOf(value.toString()) return !!~row.v.indexOf(value.toString())
}, },
render: (row) => { render: (row) => {
if (displayCode.value) { if (isCode.value) {
return h('pre', {}, row.dv || row.v) return h('pre', {}, row.dv || row.v)
} }
return row.dv || row.v return row.dv || row.v

View File

@ -76,16 +76,16 @@ const inEdit = computed(() => {
}) })
const fullEdit = ref(false) const fullEdit = ref(false)
const displayCode = computed(() => { const isCode = computed(() => {
return props.format === formatTypes.JSON return props.format === formatTypes.JSON || props.format === formatTypes.UNICODE_JSON
}) })
const valueFilterOption = ref(null) const valueFilterOption = ref(null)
const valueColumn = computed(() => ({ const valueColumn = computed(() => ({
key: 'value', key: 'value',
title: () => i18n.t('common.value'), title: () => i18n.t('common.value'),
align: displayCode.value ? 'left' : 'center', align: isCode.value ? 'left' : 'center',
titleAlign: 'center', titleAlign: 'center',
ellipsis: displayCode.value ellipsis: isCode.value
? false ? false
: { : {
tooltip: { tooltip: {
@ -105,7 +105,7 @@ const valueColumn = computed(() => ({
return !!~row.v.indexOf(value.toString()) return !!~row.v.indexOf(value.toString())
}, },
render: (row) => { render: (row) => {
if (displayCode.value) { if (isCode.value) {
return h('pre', {}, row.dv || row.v) return h('pre', {}, row.dv || row.v)
} }
return row.dv || row.v return row.dv || row.v

View File

@ -47,6 +47,7 @@ const keyType = redisTypes.STRING
const viewLanguage = computed(() => { const viewLanguage = computed(() => {
switch (viewAs.format) { switch (viewAs.format) {
case formatTypes.JSON: case formatTypes.JSON:
case formatTypes.UNICODE_JSON:
return 'json' return 'json'
case formatTypes.YAML: case formatTypes.YAML:
return 'yaml' return 'yaml'

View File

@ -124,17 +124,17 @@ const scoreColumn = computed(() => ({
}, },
})) }))
const displayCode = computed(() => { const isCode = computed(() => {
return props.format === formatTypes.JSON return props.format === formatTypes.JSON || props.format === formatTypes.UNICODE_JSON
}) })
const valueFilterOption = ref(null) const valueFilterOption = ref(null)
const valueColumn = computed(() => ({ const valueColumn = computed(() => ({
key: 'value', key: 'value',
title: () => i18n.t('common.value'), title: () => i18n.t('common.value'),
align: displayCode.value ? 'left' : 'center', align: isCode.value ? 'left' : 'center',
titleAlign: 'center', titleAlign: 'center',
resizable: true, resizable: true,
ellipsis: displayCode.value ellipsis: isCode.value
? false ? false
: { : {
tooltip: { tooltip: {
@ -155,7 +155,7 @@ const valueColumn = computed(() => ({
}, },
// sorter: (row1, row2) => row1.value - row2.value, // sorter: (row1, row2) => row1.value - row2.value,
render: (row) => { render: (row) => {
if (displayCode.value) { if (isCode.value) {
return h('pre', {}, row.dv || row.v) return h('pre', {}, row.dv || row.v)
} }
return row.dv || row.v return row.dv || row.v

View File

@ -5,6 +5,7 @@
export const formatTypes = { export const formatTypes = {
RAW: 'Raw', RAW: 'Raw',
JSON: 'JSON', JSON: 'JSON',
UNICODE_JSON: 'Unicode JSON',
YAML: 'YAML', YAML: 'YAML',
XML: 'XML', XML: 'XML',
HEX: 'Hex', HEX: 'Hex',