파이썬 객체에 json 문자열을 비 직렬화하는 데 simplejson을 사용하고 있습니다. json을 내 도메인 객체로 역 직렬화하는 것을 담당하는 맞춤 작성된 object_hook이 있습니다.파이썬 객체에 거대한 json 문자열을 비 직렬화
내 json 문자열이 거대한 경우 (즉, 서버가 json 문자열 형태로 약 800,000 개의 도메인 객체를 반환하는 경우) 내 파이썬 디시리얼라이저가 약 10 분 동안 비 직렬화됩니다.
나는 조금 더 뚫어졌고, 단순한 것 같아. 그런데 많은 일을하지는 않고 모든 것을 object_hook에 위임하고있다. 내 object_hook 최적화를 시도했지만 너무 성능 향상되지 않습니다. (나는 거의 1 분 개선을 얻지 못했습니다)
내 질문에 거대한 데이터 세트를 처리하도록 최적화 된 다른 표준 프레임 워크가 있거나 object_hook 레벨에서 모든 작업을 수행하는 대신 프레임 워크의 기능을 활용할 수있는 방법이 있습니까? .
object_hook이 없으면 프레임 워크는 도메인 객체 목록이 아닌 사전 목록 만 반환합니다.
모든 포인터가 유용 할 것입니다. 내가 simplejson 버전 여기에 3.7.2
을 사용하고 참고
내 샘플 _object_hook입니다 :
def _object_hook(dct):
if '@CLASS' in dct: # server sends domain objects with this @CLASS
clsname = dct['@CLASS']
# This is like Class.forName (This imports the module and gives the class)
cls = get_class(clsname)
# As my server is in java, I convert the attributes to python as per python naming convention.
dct = dict((convert_java_name_to_python(k), dct[k]) for k in dct.keys())
if cls != None:
obj_key = None
if "@uuid"in dct
obj_key = dct["@uuid"]
del(dct["@uuid"])
else:
info("Class missing uuid: " + clsname)
dct.pop("@CLASS", None)
obj = cls(**dct) #This I found to be the most time consuming process. In my domian object, in the __init__ method I have the logic to set all attributes based on the kwargs passed
if obj_key is not None:
shared_objs[obj_key] = obj #I keep all uuids along with the objects in shared_objs dictionary. This shared_objs will be used later to replace references.
else:
warning("class not found: " + clsname)
obj = dct
return obj
else:
return dct
샘플 응답 :
{"@CLASS":"sample.counter","@UUID":"86f26a0a-1a58-4429-a762- 9b1778a99c82","val1":"ABC","val2":1131,"val3":1754095,"value4": {"@CLASS":"sample.nestedClass","@UUID":"f7bb298c-fd0b-4d87-bed8- 74d5eb1d6517","id":1754095,"name":"XYZ","abbreviation":"ABC"}}
내가 중첩과 수 많은 수준이 서버에서받는 레코드의 수는 800K 이상입니다.
흥미로운 것 같습니다. 어떤 샘플 스 니펫이라도 신속하게 확인하면 유용 할 것입니다. –
'object_hook' 함수의 코드와 구문 분석하려는 JSON의 샘플을 게시 할 수 있다면 질문에 답하는 데 도움이 될 것입니다. – jstlaurent