Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Support custom types for IDs and attributes #114

Open

Description

@xcoulon

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.

Activity

  1. xcoulon commented on Sep 26, 2017

    @xcoulon
    Author

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

  2. added a commit that references this issue on Nov 9, 2017
  3. tprost commented on Apr 3, 2020

    @tprost

    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?

  4. aren55555 commented on Apr 9, 2020

    @aren55555
    Contributor

    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.

  5. fosple commented on May 4, 2025

    @fosple

    @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)

    Caused by

    "id should be either string, int(8,16,32,64) or uint(8,16,32,64)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions