Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-132089 / 25.04 / Make sure ipv4/ipv6 interface objects are properly dumped #12

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading