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

Unmarshal of object/struct attributes fails #74

Open
gopherfortress opened this issue Feb 3, 2017 · 5 comments
Open

Unmarshal of object/struct attributes fails #74

gopherfortress opened this issue Feb 3, 2017 · 5 comments

Comments

@gopherfortress
Copy link

Hello,

I am trying to unmarshal a JSON API response which contains attributes that are objects. I tried marshaling and the result JSON is correct but when I try to unmarshal that same JSON it fails with data is not a jsonapi representation of '*main.Profile'. Here is some example code:

package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/google/jsonapi"
)

func main() {
	p := &Profile{ID: "1", Avatar: &Avatar{Large: "url1", Small: "url2"}}
	buf := &bytes.Buffer{}
	if err := jsonapi.MarshalOnePayload(buf, p); err != nil {
		log.Fatal(err)
	}

	p = &Profile{}
	fmt.Println(string(buf.Bytes()))
	err := jsonapi.UnmarshalPayload(buf, p)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("ID: %v, Avatar:%v\n", p.ID, p.Avatar)
}

type Profile struct {
	ID     string  `json:"id" jsonapi:"primary,profiles"`
	Avatar *Avatar `json:"avatar" jsonapi:"attr,avatar"`
}

type Avatar struct {
	Large string `json:"large"`
	Small string `json:"small"`
}

I also tried annotating with attr like

type Avatar struct {
	Large string `json:"large" jsonapi:"attr,large,omitempty"`
	Small string `json:"small" jsonapi:"attr,small,omitempty"`
}

but no luck.

@gopherfortress
Copy link
Author

Any news about this issue? Thanks.

@aren55555
Copy link
Contributor

We have been actively working on this feature in the branch https://github.com/google/jsonapi/tree/feature/embeded-structs

@wayne-brush-kw
Copy link

wayne-brush-kw commented Apr 13, 2018

Is work on this still progressing? My case is similar but here is my code:

type Profile struct {
ID string json:"id" jsonapi:"primary,profiles"
AvatarList []Avatar json:"avatar" jsonapi:"attr,avatar"
}

type Avatar struct {
Large string json:"large"
Small string json:"small"
}

I tried this with the current embeded-structs branch and it didn't work. Any status on this would be appreciated. The marshal function returned what I expected but the unmarshal returned an error ("data is not a jsonapi representation of '*jsonapi.Profile").

@mlitanin
Copy link

same issue here. Can't unmarshal the json to the struct with attributes that a objects.... Can't even find any workaround.

@jonathanmusto
Copy link

any update on this issue?

It seems we can't use arrays of simple types inside attributes either: https://go.dev/play/p/3qufy0gFk3u

glennsarti added a commit to hashicorp/go-tfe that referenced this issue Mar 20, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Mar 20, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Mar 20, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Mar 20, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Mar 27, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Apr 2, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Apr 3, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
glennsarti added a commit to hashicorp/go-tfe that referenced this issue Apr 4, 2024
The global run tasks feature updates both the Organization and Workspace
Run tasks with new attributes. Unfortunately these new attributes exposed
an issue with the JSON API pacakge, that it doesn't support complex objects
(Custom maps or non-standard types) [1]. Instead we need to unmarshall to
a more simpler object and then we adapt the simpler object to our more
correct struct.

[1] google/jsonapi#74
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants