Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoder.More() doesn't read end of socket stream properly #264

Closed
Xaenalt opened this issue Apr 18, 2018 · 0 comments
Closed

Decoder.More() doesn't read end of socket stream properly #264

Xaenalt opened this issue Apr 18, 2018 · 0 comments

Comments

@Xaenalt
Copy link

Xaenalt commented Apr 18, 2018

The issue for Decoder.More() has been resolved, thank you! Unfortunately, now when reading from socket with the code in #261 the end of the stream starts to throw

ReadMapCB: expect { or n, but found , error found in #10 byte of ...|"1000000"}
|..., bigger context ...|"999998"}
{"count": "999999"}
{"count": "1000000"}
|...

Upon adding some more debugging code, it seems that Decoder.More() is returning true at the end of the stream

Updated code:

# for i in {0..1000000}; do echo "{\"count\": \"$i\"}" >> 1mjson; done
# nc localhost 1234 < 1mjson
package main

import (
	"fmt"
	"net"

	"github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

func main() {
	addr := ":1234"
	tcpAddr, _ := net.ResolveTCPAddr("tcp4", addr)
	listener, _ := net.ListenTCP("tcp", tcpAddr)

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}

		go func(conn net.Conn) {
			defer conn.Close()
			jsonReader := json.NewDecoder(conn)

			for jsonReader.More() {
				var jsonMsg map[string]interface{}
				err := jsonReader.Decode(&jsonMsg)
				if err != nil {
					fmt.Printf("Decoder.More(): %v\n", jsonReader.More())
					fmt.Println(err)
				}
				fmt.Println(jsonMsg)

			}
			fmt.Println("Deferred close of connection executing")
		}(conn)

	}

}

Updated output:

Decoder.More(): true
ReadMapCB: expect { or n, but found , error found in #10 byte of ...|"1000000"}
|..., bigger context ...|"999998"}
{"count": "999999"}
{"count": "1000000"}
|...

I've worked around this temporarily by just returning on err != nil, but it would be awesome to have this bug squashed too

Thanks!

@taowen taowen closed this as completed in 6c702ce Apr 20, 2018
zhenzou pushed a commit to zhenzou/jsoniter that referenced this issue Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant