Description
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?