简体   繁体   中英

In golang json.Unmarshal() works in playground/copy pasted JSON but not in actual code

I am writing a program in Golang that interfaces with a modified version of the barefoot mapmatching library which returns results in json via netcat.

My in my actual code json.Unmarshal will only parse the response to the nil value of the struct. But if print the json to console (see code snippet below) and copy paste into goplayground it behaves as expected.

I am wondering if this is an encoding issue that is bypassed when I copy paste from the console as a result.

How do I get my code to process the same string as it is received from barefoot as when it is copy pasted from the console?

Here is the relevant code snippet (structs are identical to goplayground)

body := io_func(conn, cmd)
    var obvs []Json_out
    json.Unmarshal([]byte(body), &obvs)
    fmt.Println(body)
    fmt.Println(obvs)

and io_func() if relevant (the response is two lines, with a message on the first and a json string on the second)

func io_func(conn net.Conn, cmd string) string {
    fmt.Fprintf(conn, cmd+"\n")
    r := bufio.NewReader(conn)
    header, _ := r.ReadString('\n')
    if header == "SUCCESS\n" {
        resp, _ := r.ReadString('\n')
    return resp
    } else {
        return ""
    }

}

The io_func function creates and discards a bufio.Reader and data the reader may have buffered. If the application calls io_func more than once, then the application may be discarding data read from the network. Fix by creating a single bufio.Reader outside the function and pass that single reader to each invocation of io_func.

Always check and handle errors. The error returned from any of these functions may point you in the right direction for a fix.

func io_func(r *bufio.Reader, conn net.Conn, cmd string) (string, error) {
    fmt.Fprintf(conn, cmd+"\n")
    header, err := r.ReadString('\n')
    if err != nil {
        return "", err
    }
    if header == "SUCCESS\n" {
        return r.ReadString('\n')
    }
    return "", nil
}


...

r := bufio.NewReader(conn)

body, err := io_func(r, conn, cmd)
if err != nil {
    // handle error
}
var obvs []Json_out
err = json.Unmarshal([]byte(body), &obvs)
if err != nil {
    // handle error
}
fmt.Println(body)
fmt.Println(obvs)

// read next 
body, err = io_func(r, conn, cmd)
if err != nil {
    // handle error
}

The application uses newline to terminate the JSON body, but newline is valid whitespace in JSON. If the peer includes a newline in the JSON, then the application will read a partial message.

Following Cerise Limón's advice to properly handle error messages I determined the osm_id value in the JSON was being parsed by json.Unmarshall as number when taking the string from io_func() , although it wasn't doing so when the string was passed in manually in the playground example. Although I don't understand why this is so I would have picked it up with proper error handling.

I altered barefoot code to return the osm_id explicitly in inverted commas since, although only ever composed of digits, I only use it as a string. It now works as expected. Equally I could have changed the type in the struct and convert in Go as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM