#1 2015-06-15 10:21:08

hnb
Member
Registered: 2015-06-15
Posts: 290

More human readable way for TDocVariant

Hi :)

mORMot is amazing, thanks for great work.

I have small problem. How can I save TDocVariant in a more human readable way?

I need this for settings. For example:

var
  cfg: Variant;
begin
  TDocVariant.New(cfg);

  cfg.Host := eHost.Text;
  cfg.Port := ePort.Text;
  cfg.DBName := eDBName.Text;
  cfg.User := eUser.Text;
  cfg.Passwd := ePasswd.Text;

  FileFromString(string(cfg), 'settings.json');
end;

best regards,
Maciej Izak


best regards,
Maciej Izak

Offline

#2 2015-06-15 13:59:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,662
Website

Re: More human readable way for TDocVariant

Use JSONReformatToFile().

And you should better use VariantSaveJSON(cfg) instead of plain string(cfg), which makes a conversion from UTF-8 to UTF-16, then back to Ansi, which is not a good idea.

  JsonReformatToFile(VariantSaveJSON(cfg),'settings.json');

So everything stays with a single RawUTF8 variable.

Offline

#3 2015-06-16 08:34:54

hnb
Member
Registered: 2015-06-15
Posts: 290

Re: More human readable way for TDocVariant

Thanks. Works like a charm.


best regards,
Maciej Izak

Offline

Board footer

Powered by FluxBB