lostisland / faraday_middleware

Various Faraday middlewares for Faraday-based API wrappers
MIT License
556 stars 205 forks source link

Configurable JSON encoders and decoders #286

Closed ne006 closed 9 months ago

ne006 commented 9 months ago

By default FaradayMiddleware::EncodeJson and FaradayMiddleware::ParseJson uses json to encode/decode JSON data, although there are gems like oj which do the job faster than Ruby's built-in json.

This implementation allows to pass encoder option to FaradayMiddleware::EncodeJson and decoder option to pass in parser_options to FaradayMiddleware::ParseJson

require 'oj'

Faraday.new do |conn|
  conn.request :json, encoder: Oj
  conn.response :json, parser_options: { decoder: Oj }
end
iMacTia commented 9 months ago

Hi @ne006 and thank you for proposing this contribution. I like the idea and I've heard similar requests in the past as well. However, please note that as stated in the README faraday_middleware is now deprecated, so we're not accepting new feature PRs against it.

The EncodeJson and ParseJson middleware are now shipped out-of-the-box with Faraday, so if you'd like you can "move" this contribution directly to them 👍 . See the json request and json response middleware in Faraday.

If you reopen this PR against those, I'll be happy to review. Based on a quick review, I can say the PR looks good and has good test coverage, but we'd require the documentation to be updated as well before it can be merged, especially around the functionality of passing a class/method pair I see you implemented. You can find the json middleware (both request and response) documentation here.

Thank you again for the contribution and I look forward to reviewing it once moved on Faraday. For now, I'll be closing this PR for the reason mentioned at the beginning.