diff mbox series

[ovs-dev,v1,10/18] python: add a json encoder to flow fields

Message ID 20211122112256.2011194-11-amorenoz@redhat.com
State Changes Requested
Headers show
Series python: add flow parsing library | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test fail github build: failed

Commit Message

Adrian Moreno Nov. 22, 2021, 11:22 a.m. UTC
The json encoder can be used to convert Flows to json

Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
---
 python/ovs/flows/decoders.py | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Eelco Chaudron Dec. 23, 2021, 1:49 p.m. UTC | #1
Except for two dot’s it looks good, so please add my Acked-by if you fix those ;)

On 22 Nov 2021, at 12:22, Adrian Moreno wrote:

> The json encoder can be used to convert Flows to json

Add dot.

>
> Signed-off-by: Adrian Moreno <amorenoz@redhat.com>
> ---
>  python/ovs/flows/decoders.py | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/python/ovs/flows/decoders.py b/python/ovs/flows/decoders.py
> index 3def9f279..96bb56c06 100644
> --- a/python/ovs/flows/decoders.py
> +++ b/python/ovs/flows/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
>
> @@ -451,3 +452,17 @@ 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

Add a dot.

> +    """
> +
> +    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)
> -- 
> 2.31.1
diff mbox series

Patch

diff --git a/python/ovs/flows/decoders.py b/python/ovs/flows/decoders.py
index 3def9f279..96bb56c06 100644
--- a/python/ovs/flows/decoders.py
+++ b/python/ovs/flows/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
 
@@ -451,3 +452,17 @@  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)