haskell-waargonaut / waargonaut

JSON decoding/encoding/manipulation library.
Other
94 stars 14 forks source link

ByteString.Char8 encoder #74

Open hasufell opened 4 years ago

hasufell commented 4 years ago

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

mankyKitty commented 4 years ago

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

:)

hasufell commented 4 years ago

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

mankyKitty commented 4 years ago

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?

hasufell commented 4 years ago

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

mankyKitty commented 4 years ago

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 commented 4 years ago

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