You are not logged in.
Pages: 1
Thanks very much!now it works, that json string can be translated to TDictionary<double,string> This is my code :
TDstring = TDictionary<double, string>;
class procedure c_method.CustomReadClass(var Context: TJsonParserContext; Instance: TObject);
var
doc: TDocVariantData;
begin
if Context.ParseObject then
begin
if not assigned(Instance) then
Instance := TDstring.Create;
TDstring(Instance).Clear;
var s : u8string := add_bracket(Context.json); //'{ ' + Context.json + ' }'
doc.InitJson(s);
var nameList := doc.Names;
for var i :integer := 0 to High(nameList) do
begin
TDstring(Instance).TryAdd(To_string(nameList[i]).ToDouble, doc.s[nameList[i]]);
end;
Context.ParseEndOfObject;
Context.Valid := true;
end;
end;
initialization
TRttiJson.RegisterCustomSerializerClass(TDstring, c_method.CustomReadClass, nil);
finalization
TRttiJson.UnRegisterCustomSerializerClass(TDstring);
Thanks again!
I can parse any json string , get its key or value. If a json string will be translated to a record, then record must be define , And It's impossible to know the key string and the value in advance,Maybe can RegisterCustomSerializer for it?
Just like:
TPairString = TPair<double, string>;
TPairstrings = TArray<TPairString>;
PPairstrings = ^TPairstrings;
TDictStringMethod = class
public
class procedure CustomReader(var Context: TJsonParserContext; data: pointer);
class procedure CustomWriter(W: TJsonWriter; data: pointer; Options: TTextWriterWriteObjectOptions);
end;
initialization
TRttiJson.RegisterCustomSerializer(typeinfo(PPairstrings), TdictStringMethod.CustomReader, TdictStringMethod.CustomWriter);
Can this be work?
Yes , You are right. I can save and load the TSynDictionary , But I can't use a json string , such as :"{"name":"mark","data":[1,2,3,4,5],"some":{"11.1":"Hello","22.2":",","44.4":"World"}}".
It is so sad~
Maybe I must translater a std::multimap to a std::vector and save it, then I'm able to load and use it in delphi.
thank you first! i try it use TIKeyValue<integer, string> or TSynDictionary, but it can not work yet.i view the code, TIKeyValue implemented by two member: "Key" and "Value", it seems no way~
when i use mormot/delphi to deserialization a json from c++, there has some string which Serializated from std::map<int,std::string>,such as{"1":"Hello","2":"world"},but i hasn't struct to receive it!
so in delphi i try to serialization a example data,such as: Tarray<Tpair<integer,string>>,but the result is {[{"Key":1,"Value":"Hello"},{"Key":2,"Value":"World"}]}.
how can i deserialization "{"1":"Hello","2":"world"}" and use it in delphi??
thanks!
Pages: 1