Efficient way to modify response json in plug

I have a plug that abstracts injecting some data in all json responses. I have following plug that uses register_before_send but its very inefficient. Is there better way I can skip decoding and re-encoding json.

register_before_send(conn, fn conn ->
      case conn.assigns[:data] do
        # pattern match is done on data wrt struct
        data ->
          res = Poison.decode!(conn.resp_body)
          |> Map.put(:data, data)
          |> Poison.encode!

          resp(conn, conn.status, res)

        _ -> conn
      end
    end)

:wave:

Have you tried adding a plug in the end of your pipeline? Then the body would still be a map there, I think.

body is saved as map ??? I didn’t find anything about that in doc. Can you please point me to it?

The plug itself is at the end. AFAIK plug is meant to change conn and the response, and the resp_body is already seralized when callback is called.