diff options
Diffstat (limited to 'python/ovs/flow/decoders.py')
-rw-r--r-- | python/ovs/flow/decoders.py | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/python/ovs/flow/decoders.py b/python/ovs/flow/decoders.py index 73d28e057..7378d4176 100644 --- a/python/ovs/flow/decoders.py +++ b/python/ovs/flow/decoders.py @@ -5,6 +5,7 @@ A decoder is generally a callable that accepts a string and returns the value object. """ +import json import netaddr import re @@ -522,3 +523,16 @@ def decode_nat(value): result[flag] = True return result + + +class FlowEncoder(json.JSONEncoder): + """FlowEncoder is a json.JSONEncoder instance that can be used to + serialize flow fields.""" + + def default(self, obj): + if isinstance(obj, Decoder): + return obj.to_json() + elif isinstance(obj, netaddr.IPAddress): + return str(obj) + + return json.JSONEncoder.default(self, obj) |