mirror of
https://github.com/asciinema/asciinema.git
synced 2025-12-16 03:38:03 +01:00
Don't reinvent "marshal string to JSON" wheel
This commit is contained in:
@@ -3,7 +3,6 @@ package asciicast
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Frame struct {
|
||||
@@ -12,8 +11,9 @@ type Frame struct {
|
||||
}
|
||||
|
||||
func (f *Frame) MarshalJSON() ([]byte, error) {
|
||||
s := fmt.Sprintf(`[%.6f, "%v"]`, f.Delay, rawBytesToEscapedString(f.Data))
|
||||
return []byte(s), nil
|
||||
s, _ := json.Marshal(string(f.Data))
|
||||
json := fmt.Sprintf(`[%.6f, %s]`, f.Delay, s)
|
||||
return []byte(json), nil
|
||||
}
|
||||
|
||||
func (f *Frame) UnmarshalJSON(data []byte) error {
|
||||
@@ -33,28 +33,3 @@ func (f *Frame) UnmarshalJSON(data []byte) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func rawBytesToEscapedString(bytes []byte) string {
|
||||
res := make([]rune, 0)
|
||||
|
||||
for _, runeValue := range string(bytes) {
|
||||
runes := []rune(strconv.Quote(string(runeValue)))
|
||||
runes = runes[1 : len(runes)-1]
|
||||
|
||||
if len(runes) == 4 && runes[0] == '\\' && runes[1] == 'x' {
|
||||
runes = []rune{'\\', 'u', '0', '0', runes[2], runes[3]}
|
||||
}
|
||||
|
||||
if len(runes) == 2 && runes[0] == '\\' && runes[1] == 'a' {
|
||||
runes = []rune{'\\', 'u', '0', '0', '0', '7'}
|
||||
}
|
||||
|
||||
if len(runes) == 2 && runes[0] == '\\' && runes[1] == 'v' {
|
||||
runes = []rune{'\\', 'u', '0', '0', '0', 'b'}
|
||||
}
|
||||
|
||||
res = append(res, runes...)
|
||||
}
|
||||
|
||||
return string(res)
|
||||
}
|
||||
|
||||
@@ -16,12 +16,22 @@ func TestFrame_MarshalJSON(t *testing.T) {
|
||||
{
|
||||
4.906e-06,
|
||||
[]byte{0x1b, 0x5b, 0x31, 0x6d, 0x25, 0x1b, 0x5b, 0x32, 0x33, 0x6d, 0x1b, 0x5b, 0x30, 0x6d, 0x0d, 0x20, 0x0d, 0xc5, 0x82, 0x07, 0x0b, 0x85, 0x61},
|
||||
`{"test":[0.000005,"\u001b[1m%\u001b[23m\u001b[0m\r \rł\u0007\u000b<EFBFBD>a"]}`,
|
||||
`{"test":[0.000005,"\u001b[1m%\u001b[23m\u001b[0m\r \rł\u0007\u000b\ufffda"]}`,
|
||||
},
|
||||
{
|
||||
12.345,
|
||||
[]byte{0xe2, 0x8c, 0x98, 0x6c, 0x25, 0x50, 0x93, 0xe8, 0xd4, 0x6a, 0x03, 0xbe, 0xf3, 0xfe, 0xc3, 0x45, 0xee, 0x87, 0xca, 0x6b, 0x92, 0xa6, 0xa7, 0x8f, 0xb8, 0x85, 0xd0, 0x07, 0x91, 0x9b, 0x91, 0x45, 0x2f, 0x1c, 0xc8, 0xb3, 0x26, 0x96, 0xfa, 0x22, 0x8e, 0x3f, 0x12, 0x64, 0xcf, 0xf0, 0xe4, 0x01, 0x71, 0xee, 0x65, 0x6e, 0x4a, 0x7a, 0x81, 0x3f, 0x2f, 0x84, 0x3f, 0xc4, 0x27, 0x2d, 0xf5, 0x35, 0x34, 0x02, 0x6c},
|
||||
`{"test":[12.345000,"⌘l%P<EFBFBD><EFBFBD><EFBFBD>j\u0003<30><33><EFBFBD><EFBFBD>E<EFBFBD><45><EFBFBD>k<EFBFBD><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\u0007<30><37><EFBFBD>E/\u001cȳ\u0026<32><36>\"<22>?\u0012d<32><64><EFBFBD>\u0001q<31>enJz<4A>?/<2F>?<3F>'-<2D>54\u0002l"]}`,
|
||||
`{"test":[12.345000,"⌘l%P\ufffd\ufffd\ufffdj\u0003\ufffd\ufffd\ufffd\ufffdE\ufffd\ufffd\ufffdk\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u0007\ufffd\ufffd\ufffdE/\u001cȳ\u0026\ufffd\ufffd\"\ufffd?\u0012d\ufffd\ufffd\ufffd\u0001q\ufffdenJz\ufffd?/\ufffd?\ufffd'-\ufffd54\u0002l"]}`,
|
||||
},
|
||||
{
|
||||
0.123456,
|
||||
[]byte{byte('&'), byte('<'), byte('>')},
|
||||
`{"test":[0.123456,"\u0026\u003c\u003e"]}`,
|
||||
},
|
||||
{
|
||||
0.999999,
|
||||
[]byte{0xf3, 0xba, 0x88, 0x8f, 0xbe, 0xc7, 0xd6},
|
||||
`{"test":[0.999999,"\ufffd\ufffd\ufffd"]}`,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user