Remove frozendict_json_encoder and support frozendicts everywhere

Not being able to serialise `frozendicts` is fragile, and it's annoying to have
to think about which serialiser you want. There's no real downside to
supporting frozendicts, so let's just have one json encoder.
This commit is contained in:
Richard van der Hoff 2020-10-28 15:51:15 +00:00
parent fedfdfd750
commit b6ca69e4f1
7 changed files with 32 additions and 38 deletions

View file

@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from frozendict import frozendict
@ -49,23 +47,3 @@ def unfreeze(o):
pass
return o
def _handle_frozendict(obj):
"""Helper for EventEncoder. Makes frozendicts serializable by returning
the underlying dict
"""
if type(obj) is frozendict:
# fishing the protected dict out of the object is a bit nasty,
# but we don't really want the overhead of copying the dict.
return obj._dict
raise TypeError(
"Object of type %s is not JSON serializable" % obj.__class__.__name__
)
# A JSONEncoder which is capable of encoding frozendicts without barfing.
# Additionally reduce the whitespace produced by JSON encoding.
frozendict_json_encoder = json.JSONEncoder(
allow_nan=False, separators=(",", ":"), default=_handle_frozendict,
)