marten icon indicating copy to clipboard operation
marten copied to clipboard

feat: add JSON representation for Marten::HTTP::UploadedFile

Open miry opened this issue 2 months ago • 0 comments

This change introduces a to_json method to Marten::HTTP::UploadedFile, allowing uploaded files to be included in JSON representations such as request.data.to_json.

Previously, converting a request containing an uploaded file to JSON raised:

Error: expected argument #1 to 'Marten::HTTP::UploadedFile#to_json' to be IO, not JSON::Builder

The new implementation defines #to_json and #to_json(json : JSON::Builder), returning an object with the following fields:

  • original_filename
  • tempfile
  • content_type

This makes it possible to safely serialize uploaded files in request data.

I was inspired by ruby rack implementation (src):

puts Rack::Multipart::UploadedFile.new("/usr/bin/nvim").to_json
{"original_filename":"nvim","tempfile":"#<File:0x00007f2e4d386648>","content_type":"text/plain"}

miry avatar Nov 09 '25 11:11 miry