I am having a very strange error while working with a json string. The problem was first introduced when I added a key-value pair of strings to the json input, which was "DeviceIdentifier": "device-id"
. I pared down my code to the minimum necessary to display the error. The error disappears when I change pretty much anything about the data in that key-value pair, which seems very strange to me. I can just use other keys to circumvent the error, but it seems like there is something I am missing here. Either that or there would seem to be something wrong with the library function... Any ideas?
package main
import (
"encoding/json"
"fmt"
)
type S struct {
Name string
DeviceIdentifier []byte
}
func main() {
var s S
data := []byte(`{"Name": "test", "DeviceIdentifier": "device-id"}`)
if err := json.Unmarshal(data, &s); err != nil {
fmt.Println(err.Error())
}
}
Go playground link: http://play.golang.org/p/huXuaokGik
Json package documentation: http://golang.org/pkg/encoding/json/
UPDATE
I just discovered that the encoding succeeds when the value string has a length that is divisible by 4, e.g. abcd
and abcdefgh
work, while abcde
and abcdefg` do not.
Now that I know what base64 strings are the error makes a lot of sense. References here:
Wikipedia: http://en.wikipedia.org/wiki/Base64
Conversion tool: http://www.string-functions.com/base64encode.aspx