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 and special character tag #10677

Closed
cdeadlock opened this issue May 3, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@cdeadlock
Copy link

commented May 3, 2015

The below json demarshalling code will not detect the @rid field.

I can not tell what I am doing wrong here, is the @ symbol some kind of utf problem?

type TargetsResult struct{
  Result Targets "json: 'result'" 
}
type Targets []struct{
  Rid string "json: '@rid'"
  Toclaim string "json: 'toClaim'"
}

func TestJsonTarget(t *testing.T){
  //t.Skip()
  input := "{\"result\": [  {\"@rid\": \"#5:0\", \"toClaim\": \"testcode\"},{\"@rid\": \"#5:2\", \"toClaim\": \"second\"},{\"@rid\": \"#5:1\"}]}"
  var results TargetsResult
  if err := json.Unmarshal([]byte(input), &results); err != nil{
    fmt.Print(err) 
  }
  fmt.Print(results.Result)
}
Output:
[{ testcode} { second} { }]

If I unmarshal the same sample into the ugly

type TargetsResult map[string][]map[string]interface{}

I can access it with

results["result"][0]["@rid"].(string)

Besides this test where the input is a go string (a rune?) I also tested the actual result coming from an http REST response where the header specifically says utf8.

@mdempsky

This comment has been minimized.

Copy link
Member

commented May 3, 2015

If you use json:"@rid" instead, it works: http://play.golang.org/p/TcmPNuwFsd

I can't explain why "json: 'toClaim'" works though. It seems like that's actually a bug.

@mdempsky mdempsky changed the title encoding json Unmarshal and special character tag encoding/json: Unmarshal and special character tag May 3, 2015

@mdempsky

This comment has been minimized.

Copy link
Member

commented May 3, 2015

Oh, right, the other tags weren't actually working. It's just that your struct fields were already named "Result" and "Toclaim".

Closing since it seems to be working as intended.

@mdempsky mdempsky closed this May 3, 2015

@cdeadlock

This comment has been minimized.

Copy link
Author

commented May 3, 2015

Yes it appears I was confused by the super specific requirements for the struct tags:

You can not use single quote to make strings (' same key as double quote), only back-quote (` same key as tilde) this gives compilations errors

You can not have a space between the json: and the double quoted tag "@Rid" (no compilation errors, it just does not use it during demarshalling)
go version go1.2.1 linux/amd64

Thanks for the help

@golang golang locked and limited conversation to collaborators Jun 25, 2016

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.