Repository: JoshData/python-email-validator

Issue:
TypeError: Object of type ValidatedEmail is not JSON serializable
As can be seen in `main()`:

https://github.com/JoshData/python-email-validator/blob/cc9b0019fc9d4cedb491fd760e092572858d4604/email_validator/__init__.py#L526-L527

It looks like running `email_validator` as a standalone module can only produce runtime errors due to the inability to serialize ValidatedEmail to a JSON object.

The easy fix is to use `result.__dict__` instead of `result`, which produces a dict representation of the class. 

I actually have another PR in the works that does some code cleanup functions, but wanted to capture it as an independent issue in case anyone else goes looking for this bug.

Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=h1) Report
> Merging [#49](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=desc) into [primary](https://codecov.io/gh/JoshData/python-email-validator/commit/458c9c49422eeac92903e4e7d2865b1256b361d6&el=desc) will **decrease** coverage by `0.63%`.
> The diff coverage is `33.33%`.

[![Impacted file tree graph](https://codecov.io/gh/JoshData/python-email-validator/pull/49/graphs/tree.svg?width=650&height=150&src=pr&token=HH5hPlDZRR)](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=tree)

```diff
@@             Coverage Diff             @@
##           primary      #49      +/-   ##
===========================================
- Coverage    69.86%   69.23%   -0.64%     
===========================================
  Files            1        1              
  Lines          219      221       +2     
===========================================
  Hits           153      153              
- Misses          66       68       +2     
```


| [Impacted Files](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [email\_validator/\_\_init\_\_.py](https://codecov.io/gh/JoshData/python-email-validator/pull/49/diff?src=pr&el=tree#diff-ZW1haWxfdmFsaWRhdG9yL19faW5pdF9fLnB5) | `69.23% <33.33%> (-0.64%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=footer). Last update [458c9c4...3b9ca41](https://codecov.io/gh/JoshData/python-email-validator/pull/49?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/email_validator/__init__.py b/email_validator/__init__.py
index 3b7014f..40140ca 100644
--- a/email_validator/__init__.py
+++ b/email_validator/__init__.py
@@ -156,6 +156,10 @@ class ValidatedEmail(object):
                        ) \
             + ")"
 
+    """Convenience method for accessing ValidatedEmail as a dict"""
+    def as_dict(self):
+        return self.__dict__
+
 
 def validate_email(
     email,
@@ -536,7 +540,7 @@ def main():
             email = email.decode("utf8")  # assume utf8 in input
         try:
             result = validate_email(email, allow_smtputf8=allow_smtputf8, check_deliverability=check_deliverability)
-            print(json.dumps(result, indent=2, sort_keys=True, ensure_ascii=False))
+            print(json.dumps(result.as_dict(), indent=2, sort_keys=True, ensure_ascii=False))
         except EmailNotValidError as e:
             if sys.version_info < (3,):
                 print(unicode_class(e).encode("utf8"))


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
index 7d5fe4b..d0c627a 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -250,6 +250,13 @@ def test_email_invalid(email_input, error_msg):
     assert str(exc_info.value) == error_msg
 
 
+def test_dict_accessor():
+    input_email = "testaddr@example.com"
+    valid_email = validate_email(input_email, check_deliverability=False)
+    assert isinstance(valid_email.as_dict(), dict)
+    assert valid_email.as_dict()["original_email"] == input_email
+
+
 def test_deliverability_no_records():
     assert validate_email_deliverability('example.com', 'example.com') == {'mx': [(0, '')], 'mx-fallback': None}