Repository: pydantic/pydantic

Issue:
json_encoders inheritance
# Feature Request

Currently, defining `json_encoders` in child classes completely overrides the one defined from the parent classes:
```py
from datetime import datetime, timedelta
from pydantic import BaseModel

class A(BaseModel):
    dt: datetime = datetime.now()
    class Config:
        json_encoders = {datetime: lambda _: "encoded_from_A"}

print(A().json())
#> {"dt": "encoded_from_A"}
class B(A):
    timedt: timedelta = timedelta(hours=100)
    class Config:
        json_encoders = {timedelta: lambda _: "encoded_from_B"}

print(B().json())
#> {"dt": "2020-10-23T01:11:20.613624", "timedt": "encoded_from_B"}

```

I think it would be handy to merge the JSON encoders with the one defined in the parent ones, thus the result would be:
```py
print(B().json())
#> {"dt": "encoded_from_A", "timedt": "encoded_from_B"}
```

As an extension, it should still be possible to override the parent ones:
```py
class Bbis(A):
    timedt: timedelta = timedelta(hours=100)

    class Config:
        json_encoders = {
            datetime: lambda _: "encoded_from_Bbis",
            timedelta: lambda _: "encoded_from_Bbis",
        }

print(Bbis().json()) 
#> {"dt": "encoded_from_Bbis", "timedt": "encoded_from_Bbis"}
```

I'm eager to implement this feature if you think it's worth it :smile: 


Discussion:

Resolving patch:
diff --git a/docs/examples/exporting_models_json_encoders_merge.py b/docs/examples/exporting_models_json_encoders_merge.py
new file mode 100644
--- /dev/null
+++ b/docs/examples/exporting_models_json_encoders_merge.py
@@ -0,0 +1,24 @@
+from datetime import datetime, timedelta
+from pydantic import BaseModel
+from pydantic.json import timedelta_isoformat
+
+
+class BaseClassWithEncoders(BaseModel):
+    dt: datetime
+    diff: timedelta
+
+    class Config:
+        json_encoders = {
+            datetime: lambda v: v.timestamp()
+        }
+
+
+class ChildClassWithEncoders(BaseClassWithEncoders):
+    class Config:
+        json_encoders = {
+            timedelta: timedelta_isoformat
+        }
+
+
+m = ChildClassWithEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100))
+print(m.json())
diff --git a/pydantic/main.py b/pydantic/main.py
--- a/pydantic/main.py
+++ b/pydantic/main.py
@@ -170,13 +170,19 @@ def prepare_field(cls, field: 'ModelField') -> None:
 
 
 def inherit_config(self_config: 'ConfigType', parent_config: 'ConfigType') -> 'ConfigType':
+    namespace = {}
     if not self_config:
         base_classes = (parent_config,)
     elif self_config == parent_config:
         base_classes = (self_config,)
     else:
         base_classes = self_config, parent_config  # type: ignore
-    return type('Config', base_classes, {})
+        namespace['json_encoders'] = {
+            **getattr(parent_config, 'json_encoders', {}),
+            **getattr(self_config, 'json_encoders', {}),
+        }
+
+    return type('Config', base_classes, namespace)
 
 
 EXTRA_LINK = 'https://pydantic-docs.helpmanual.io/usage/model_config/'


Test patch:
diff --git a/tests/test_json.py b/tests/test_json.py
--- a/tests/test_json.py
+++ b/tests/test_json.py
@@ -170,6 +170,35 @@ class Config:
     assert m.json() == '{"x": "P0DT0H2M3.000000S"}'
 
 
+def test_json_encoder_simple_inheritance():
+    class Parent(BaseModel):
+        dt: datetime.datetime = datetime.datetime.now()
+        timedt: datetime.timedelta = datetime.timedelta(hours=100)
+
+        class Config:
+            json_encoders = {datetime.datetime: lambda _: 'parent_encoder'}
+
+    class Child(Parent):
+        class Config:
+            json_encoders = {datetime.timedelta: lambda _: 'child_encoder'}
+
+    assert Child().json() == '{"dt": "parent_encoder", "timedt": "child_encoder"}'
+
+
+def test_json_encoder_inheritance_override():
+    class Parent(BaseModel):
+        dt: datetime.datetime = datetime.datetime.now()
+
+        class Config:
+            json_encoders = {datetime.datetime: lambda _: 'parent_encoder'}
+
+    class Child(Parent):
+        class Config:
+            json_encoders = {datetime.datetime: lambda _: 'child_encoder'}
+
+    assert Child().json() == '{"dt": "child_encoder"}'
+
+
 def test_custom_encoder_arg():
     class Model(BaseModel):
         x: datetime.timedelta