💾 Archived View for bbs.geminispace.org › s › pascal › 17048 captured on 2024-08-18 at 19:45:41. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2024-07-09)

🚧 View Differences

-=-=-=-=-=-=-

Since we're talking Pascal today, does anyone know a good way to persist objects to and from sqlite? Say, TDictionary?

Also the documentation on JSON is not good. In Python, you just convert the dict to json and write to the db, then pull it out amd assign back to the dict. Easy. Delphi may have a solution but I'm using Free Pascal.

Posted in: s/pascal

🍀 gritty

May 17 · 3 months ago

1 Comment

🐙 norayr [mod] · May 17 at 13:12:

this was a function i wrote recently to help my friend to get the ip via tor network:

procedure TForm1.Button1Click(Sender: TObject);
  var
    HTTP: TIdHTTP;
    Response, city, country: string;
    JSON: TJSONObject;
    SocksInfo: TIdSocksInfo;
    SSLHandler: TIdSSLIOHandlerSocketOpenSSL;
    IOHandlerStack: TIdIOHandlerStack;
  begin
    HTTP := TIdHTTP.Create(nil);
    SocksInfo := TIdSocksInfo.Create(nil);
    IOHandlerStack := TIdIOHandlerStack.Create(nil);
    try
      SocksInfo.Host := '127.0.0.1';
      SocksInfo.Port := 9050;
      SocksInfo.Version := svSocks5;

      IOHandlerStack.TransparentProxy := SocksInfo;
      // for http
      //IOHandlerStack.Handler := SocksInfo;

      // for https
      // SSLHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
      // IOHandlerStack.Handler := SSLHandler;
      // SSLHandler.TransparentProxy := SocksInfo;

      HTTP.IOHandler := IOHandlerStack;

      HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36';

      // Optionally set other headers
       //HTTP.Request.Accept := 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
       //HTTP.Request.AcceptLanguage := 'en-US,en;q=0.5';
       //HTTP.Request.AcceptEncoding := 'gzip, deflate';
    //HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36';
    //HTTP.Request.Accept := 'application/json'; // Since we're expecting JSON response
    //HTTP.Request.Connection := 'keep-alive';

      try
        Response := HTTP.Get('http://ipinfo.io?token=<YOURTOKEN>');
        JSON := TJSONObject(GetJSON(Response));
        try
          ShowMessage('Location: ' + JSON.Get('city', '') + ', ' + JSON.Get('country', ''));
          city := JSON.Get('city', ''); country := JSON.Get('country', '');
          StatusBar1.Panels[0].Text := 'Location: ' + city + ', ' + country ;

        finally
          JSON.Free;
        end;
      except
        on E: Exception do
          ShowMessage('Failed to retrieve location: ' + E.Message);
      end;
    finally
      HTTP.Free;
      IOHandlerStack.Free;
      SocksInfo.Free;

    end;
  end;

i was pleasantly surprised by how easy it was to work with json files.

json was a huge problem for me with oberon. and it was sort of solved when a member of #oberon chat on irc have been written a usable implementation. but that is much harder to use than this one.

i never worked with tdictionary, alas. i think freepascal forums are the best way to get help with what you want to do in this case.