mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-04 10:58:29 +02:00
Fix bug when output json with special chars
This commit is contained in:
@ -4,6 +4,7 @@
|
|||||||
package print
|
package print
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@ -164,6 +165,8 @@ func toSnakeCase(str string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// outputJSON prints structured data as json
|
// outputJSON prints structured data as json
|
||||||
|
// Since golang's map is unordered, we need to ensure consistent ordering, we have
|
||||||
|
// to output the JSON ourselves.
|
||||||
func outputJSON(f io.Writer, headers []string, values [][]string) {
|
func outputJSON(f io.Writer, headers []string, values [][]string) {
|
||||||
fmt.Fprintln(f, "[")
|
fmt.Fprintln(f, "[")
|
||||||
itemCount := len(values)
|
itemCount := len(values)
|
||||||
@ -172,12 +175,12 @@ func outputJSON(f io.Writer, headers []string, values [][]string) {
|
|||||||
for i, value := range values {
|
for i, value := range values {
|
||||||
fmt.Fprintf(f, "%s{\n", space)
|
fmt.Fprintf(f, "%s{\n", space)
|
||||||
for j, val := range value {
|
for j, val := range value {
|
||||||
intVal, _ := strconv.Atoi(val)
|
v, err := json.Marshal(val)
|
||||||
if strconv.Itoa(intVal) == val {
|
if err != nil {
|
||||||
fmt.Fprintf(f, "%s%s\"%s\": %s", space, space, toSnakeCase(headers[j]), val)
|
return
|
||||||
} else {
|
|
||||||
fmt.Fprintf(f, "%s%s\"%s\": \"%s\"", space, space, toSnakeCase(headers[j]), val)
|
|
||||||
}
|
}
|
||||||
|
key, _ := json.Marshal(toSnakeCase(headers[j]))
|
||||||
|
fmt.Fprintf(f, "%s:%s", key, v)
|
||||||
if j != headersCount-1 {
|
if j != headersCount-1 {
|
||||||
fmt.Fprintln(f, ",")
|
fmt.Fprintln(f, ",")
|
||||||
} else {
|
} else {
|
||||||
|
@ -21,6 +21,9 @@ func TestPrint(t *testing.T) {
|
|||||||
values: [][]string{
|
values: [][]string{
|
||||||
{"new a", "some bbbb"},
|
{"new a", "some bbbb"},
|
||||||
{"AAAAA", "b2"},
|
{"AAAAA", "b2"},
|
||||||
|
{"\"abc", "\"def"},
|
||||||
|
{"'abc", "de'f"},
|
||||||
|
{"\\abc", "'def\\"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,7 +36,16 @@ func TestPrint(t *testing.T) {
|
|||||||
}{}
|
}{}
|
||||||
assert.NoError(t, json.NewDecoder(buf).Decode(&result))
|
assert.NoError(t, json.NewDecoder(buf).Decode(&result))
|
||||||
|
|
||||||
if assert.Len(t, result, 2) {
|
if assert.Len(t, result, 5) {
|
||||||
assert.EqualValues(t, "new a", result[0].A)
|
assert.EqualValues(t, "new a", result[0].A)
|
||||||
|
assert.EqualValues(t, "some bbbb", result[0].B)
|
||||||
|
assert.EqualValues(t, "AAAAA", result[1].A)
|
||||||
|
assert.EqualValues(t, "b2", result[1].B)
|
||||||
|
assert.EqualValues(t, "\"abc", result[2].A)
|
||||||
|
assert.EqualValues(t, "\"def", result[2].B)
|
||||||
|
assert.EqualValues(t, "'abc", result[3].A)
|
||||||
|
assert.EqualValues(t, "de'f", result[3].B)
|
||||||
|
assert.EqualValues(t, "\\abc", result[4].A)
|
||||||
|
assert.EqualValues(t, "'def\\", result[4].B)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user