2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 09:58:01 +00:00

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>
This commit is contained in:
Adrian Moreno 2022-07-08 20:03:09 +02:00 committed by Ilya Maximets
parent 7e588e82f0
commit 6a71bc09bb

View File

@ -5,6 +5,7 @@ A decoder is generally a callable that accepts a string and returns the value
object. object.
""" """
import json
import netaddr import netaddr
import re import re
@ -522,3 +523,16 @@ def decode_nat(value):
result[flag] = True result[flag] = True
return result 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)