It fails because the dictionary in the result contains a byte string
b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local'
. A solution is to decode it to unicode with the utf8 encoding. Here is working code that I found with my search engine
result = (True, {'result': 0, 'description': 'success', 'dn': '', 'message': '', 'referrals': None, 'type': 'searchResDone'}, [{'raw_dn': b'CN=user G\xc3\xbc\xc3\xa7l\xc3\xbc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'dn': 'CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local', 'raw_attributes': {}, 'attributes': {}, 'type': 'searchResEntry'}, {'uri': ['ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local'], 'type': 'searchResRef'}, {'uri': ['ldap://TRD.local/CN=Configuration,DC=TRD,DC=local'], 'type': 'searchResRef'}], {'base': 'dc=my,dc=server', 'scope': 2, 'dereferenceAlias': 3, 'sizeLimit': 0, 'timeLimit': 0, 'typesOnly': False, 'filter': '(sAMAccountName=user1)', 'attributes': ['1.1'], 'type': 'searchRequest', 'controls': None})
import json
class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return str(obj, encoding='utf-8');
return json.JSONEncoder.default(self, obj)
s = json.dumps(result, cls=MyEncoder, indent=4)
print(s)
Output:
[
true,
{
"result": 0,
"description": "success",
"dn": "",
"message": "",
"referrals": null,
"type": "searchResDone"
},
[
{
"raw_dn": "CN=user G\u00fc\u00e7l\u00fc,OU=DEVELOPERS,OU=ANKARA,OU=TURKIYE,OU=TRD
-GLOBAL,DC=TRD,DC=local",
"dn": "CN=User1,OU=DEVELOPER,OU=ANKARA,OU=TURKIYE,OU=TRD-GLOBAL,DC=TRD,DC=local",
"raw_attributes": {},
"attributes": {},
"type": "searchResEntry"
},
{
"uri": [
"ldap://ForestDnsZones.TRD.local/DC=ForestDnsZones,DC=TRD,DC=local"
],
"type": "searchResRef"
},
{
"uri": [
"ldap://DomainDnsZones.TRD.local/DC=DomainDnsZones,DC=TRD,DC=local"
],
"type": "searchResRef"
},
{
"uri": [
"ldap://TRD.local/CN=Configuration,DC=TRD,DC=local"
],
"type": "searchResRef"
}
],
{
"base": "dc=my,dc=server",
"scope": 2,
"dereferenceAlias": 3,
"sizeLimit": 0,
"timeLimit": 0,
"typesOnly": false,
"filter": "(sAMAccountName=user1)",
"attributes": [
"1.1"
],
"type": "searchRequest",
"controls": null
}
]
You could perhaps serialize only result[1] because I think the True value at the start of the result is not part of the result's payload. Also note that this solution may encounter other issues if the result contains bytes data encoded in another encoding than utf8 or raw bytes data that don't represent unicode strings.