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_JSON = "JSON"
const FORMAT_UNICODE_JSON = "Unicode JSON"
const FORMAT_YAML = "YAML"
const FORMAT_XML = "XML"
const FORMAT_HEX = "Hex"

View File

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

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(() => {
switch (viewAs.format) {
case formatTypes.JSON:
case formatTypes.UNICODE_JSON:
return 'json'
case formatTypes.YAML:
return 'yaml'

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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