#1 Re: mORMot 2 » How delphi to deserialization json as std::map » 2023-03-07 11:28:39

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!

#2 Re: mORMot 2 » How delphi to deserialization json as std::map » 2023-03-07 06:15:09

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?

#3 Re: mORMot 2 » How delphi to deserialization json as std::map » 2023-03-06 21:15:31

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.

#4 Re: mORMot 2 » How delphi to deserialization json as std::map » 2023-03-06 18:46:03

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~

#5 mORMot 2 » How delphi to deserialization json as std::map » 2023-03-06 16:59:29

chen ken
Replies: 8

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!

Board footer

Powered by FluxBB