MarkReedZ / mrhttp

The fastest python web framework - 8.5m requests per second!
MIT License
24 stars 6 forks source link

Add a json member to the request? #4

Closed ShermanGirl closed 6 years ago

ShermanGirl commented 6 years ago

When running the 3_request.py example I see the json is available from r.body as a byte string. Can we add r.json with it preloaded as an object [1,2,3...] ?

Body:  b'[1,2,3,4,5,6,7,8,9]'
MarkReedZ commented 6 years ago

Thanks for pointing this out.

I added the following to the request. I will commit this tomorrow. I'm assuming utf-8. Perhaps I'll update this to check for a charset=something else in the Content-Type header before committing. I'll have to benchmark it since utf-8 should cover most everyone.

  @property
  def json(self):
    if self._json == None:
      try:
        if self.mime_type == "application/json":
          self._json = json.loads(self.body.decode("utf-8"))
      except:
        pass
    return self._json
MarkReedZ commented 6 years ago

Committed as above