waargonaut icon indicating copy to clipboard operation
waargonaut copied to clipboard

ByteString.Char8 encoder

Open hasufell opened this issue 5 years ago • 6 comments

I'm surprised there exists no encoder for that: https://hackage.haskell.org/package/waargonaut-0.8.0.1/docs/Waargonaut-Encode.html#g:4

hasufell avatar Jan 12 '20 17:01 hasufell

I'm not sure that's really "issue" worthy, but I would happily accept a PR for such a thing. :+1:

import qualified Data.ByteString.Char8 as B8

encodeByteStringChar8 :: Applicative f => Encoder f B8.ByteString
encodeByteStringChar8 = B8.unpack >$< string

:)

mankyKitty avatar Jan 13 '20 10:01 mankyKitty

Well, this converts to string. I was wondering if there could be a more efficient low-level encoder.

hasufell avatar Jan 13 '20 11:01 hasufell

Quite possible, as ByteString.Char8 is limited to the latin1 charset which is a subset of what the JSON RFC allows. So you could see how well it goes to just drop it directly into the encoded structure.

Have you encountered any efficiency issues with Waargonaut?

mankyKitty avatar Jan 13 '20 16:01 mankyKitty

Have you encountered any efficiency issues with Waargonaut?

Not really, it's my first time trying to use it.

So you could see how well it goes to just drop it directly into the encoded structure.

Yeah, but the internal API is a bit complicated ;)

hasufell avatar Jan 13 '20 16:01 hasufell

Not really, it's my first time trying to use it.

Phew and yay!

Yeah, but the internal API is a bit complicated ;)

Yeah... it is a bit.. ahem :<

Part of this is due to a slightly crazy adherence to the RFC for JSON. Hence the wacky level of detail in the string types etc. As well as being able to support round trip parsing & printing, which meant trying to find a way to remember where all the whitespace is, and what type of whitespace it is.

mankyKitty avatar Jan 13 '20 17:01 mankyKitty

One option could be to change the type alias here https://github.com/qfpl/waargonaut/blob/master/src/Waargonaut/Types/JString.hs#L78 to be a sum type that is either the internal JString' representation or a narrower NonUnicode type... maybe:

data JString
  = JString' HeXDigit
  | NonUnicode Char8.ByteString

mankyKitty avatar Jan 13 '20 17:01 mankyKitty