One of the useful settings storage components that comes with Delphi XE6 Firemonkey is TIniFile (and TMemIniFile). The format of an INI file is basically sections that contain name value pairs. You can have multiple sections and then multiple name value pairs in each section. In order to make the data you are storing in INI files more versatile you may want to be able to convert your INI file format into JSON format and back again. I use the XSuperObject library for handling the JSON half of the conversion because it works on Android, IOS, OSX, and Windows. It should also work in Delphi XE5 and AppMethod as well. There have been some updates to XSuperObject as of early May 2014 so if you have an earlier version you should update it. Here are four functions which will do the conversion for you. Two of them are for reading and writing a JSON array to and from an IniFile and two of them are for reading and writing a JSON object to and from an IniFile. The functions are not veriy extensive because they really only handle one layer of JSON objects. If you want to handle more than one layer or if you want to handle subsections in INI files you’ll have to extend the functions yourself. Additionally, the functions support a limited number of datatypes but I usually just use strings everywhere in my INI files and JSON data.
procedure JSONObjectToIniFile(aobj: ISuperObject; Filename: String);
var
IniFile: TMemIniFile;
I, II: Integer;
obj, obj2: ISuperObject;
begin
IniFile := TMemIniFile.Create(Filename);
for I := 0 to aobj.Count-1 do
begin
obj2 := aobj.O[aobj.CurrentKey];
for II := 0 to obj2.Count-1 do
begin
case obj2.CurrentValue.DataType of
TDataType.dtString:
IniFile.WriteString(aobj.CurrentKey,obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtInteger:
IniFile.WriteInteger(aobj.CurrentKey,obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtBoolean:
IniFile.WriteBool(aobj.CurrentKey,obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtFloat:
IniFile.WriteFloat(aobj.CurrentKey,obj2.CurrentKey,obj2.CurrentValue.AsVariant);
else
IniFile.WriteString(aobj.CurrentKey,obj2.CurrentKey,obj2.CurrentValue.AsVariant);
end;
obj2.Next;
end;
obj2 := nil;
aobj.Next;
end;
IniFile.UpdateFile;
IniFile.Free;
end;
procedure JSONArrayToIniFile(aobj: ISuperArray; Filename: String);
var
IniFile: TMemIniFile;
I, II: Integer;
obj, obj2: ISuperObject;
begin
IniFile := TMemIniFile.Create(Filename);
for I := 0 to aobj.Length-1 do
begin
obj2 := aobj.O[I];
for II := 0 to obj2.Count-1 do
begin
case obj2.CurrentValue.DataType of
TDataType.dtString:
IniFile.WriteString(IntToStr(I),obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtInteger:
IniFile.WriteInteger(IntToStr(I),obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtBoolean:
IniFile.WriteBool(IntToStr(I),obj2.CurrentKey,obj2.CurrentValue.AsVariant);
TDataType.dtFloat:
IniFile.WriteFloat(IntToStr(I),obj2.CurrentKey,obj2.CurrentValue.AsVariant);
else
IniFile.WriteString(IntToStr(I),obj2.CurrentKey,obj2.CurrentValue.AsVariant);
end;
obj2.Next;
end;
obj2 := nil;
end;
IniFile.UpdateFile;
IniFile.Free;
end;
function IniFileToJSONObject(Filename: String): String;
var
IniFile: TMemIniFile;
I, II: Integer;
SL, SL2: TStringList;
aobj: ISuperObject;
obj, obj2: ISuperObject;
begin
IniFile := TMemIniFile.Create(Filename);
SL := TStringList.Create;
SL2 := TStringList.Create;
IniFile.ReadSections(SL);
aobj := SO;
for I := 0 to SL.Count-1 do
begin
SL2.Clear;
IniFile.ReadSectionValues(SL[I],SL2);
obj2 := SO;
for II := 0 to SL2.Count-1 do
begin
obj2.V[SL2.Names[II]] := SL2.ValueFromIndex[II];
end;
aobj.O[SL[I]] := obj2;
obj2 := nil;
end;
SL2.Free;
SL.Free;
IniFile.Free;
Result := aobj.AsJSON;
end;
function IniFileToJSONArray(Filename: String): String;
var
IniFile: TMemIniFile;
I, II: Integer;
SL, SL2: TStringList;
aobj: ISuperArray;
obj, obj2: ISuperObject;
begin
IniFile := TMemIniFile.Create(Filename);
SL := TStringList.Create;
SL2 := TStringList.Create;
IniFile.ReadSections(SL);
aobj := SA;
for I := 0 to SL.Count-1 do
begin
SL2.Clear;
IniFile.ReadSectionValues(SL[I],SL2);
obj2 := SO;
for II := 0 to SL2.Count-1 do
begin
obj2.V[SL2.Names[II]] := SL2.ValueFromIndex[II];
end;
aobj.Add(obj2);
obj2 := nil;
end;
SL2.Free;
SL.Free;
IniFile.Free;
Result := aobj.AsJSON;
end;
Head over and download the latest version of XSuperObject which is required to use the JSON to INI file functions.
1 Comment