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

Skipping an element in an array due to unmatched type #194

Open
sivalingamr opened this issue Jul 20, 2020 · 0 comments
Open

Skipping an element in an array due to unmatched type #194

sivalingamr opened this issue Jul 20, 2020 · 0 comments

Comments

@sivalingamr
Copy link

When I tried to unmarshal the JSON API payload (contain unmatched type error) to a specific model (mentioned in the below source code), I was not getting any error and also it is not unmarshalling the struct data which contain an error. It was just skipping those data and moved to the next step.

Source code:

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "io/ioutil"

    "github.com/google/jsonapi"
)

type template struct {
    Temp      string          `json:"temp" jsonapi:"attr,temp"`
    ID        string          `json:"id" jsonapi:"primary,jsonApiTest"`
    Parameter []paramTemplate `json:"parameters" jsonapi:"attr,parameters"`
}

type paramTemplate struct {
    Name  string `json:"name" jsonapi:"attr,name"`
    Value string `json:"value" jsonapi:"attr,value"`
}

func main() {
    dataBytes, _ := ioutil.ReadFile("data.json")
    fmt.Println(string(dataBytes))
    templateModel := &template{}
    err := jsonapi.UnmarshalPayload(bytes.NewReader(dataBytes), templateModel)
    if err != nil {
        fmt.Println("Error occured while unmarshal: ")
    }

    jsonBytes, err := json.Marshal(templateModel)
    fmt.Println(string(jsonBytes))
}

Input data:

{
"data": {
"type": "jsonApiTest",
"id": "1",
"attributes": {
"temp": "Test",
"parameters": [
{
"name": "Default slot type",
"value": "Default"
},
{
"name": "Default Session Category",
"value": 13
}
]
}
}
}

Output Data:
{"temp":"Test","id":"1","parameters":[{"name":"Default slot type","value":"Default"}]}

Just have a look at the input data, I am providing one of the parameter's value (which was expected to be a string) as a number. Look at the output data, you could see that it was just skipped that element in a parameters array.

Could someone please help me out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant