mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-07 04:12:55 +02:00
Fix bug when output json with special chars
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
package print
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@ -164,6 +165,8 @@ func toSnakeCase(str string) string {
|
||||
}
|
||||
|
||||
// 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) {
|
||||
fmt.Fprintln(f, "[")
|
||||
itemCount := len(values)
|
||||
@ -172,12 +175,12 @@ func outputJSON(f io.Writer, headers []string, values [][]string) {
|
||||
for i, value := range values {
|
||||
fmt.Fprintf(f, "%s{\n", space)
|
||||
for j, val := range value {
|
||||
intVal, _ := strconv.Atoi(val)
|
||||
if strconv.Itoa(intVal) == val {
|
||||
fmt.Fprintf(f, "%s%s\"%s\": %s", space, space, toSnakeCase(headers[j]), val)
|
||||
} else {
|
||||
fmt.Fprintf(f, "%s%s\"%s\": \"%s\"", space, space, toSnakeCase(headers[j]), val)
|
||||
v, err := json.Marshal(val)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
key, _ := json.Marshal(toSnakeCase(headers[j]))
|
||||
fmt.Fprintf(f, "%s:%s", key, v)
|
||||
if j != headersCount-1 {
|
||||
fmt.Fprintln(f, ",")
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user