How to write the FromJSON instance for CUid

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

How to write the FromJSON instance for CUid

PICCA Frederic-Emmanuel
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

David McBride
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 <[hidden email]> wrote:
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

PICCA Frederic-Emmanuel
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

PICCA Frederic-Emmanuel
> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

PICCA Frederic-Emmanuel
> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How to write the FromJSON instance for CUid

PICCA Frederic-Emmanuel
Thanks

Fred
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners