summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorAdrian Moreno <amorenoz@redhat.com>2022-07-08 20:03:09 +0200
committerIlya Maximets <i.maximets@ovn.org>2022-07-15 20:14:24 +0200
commit6a71bc09bb89d73cc4d270ab9e5407e7da6ea4f9 (patch)
tree1965c74e2bff4f00dccdfc1b842bc5c606f1f93b /python
parent7e588e82f0663d0ef8ff777df8bc7111cc215657 (diff)
downloadopenvswitch-6a71bc09bb89d73cc4d270ab9e5407e7da6ea4f9.tar.gz
python: Add a json encoder to flow fields.
The json encoder can be used to convert Flows to json. Acked-by: Eelco Chaudron <echaudro@redhat.com> Signed-off-by: Adrian Moreno <amorenoz@redhat.com> Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Diffstat (limited to 'python')
-rw-r--r--python/ovs/flow/decoders.py14
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)