You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when using the Encode of the standard package, it adds a \n after every object,
but when json-iterator gets it as an input, it fails,
see code for an example:
import (
"bytes"
"encoding/json"
"io"
"testing"
jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/require"
)
func TestJsonIterator(t *testing.T) {
// encode jsons using the built in package
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
err := enc.Encode(map[string]string{"key": "value"})
require.NoError(t, err)
err = enc.Encode(map[string]string{"key2": "value2"})
require.NoError(t, err)
// after the last value, \n is added
var nl byte = '\n'
require.Equal(t, nl, buf.Bytes()[buf.Len()-1])
// the build in package can decode the values as expected
reader1 := bytes.NewReader(buf.Bytes())
dec1 := json.NewDecoder(reader1)
var res map[string]string
err = dec1.Decode(&res)
require.NoError(t, err)
require.Equal(t, map[string]string{"key": "value"}, res)
res = nil
err = dec1.Decode(&res)
require.NoError(t, err)
require.Equal(t, map[string]string{"key2": "value2"}, res)
res = nil
err = dec1.Decode(&res)
require.Equal(t, io.EOF, err)
reader2 := bytes.NewReader(buf.Bytes())
// if the last \n is removed from the input, everything works as expected
//reader2 := bytes.NewReader(buf.Bytes()[:buf.Len()-1])
dec2 := jsoniter.ConfigDefault.NewDecoder(reader2)
err = dec2.Decode(&res)
require.NoError(t, err)
require.Equal(t, map[string]string{"key": "value"}, res)
res = nil
err = dec2.Decode(&res)
require.NoError(t, err)
require.Equal(t, map[string]string{"key2": "value2"}, res)
res = nil
err = dec2.Decode(&res)
// this check fails
// the actual error is:
// &errors.errorString{s:"ReadMapCB: expect { or n, but found \x00, error found in #10 byte of ...|:\"value2\"}\n|..., bigger context ...|{\"key\":\"value\"}\n{\"key2\":\"value2\"}\n|..."}
require.Equal(t, io.EOF, err)
}
The text was updated successfully, but these errors were encountered:
when using the Encode of the standard package, it adds a
\n
after every object,but when json-iterator gets it as an input, it fails,
see code for an example:
The text was updated successfully, but these errors were encountered: