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

encoding/json: unmarshal malformed json doesn't produce an error #16899

Closed
4xoc opened this issue Aug 27, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@4xoc
Copy link

commented Aug 27, 2016

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go1.7
  2. What operating system and processor architecture are you using (go env)?
    linux/amd64 (Arch Linux)
    kernel: 4.7.1-1-ARCH
  3. What did you do?
    Simply reading a file containing JSON data where in one string a backslash '' is present. At this point it is not meant to escape anything but simply represents malformed JSON. Below an example of what I've done.

foo.json {"foo":"b\ar"}

func LoadConfig() int {
  config struct {
    Foo string
  }

  file, err := os.Open("foo.json")
  if err != nil {
    return 1
  }

  decoder := json.NewDecoder(file)
  err = decoder.Decode(config)
  if err != nil {
    return 2
  }

  return 0
}
  1. What did you expect to see?
    Unmarshal returning a non-nil error describing malformed JSON.
  2. What did you see instead?
    No value of the target struct is set by unmarshal.

From my understanding this should produce an error as not just a single field wasn't read but the whole file.

@odeke-em

This comment has been minimized.

Copy link
Member

commented Aug 27, 2016

Hello there @4xoc.

For starters I've fixed up a bit of your example to make it a runnable go program but it correctly reports that we have an invalid character 'a' in string
screen shot 2016-08-27 at 5 58 18 am

I see in your code

if err != nil {
    return 2
  }

Perhaps you aren't catching that status code of 2 in whichever caller is invoking LoadConfig()?

And, yes if it encounters an error it won't set the properties, so please check your errors/status codes.

To test it out without having to make a file; just to make the code sample easily reproducible on the playground at https://play.golang.org/p/W91r6xm-t9 or inlined. As you'll see, we can successfully catch and print the error

package main

import (
    "encoding/json"
    "fmt"
    "strings"
)

func main() {
    fmt.Println(LoadConfig())
}

func LoadConfig() error {
    type config struct {
        Foo string
    }

    r := strings.NewReader(`{"foo":"b\ar"}`)
    decoder := json.NewDecoder(r)
    cf := new(config)
    if err := decoder.Decode(cf); err != nil {
        return err
    }

    return nil
}

I am marking this as not a bug. If you think otherwise, please let me know.

@odeke-em odeke-em changed the title unmarshal malformed json doesn't produce an error encoding/json: unmarshal malformed json doesn't produce an error Aug 27, 2016

@odeke-em odeke-em closed this Aug 27, 2016

@4xoc

This comment has been minimized.

Copy link
Author

commented Aug 27, 2016

Thanks @odeke-em, I found a problem in my code connected to the return 2 you pointed out and that was indeed the problem.

@golang golang locked and limited conversation to collaborators Aug 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.