17 using Newtonsoft.Json;
18 using Newtonsoft.Json.Linq;
21 using System.Collections.Generic;
30 private static PyObject _linkSerializationMethod;
40 using var _ = Py.GIL();
42 var instance = type.Invoke();
45 if (!
string.IsNullOrEmpty(data))
52 foreach (var kvp
in JsonConvert.DeserializeObject<Dictionary<string, object>>(data))
54 if (kvp.Value is JArray jArray)
56 SetProperty(kvp.Key, jArray.ToObject<List<object>>());
58 else if (kvp.Value is JObject jobject)
60 SetProperty(kvp.Key, jobject.ToObject<Dictionary<string, object>>());
77 using var _ = Py.GIL();
79 return result.GetAndDispose<
bool?>();
92 using var _ = Py.GIL();
93 if (_linkSerializationMethod ==
null)
95 var module = PyModule.FromString(
"python_serialization",
@"from json import dumps
96 from inspect import getmembers
98 def serialize(target):
99 if isinstance(target, dict):
102 if not hasattr(target, '__dict__') or not target.__dict__:
103 # python command inheriting base Command
104 members = getmembers(target)
106 for name, value in members:
107 if value and not name.startswith('__'):
108 potential_entry = str(value)
109 if not potential_entry.startswith('<bound '):
112 # pure python command object
113 return dumps(target.__dict__)
115 _linkSerializationMethod = module.GetAttr(
"serialize");
117 using var strResult = _linkSerializationMethod.Invoke(command);
119 return strResult.As<
string>();