1 # Unix SMB/CIFS implementation.
3 # encoders: JSONEncoder class for dealing with object fields.
5 # Copyright (C) Catalyst.Net Ltd. 2023
7 # Written by Rob van der Linde <rob@catalyst.net.nz>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 from datetime
import datetime
25 from decimal
import Decimal
28 from ldb
import Dn
, MessageElement
, Result
30 from samba
.dcerpc
.security
import descriptor
33 class JSONEncoder(json
.JSONEncoder
):
34 """Custom JSON encoder class to help out with some data types.
36 For example, the json module has no idea how to encode a Dn object to str.
37 Another common object that is handled is Decimal types.
39 In addition, any objects that have a __json__ method will get called.
42 def default(self
, obj
):
43 if isinstance(obj
, (Decimal
, Dn
, Exception, MessageElement
)):
45 if isinstance(obj
, Result
):
47 elif isinstance(obj
, Enum
):
49 elif isinstance(obj
, datetime
):
50 return obj
.isoformat()
51 elif isinstance(obj
, descriptor
):
53 elif getattr(obj
, "__json__", None) and callable(obj
.__json
__):
55 return super().default(obj
)