표준 델파이 시리얼 라이저를 사용하여 표준 델파이 컨테이너를 직렬화/비 직렬화하려고합니다.deserialized TDictionary가 제대로 작동하지 않는 이유는 무엇입니까?
procedure TForm7.TestButtonClick(Sender: TObject);
var
dict: TDictionary<Integer, Integer>;
jsonValue: TJSONValue;
begin
//serialization
dict := TDictionary<Integer, Integer>.Create;
dict.Add(1, 1);
jsonValue := TJsonConverter.ObjectToJSON(dict);
dict.Free;
//deserialization
dict := TJsonConverter.JSONToObject(jsonValue) as TDictionary<Integer, Integer>;
try
Assert(dict.ContainsKey(1), 'deserialization error - key not found');
except
Assert(false, 'deserialization error - dict object broken');
end;
end;
개체를 JSON으로 또는 그 반대로 변환하는 방법이 있습니다.
class function TJsonConverter.JSONToObject(AJSONValue: TJSONValue): TObject;
var
lUnMarshal: TJSONUnMarshal;
begin
lUnMarshal := TJSONUnMarshal.Create();
try
Result := lUnMarshal.Unmarshal(AJSONValue);
finally
lUnMarshal.Free;
end;
end;
class function TJsonConverter.ObjectToJSON(AData: TObject): TJSONValue;
var
lMarshal: TJSONMarshal;
begin
lMarshal := TJSONMarshal.Create();
try
Result := lMarshal.Marshal(AData);
finally
lMarshal.Free;
end;
end;
라인 :
dict := TJsonConverter.JSONToObject(jsonValue) as TDictionary<Integer, Integer>;
가 제대로 사전을 만들지 않습니다. [
![Dictionary created correctly[1]](https://i.stack.imgur.com/8Svps.png)
여기 직렬화에 의해 DICT 작성 : 여기 은 DICT 생성자로 만드는 모습입니다
내가 그것을 어떻게 해결할 수 있습니까?
편집
: 여기 이 문제는TJSONMarshal
는 RTTI를 사용하여 사전을 인스턴스화된다는 것이다 JSON 내용
{
"type" : "System.Generics.Collections.TDictionary<System.Integer,System.Integer>",
"id" : 1,
"fields" : {
"FItems" : [
[ -1, 0, 0 ],
[ -1, 0, 0 ],
[ -1, 0, 0 ],
[ 911574339, 1, 1 ]
],
"FCount" : 1,
"FGrowThreshold" : 3,
"FKeyCollection" : null,
"FValueCollection" : null
}
}
당신은 JSON 컨텐츠를 추가 할 수 있습니까? – mjn