You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
The library only supports string, int(8,16,32,64) or uint(8,16,32,64) for ID and other non-complex struct attributes, but it would be nice to have a mean to support other types, such as UUIDs, although they require the use of third-party libraries such as https://github.com/satori/go.uuid. This would save the developers to have to convert from string to UUID in the their code.
Since it's not possible to support all third party libraries in https://github.com/google/jsonapi, a solution could be to provide a public function to register custom types that can be marshalled to/unmarshalled from a string.
@aren55555, may I ask if could you give a look at my PR (#115) and let me know if it's something you'd be interested in including in the library, please ?
Why is it necessary to have developers register the type? Perhaps any types that implement MarshalJSON() and UnmarshalJSON(), or even just String() should be usable out of the box?
I'll elaborate on what @tprost mentioned: an interface based approach (similar to what Go's standard encodijng/json library supports) would be the ideal way to solve this problem. See json.Marshaler and json.Unmarshaler for reference.
Granted for the UUID type @xcoulon mentioned, you'd need to embed this in your own custom type and implement the interface, or ask the package maintainer to support implementing the interface. However I think it's a bit farfetched to expect every package's custom types to support the interfaces you desire. Based on the Godoc it appears the UUID type doesn't even implement the standard encoding/json lib's json.Marshaler or json.Unmarshaler interfaces, and it's doubtful they would implement an interface declared by this jsonapi package.
@aren55555 What is the proposed solution to use uuids and jsonapi? I tried to implement my own type with MarshalJSON, UnmarshalJSON & String, however, never got it to work.
This is my starting point:
package example
import (
"fmt"
"net/http"
"github.com/google/jsonapi"
"github.com/google/uuid"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type DBUsers struct {
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" jsonapi:"primary,users"`
}
func GetUsers(w http.ResponseWriter, r *http.Request) {
db, _ := gorm.Open(postgres.Open("postgres://..."), &gorm.Config{})
users := []*DBUsers{}
db.Find(&users)
if err := jsonapi.MarshalPayload(w, users); err != nil {
respondWithError(w, http.StatusInternalServerError, "Failed to marshal payload: "+err.Error())
}
}
It always returns: Failed to marshal payload: id should be either string, int(8,16,32,64) or uint(8,16,32,64)
Activity
Support custom types (google#114)
Support custom types (google#114)
Support custom types (google#114)
xcoulon commentedon Sep 26, 2017
@aren55555, may I ask if could you give a look at my PR (#115) and let me know if it's something you'd be interested in including in the library, please ?
Support custom types (google#114)
tprost commentedon Apr 3, 2020
Why is it necessary to have developers register the type? Perhaps any types that implement
MarshalJSON()andUnmarshalJSON(), or even justString()should be usable out of the box?aren55555 commentedon Apr 9, 2020
I'll elaborate on what @tprost mentioned: an interface based approach (similar to what Go's standard
encodijng/jsonlibrary supports) would be the ideal way to solve this problem. Seejson.Marshalerandjson.Unmarshalerfor reference.Granted for the UUID type @xcoulon mentioned, you'd need to embed this in your own custom type and implement the interface, or ask the package maintainer to support implementing the interface. However I think it's a bit farfetched to expect every package's custom types to support the interfaces you desire. Based on the Godoc it appears the
UUIDtype doesn't even implement the standardencoding/jsonlib'sjson.Marshalerorjson.Unmarshalerinterfaces, and it's doubtful they would implement an interface declared by thisjsonapipackage.fosple commentedon May 4, 2025
@aren55555 What is the proposed solution to use uuids and jsonapi? I tried to implement my own type with MarshalJSON, UnmarshalJSON & String, however, never got it to work.
This is my starting point:
It always returns:
Failed to marshal payload: id should be either string, int(8,16,32,64) or uint(8,16,32,64)Caused by
jsonapi/response.go
Line 21 in 1e07b10