From ecc7ccef60e68d3fd765a68d84b1e37adf0784dc Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Wed, 30 Oct 2024 12:28:11 +0500 Subject: [PATCH 1/2] Make sure ipv4/ipv6 interface objects are properly dumped --- truenas_api_client/ejson.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/truenas_api_client/ejson.py b/truenas_api_client/ejson.py index 721afa4..4dd768b 100644 --- a/truenas_api_client/ejson.py +++ b/truenas_api_client/ejson.py @@ -17,6 +17,7 @@ """ import calendar from datetime import date, datetime, time, timedelta, timezone +from ipaddress import IPv4Interface, IPv6Interface import json @@ -49,6 +50,8 @@ def default(self, obj): return {'$time': str(obj)} elif isinstance(obj, set): return {'$set': list(obj)} + elif isinstance(obj, (IPv4Interface, IPv6Interface)): + return str(obj) return super(JSONEncoder, self).default(obj) From 6f98c6d110822afc91d768ac05181516dacc010b Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Wed, 30 Oct 2024 12:32:31 +0500 Subject: [PATCH 2/2] Make sure ipv4/ipv6 interface gets loaded back correctly --- truenas_api_client/ejson.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/truenas_api_client/ejson.py b/truenas_api_client/ejson.py index 4dd768b..205d458 100644 --- a/truenas_api_client/ejson.py +++ b/truenas_api_client/ejson.py @@ -50,8 +50,10 @@ def default(self, obj): return {'$time': str(obj)} elif isinstance(obj, set): return {'$set': list(obj)} - elif isinstance(obj, (IPv4Interface, IPv6Interface)): - return str(obj) + elif isinstance(obj, IPv4Interface): + return {'$ipv4_interface': str(obj)} + elif isinstance(obj, IPv6Interface): + return {'$ipv6_interface': str(obj)} return super(JSONEncoder, self).default(obj) @@ -69,6 +71,10 @@ def object_hook(obj: dict): return time(*[int(i) for i in obj['$time'].split(':')[:4]]) # type: ignore if '$set' in obj: return set(obj['$set']) + if '$ipv4_interface' in obj: + return IPv4Interface(obj['$ipv4_interface']) + if '$ipv6_interface' in obj: + return IPv6Interface(obj['$ipv6_interface']) if obj_len == 2 and '$type' in obj and '$value' in obj: if obj['$type'] == 'date': return date(*[int(i) for i in obj['$value'].split('-')])