Skip to content

Commit

Permalink
Merge pull request #12 from truenas/dump-ip-objects
Browse files Browse the repository at this point in the history
NAS-132089 / 25.04 / Make sure ipv4/ipv6 interface objects are properly dumped
  • Loading branch information
sonicaj authored Oct 30, 2024
2 parents cbb4d15 + 6f98c6d commit 66b567c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions truenas_api_client/ejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""
import calendar
from datetime import date, datetime, time, timedelta, timezone
from ipaddress import IPv4Interface, IPv6Interface
import json


Expand Down Expand Up @@ -49,6 +50,10 @@ def default(self, obj):
return {'$time': str(obj)}
elif isinstance(obj, set):
return {'$set': list(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)


Expand All @@ -66,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('-')])
Expand Down

0 comments on commit 66b567c

Please sign in to comment.