func NewJsonEncoder() Encoder { je := &JsonEncoder{ buff: bytes.NewBuffer(make([]byte, 1024*200)), } je.enc = ffjson.NewEncoder(je.buff) return je }
func TestMarshalEncoderError(t *testing.T) { out := NopWriter{} enc := ffjson.NewEncoder(&out) v := GiveError{} err := enc.Encode(v) require.Error(t, err, "excpected error from encoder") err = enc.Encode(newLogFFRecord()) require.NoError(t, err, "error did not clear as expected.") err = enc.EncodeFast(newLogRecord()) require.Error(t, err, "excpected error from encoder on type that isn't fast") }
func TestMarshalEncoder(t *testing.T) { record := newLogFFRecord() out := bytes.Buffer{} enc := ffjson.NewEncoder(&out) err := enc.Encode(record) require.NoError(t, err) require.NotEqual(t, 0, out.Len(), "encoded buffer size should not be 0") out.Reset() err = enc.EncodeFast(record) require.NoError(t, err) require.NotEqual(t, 0, out.Len(), "encoded buffer size should not be 0") }
func PerformStat() { go func() { // Grab the initial stats. prev := appdb.Stats() for { time.Sleep(60 * time.Second) stats := appdb.Stats() diff := stats.Sub(&prev) json.NewEncoder(os.Stdout).Encode(diff) prev = stats } }() }
//logs to a file func logConnFile(logC chan dnsLogEntry, opts *logOptions) { logger := &lumberjack.Logger{ Filename: opts.Filename, MaxSize: opts.MaxSize, // megabytes MaxBackups: opts.MaxBackups, MaxAge: opts.MaxAge, //days } enc := ffjson.NewEncoder(bufio.NewWriter(logger)) for message := range logC { enc.Encode(message) } logger.Close() }
func BenchmarkMarshalJSONNativeReuse(b *testing.B) { record := newLogFFRecord() buf, err := json.Marshal(&record) if err != nil { b.Fatalf("Marshal: %v", err) } b.SetBytes(int64(len(buf))) enc := ffjson.NewEncoder(&NopWriter{}) b.ResetTimer() for i := 0; i < b.N; i++ { err := enc.Encode(record) if err != nil { b.Fatalf("Marshal: %v", err) } } }