
Hello, I try to write the instance for the CUid[1] type But I do not know what to do. instance FromJSON CUid where parseJSON = ... {-# INLINE parseJSON #-} Thansk for your help [1] https://hackage.haskell.org/package/base-4.11.1.0/docs/System-Posix-Types.ht...

You have to worry about json having a floating point number or an integer that is too large to be represented in a Word32, although you could also just let it overflow if you don't care too much. There's probably an easier way to do this, but this is what I came up with. instance FromJSON CUid where parseJSON (Number n) = do case floatingOrInteger n of Right i | inrange i -> return . CUid . fromIntegral $ i -- not an integer, or not in range _ -> mempty where inrange :: Integer -> Bool inrange i = fromIntegral i >= (minBound @Word32) && fromIntegral i <= (maxBound @Word32) -- not a number parseJSON _ = mempty On Thu, Aug 9, 2018 at 11:37 AM, PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello, I try to write the instance for the CUid[1] type But I do not know what to do.
instance FromJSON CUid where parseJSON = ... {-# INLINE parseJSON #-}
Thansk for your help
[1] https://hackage.haskell.org/package/base-4.11.1.0/docs/ System-Posix-Types.html#t:CUid _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Sorry I really should've tested my code before I hit send.
instance FromJSON CUid where
parseJSON (Number n) = do
case floatingOrInteger n of
Right i | inrange i -> return . CUid . fromIntegral $ i
_ -> mempty
where
inrange :: Integer -> Bool
inrange i = i >= fromIntegral (minBound @Word32) &&
i <= fromIntegral (maxBound @Word32)
parseJSON _ = mempty
On Thu, Aug 9, 2018 at 1:06 PM, David McBride
You have to worry about json having a floating point number or an integer that is too large to be represented in a Word32, although you could also just let it overflow if you don't care too much. There's probably an easier way to do this, but this is what I came up with.
instance FromJSON CUid where parseJSON (Number n) = do
case floatingOrInteger n of Right i | inrange i -> return . CUid . fromIntegral $ i
-- not an integer, or not in range _ -> mempty
where inrange :: Integer -> Bool inrange i = fromIntegral i >= (minBound @Word32) && fromIntegral i <= (maxBound @Word32)
-- not a number parseJSON _ = mempty
On Thu, Aug 9, 2018 at 11:37 AM, PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello, I try to write the instance for the CUid[1] type But I do not know what to do.
instance FromJSON CUid where parseJSON = ... {-# INLINE parseJSON #-}
Thansk for your help
[1] https://hackage.haskell.org/package/base-4.11.1.0/docs/Syste m-Posix-Types.html#t:CUid _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Hello, and thanks for the reply What about this instance FromJSON CGid where parseJSON x = CGid <$> (parseJSON x :: Parser Word32) {-# INLINE parseJSON #-} Do you think that it deal with all the problem you are trying to prevent ? Cheers Fred

Didn't even occur to me that there would already be a Word32 instance. Looking at the source, it absolutely deals with every case automatically. On Fri, Aug 10, 2018 at 4:05 AM, PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello, and thanks for the reply
What about this
instance FromJSON CGid where parseJSON x = CGid <$> (parseJSON x :: Parser Word32) {-# INLINE parseJSON #-}
Do you think that it deal with all the problem you are trying to prevent ?
Cheers
Fred _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Didn't even occur to me that there would already be a Word32 instance. Looking at the source, it absolutely deals with every case automatically.
Great, now the real question ;) I use Generic with aeson and I endup like plenty of other with these orphan instances definietion. what is the right way to deal with this. fill a bug against all these types and request instances for these typeclass. or ? Cheers Fred

I'm not sure if the maintainers would accept it. You should keep in mind that these posix types are not necessarily the same system to system as the word sizes are dependent on the architecture of the machine involved, thus different machines would have to generate different instances. If that is the case, it is probably fine to just live with orphan instances. But it would be pretty easy to ask them. On Fri, Aug 10, 2018 at 7:43 AM, PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Didn't even occur to me that there would already be a Word32 instance. Looking at the source, it absolutely deals with every case automatically.
Great,
now the real question ;)
I use Generic with aeson and I endup like plenty of other with these orphan instances definietion.
what is the right way to deal with this.
fill a bug against all these types and request instances for these typeclass.
or ?
Cheers
Fred _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I'm not sure if the maintainers would accept it.
You should keep in mind that these posix types are not necessarily the same system to system as the word sizes are dependent on the architecture of the machine involved, thus different machines would have to generate different instances. If that is the case, it is probably fine to just live > with orphan instances.
But it would be pretty easy to ask them.
After some reflection, is it better to ask the aeson maintainer for the instance of the maintainer of the CGid CUid maintainer ? Cheers Fred

Since the posix types are in base, and aeson already depends on base, you would ask the aeson maintaners. On Fri, Aug 10, 2018 at 9:00 AM, PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
I'm not sure if the maintainers would accept it.
You should keep in mind that these posix types are not necessarily the same system to system as the word sizes are dependent on the architecture of the machine involved, thus different machines would have to generate different instances. If that is the case, it is probably fine to just live with orphan instances.
But it would be pretty easy to ask them.
After some reflection, is it better to ask the aeson maintainer for the instance of the maintainer of the CGid CUid maintainer ?
Cheers
Fred _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
David McBride
-
PICCA Frederic-Emmanuel