package katago import ( "bytes" "encoding/json" "git.noahlan.cn/noahlan/ntool/njson" ) type Serializer struct { contentBuf *bytes.Buffer lastJson string } func NewSerializer() *Serializer { ret := &Serializer{ contentBuf: bytes.NewBuffer([]byte{}), } return ret } func (s *Serializer) reset() { s.contentBuf.Reset() } func (s *Serializer) LastJson() string { return s.lastJson } func (s *Serializer) Marshal(v any) ([]byte, error) { return njson.Marshal(v) } func (s *Serializer) Unmarshal(line string, v any) (bool, error) { s.contentBuf.WriteString(line) if json.Valid(s.contentBuf.Bytes()) { defer func() { s.lastJson = s.contentBuf.String() s.reset() }() err := njson.Unmarshal(s.contentBuf.Bytes(), v) if err != nil { return false, err } return true, nil } else { return false, nil } }