FAQ
ReadFromUDP can return a packet of any size, from 1 to 2000 bytes, you
need to reslice your buffer using the number of bytes actually read.

json.Unmarshal(buf[:n], &msg)

Also, there is no guarantee that ReadFromUDP will return a complete json blob.
On Fri, Sep 21, 2012 at 5:42 PM, Huanchen Zhang wrote:
Hi,

When I try to umarshal a byte array received via udp, and I get the the
following error %!(EXTRA string=invalid character '\x00' after top-level
value).

Following are my codes:

buf := make([]byte, 2000)
n, _, err:= cli.udpConn.ReadFromUDP(buf)
var msg Message
json.Unmarshal(buf, &msg)

Anyone has any idea ?

Thank you !
Huanchen

--
--

Search Discussions

  • Huanchen Zhang at Sep 21, 2012 at 2:19 pm
    Hi,

    When I try to umarshal a byte array received via udp, and I get the the
    following error %!(EXTRA string=invalid character '\x00' after top-level
    value).

    Following are my codes:

    buf := make([]byte, 2000)
    n, _, err:= cli.udpConn.ReadFromUDP(buf)
    var msg Message
    json.Unmarshal(buf, &msg)

    Anyone has any idea ?

    Thank you !
    Huanchen

    --
  • Reck Hou at Apr 15, 2013 at 12:47 pm
    Hi guys, I've met the same issue.
    My code:
    request := make([]byte, 1024)
    read_len, err := conn.Read(request)
    request_right := request[:read_len]
    j := new(Json)
    err := j.UnmarshalJSON(request) // not working
    err := j.UnmarshalJSON(request_right) // works

    but I used fmt.Print(request) & fmt.Print(request_right) and got exactly
    same output, so I think it's a bug of json.UnmarshalJSON or fmt.Print

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Minux at Apr 15, 2013 at 12:52 pm

    On Mon, Apr 15, 2013 at 3:38 PM, Reck Hou wrote:
    My code:
    request := make([]byte, 1024)
    read_len, err := conn.Read(request)
    request_right := request[:read_len]
    j := new(Json)
    err := j.UnmarshalJSON(request) // not working
    if read_len < len(request) then request will contain extra "\x00" at the end
    and that's why UnmarshalJSON(request) won't work.
    err := j.UnmarshalJSON(request_right) // works

    but I used fmt.Print(request) & fmt.Print(request_right) and got exactly
    same output, so I think it's a bug of json.UnmarshalJSON or fmt.Print
    what does fmt.Print(request) say?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jan Mercl at Apr 15, 2013 at 12:53 pm

    On Mon, Apr 15, 2013 at 9:38 AM, Reck Hou wrote:
    Hi guys, I've met the same issue.
    My code:
    request := make([]byte, 1024)
    read_len, err := conn.Read(request)
    request_right := request[:read_len]
    j := new(Json)
    err := j.UnmarshalJSON(request) // not working
    err := j.UnmarshalJSON(request_right) // works

    but I used fmt.Print(request) & fmt.Print(request_right) and got exactly
    same output, so I think it's a bug of json.UnmarshalJSON or fmt.Print
    Try instead 'fmt.Printf("% x", request)' and 'fmt.Printf("% x",
    request_right)' and compare the outputs.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Mingkaidong at Sep 16, 2013 at 3:28 pm
    your request is 1024 in length and it seems that UnmarshalJSON considers
    the blank bytes (which are not filled by conn.Read) in request.
    On Monday, April 15, 2013 3:38:12 PM UTC+8, Reck Hou wrote:

    Hi guys, I've met the same issue.
    My code:
    request := make([]byte, 1024)
    read_len, err := conn.Read(request)
    request_right := request[:read_len]
    j := new(Json)
    err := j.UnmarshalJSON(request) // not working
    err := j.UnmarshalJSON(request_right) // works

    but I used fmt.Print(request) & fmt.Print(request_right) and got exactly
    same output, so I think it's a bug of json.UnmarshalJSON or fmt.Print
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 21, '12 at 2:09p
activeSep 16, '13 at 3:28p
posts6
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase