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

Support custom types for IDs and attributes #114

Open
xcoulon opened this issue Sep 13, 2017 · 3 comments
Open

Support custom types for IDs and attributes #114

xcoulon opened this issue Sep 13, 2017 · 3 comments

Comments

@xcoulon
Copy link

xcoulon commented Sep 13, 2017

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.

xcoulon added a commit to xcoulon/jsonapi that referenced this issue Sep 13, 2017
This applies to:
- ID
- Attributes
- Relationships

Custom types need to be registered before usage, using the new
`jsonapi.RegisterType()` function which takes 3 arguments:
- the type to support (`reflect.Type`)
- the function to use when marshalling a response
- the function to use when unmarshalling a response

Example:
````
RegisterType(uuidType,
  func(value interface{}) (string, error) {
    result := value.(*UUID).String()
    return result, nil
  },
  func(value string) (interface{}, error) {
    return UUIDFromString(value)
  })
````

The custom type will be represented as a `string` in the JSON document
in the requests and responses.

Fixes google#114

Signed-off-by: Xavier Coulon <[email protected]>
xcoulon added a commit to xcoulon/jsonapi that referenced this issue Sep 13, 2017
This applies to:
- ID
- Attributes
- Relationships

Custom types need to be registered before usage, using the new
`jsonapi.RegisterType()` function which takes 3 arguments:
- the type to support (`reflect.Type`)
- the function to use when marshalling a response
- the function to use when unmarshalling a response

Example:
````
RegisterType(uuidType,
  func(value interface{}) (string, error) {
    result := value.(*UUID).String()
    return result, nil
  },
  func(value string) (interface{}, error) {
    return UUIDFromString(value)
  })
````

The custom type will be represented as a `string` in the JSON document
in the requests and responses.

Fixes google#114

Signed-off-by: Xavier Coulon <[email protected]>
xcoulon added a commit to xcoulon/jsonapi that referenced this issue Sep 13, 2017
This applies to:
- ID
- Attributes
- Relationships

Custom types need to be registered before usage, using the new
`jsonapi.RegisterType()` function which takes 3 arguments:
- the type to support (`reflect.Type`)
- the function to use when marshalling a response
- the function to use when unmarshalling a response

Example:
````
RegisterType(uuidType,
  func(value interface{}) (string, error) {
    result := value.(*UUID).String()
    return result, nil
  },
  func(value string) (interface{}, error) {
    return UUIDFromString(value)
  })
````

The custom type will be represented as a `string` in the JSON document
in the requests and responses.

Fixes google#114

Signed-off-by: Xavier Coulon <[email protected]>
@xcoulon
Copy link
Author

xcoulon commented 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 ?

shwoodard pushed a commit to shwoodard/jsonapi that referenced this issue Nov 9, 2017
This applies to:
- ID
- Attributes
- Relationships

Custom types need to be registered before usage, using the new
`jsonapi.RegisterType()` function which takes 3 arguments:
- the type to support (`reflect.Type`)
- the function to use when marshalling a response
- the function to use when unmarshalling a response

Example:
````
RegisterType(uuidType,
  func(value interface{}) (string, error) {
    result := value.(*UUID).String()
    return result, nil
  },
  func(value string) (interface{}, error) {
    return UUIDFromString(value)
  })
````

The custom type will be represented as a `string` in the JSON document
in the requests and responses.

Fixes google#114

Signed-off-by: Xavier Coulon <[email protected]>
@tprost
Copy link

tprost commented Apr 3, 2020

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?

@aren55555
Copy link
Contributor

aren55555 commented Apr 9, 2020

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.

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

3 participants