mirror of
https://github.com/metabarcoding/obitools4.git
synced 2025-06-29 16:20:46 +00:00
Remove HTML escaping to json marshaling
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
@ -188,3 +189,17 @@ func AtomicCounter(initial ...int) func() int {
|
|||||||
|
|
||||||
return nextCounter
|
return nextCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Marshal is a UTF-8 friendly marshaler. Go's json.Marshal is not UTF-8
|
||||||
|
// friendly because it replaces the valid UTF-8 and JSON characters "&". "<",
|
||||||
|
// ">" with the "slash u" unicode escaped forms (e.g. \u0026). It preemptively
|
||||||
|
// escapes for HTML friendliness. Where text may include any of these
|
||||||
|
// characters, json.Marshal should not be used. Playground of Go breaking a
|
||||||
|
// title: https://play.golang.org/p/o2hiX0c62oN
|
||||||
|
func JsonMarshal(i interface{}) ([]byte, error) {
|
||||||
|
buffer := &bytes.Buffer{}
|
||||||
|
encoder := json.NewEncoder(buffer)
|
||||||
|
encoder.SetEscapeHTML(false)
|
||||||
|
err := encoder.Encode(i)
|
||||||
|
return bytes.TrimRight(buffer.Bytes(), "\n"), err
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
|
||||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
)
|
)
|
||||||
@ -70,7 +71,7 @@ func FormatFastSeqJsonHeader(sequence *obiseq.BioSequence) string {
|
|||||||
annotations := sequence.Annotations()
|
annotations := sequence.Annotations()
|
||||||
|
|
||||||
if annotations != nil {
|
if annotations != nil {
|
||||||
text, err := json.Marshal(sequence.Annotations())
|
text, err := goutils.JsonMarshal(sequence.Annotations())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/goutils"
|
||||||
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
"git.metabarcoding.org/lecasofts/go/obitools/pkg/obiseq"
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
)
|
)
|
||||||
@ -216,10 +217,12 @@ func ParseOBIFeatures(text string, annotations obiseq.Annotation) string {
|
|||||||
dict := make(map[string]int)
|
dict := make(map[string]int)
|
||||||
err = json.Unmarshal(j, &dict)
|
err = json.Unmarshal(j, &dict)
|
||||||
value = dict
|
value = dict
|
||||||
case strings.HasSuffix(key, "_status"):
|
case strings.HasSuffix(key, "_status") ||
|
||||||
|
strings.HasSuffix(key, "_mutation"):
|
||||||
dict := make(map[string]string)
|
dict := make(map[string]string)
|
||||||
err = json.Unmarshal(j, &dict)
|
err = json.Unmarshal(j, &dict)
|
||||||
value = dict
|
value = dict
|
||||||
|
|
||||||
default:
|
default:
|
||||||
dict := make(map[string]interface{})
|
dict := make(map[string]interface{})
|
||||||
err = json.Unmarshal(j, &dict)
|
err = json.Unmarshal(j, &dict)
|
||||||
@ -299,9 +302,10 @@ func FormatFastSeqOBIHeader(sequence *obiseq.BioSequence) string {
|
|||||||
case string:
|
case string:
|
||||||
text.WriteString(fmt.Sprintf("%s=%s; ", key, t))
|
text.WriteString(fmt.Sprintf("%s=%s; ", key, t))
|
||||||
case map[string]int,
|
case map[string]int,
|
||||||
|
map[string]string,
|
||||||
map[string]interface{},
|
map[string]interface{},
|
||||||
obiseq.StatsOnValues:
|
obiseq.StatsOnValues:
|
||||||
tv, err := json.Marshal(t)
|
tv, err := goutils.JsonMarshal(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Cannot convert %v value", value)
|
log.Fatalf("Cannot convert %v value", value)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user