Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

pascalscript - Inno Setup: Working with JSON

How can I load and work with JSON config file during install time? I can read string from file and write it, but if I want to change some value in config file, I have to use VBScript.RegExp COM object (which is good, but painful and slow to develop).

Current method:

ExtractTemporaryFile('config.json');
filename := ExpandConstant('{tmp}config.json');
LoadStringFromFile(filename, conf);

objRegExp := CreateOleObject('VBScript.RegExp');
objRegExp.Pattern := 'test';
conf := objRegExp.Replace(conf, 'test_replace');
SaveStringToFile(filenameOut, conf, False);

Is there a better way to do this? All I need is to replace some values in JSON object, no extra magic.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've setup the new project called Inno JSON Config, which allows you to work with simple JSON config files like shown below and which allows you to read and write string, integer and boolean values:

{
    "Section_1":{
            "Key_1": "String 1",
            "Key_2": "1",
            "Key_3": "True"
    },
    "Section_2":{
            "Key_1": "String 2",
            "Key_2": "2",
            "Key_3": "False"
    }
}

The usage is quite straightforward (even when I'm going to add handle support as well). Note, that only Unicode Inno Setup (in one of the most recent versions due to a required Int64 support) can be used:

[Files]
Source: "JSONConfig.dll"; Flags: dontcopy

[Code]
function JSONQueryString(FileName, Section, Key, Default: WideString;
  var Value: WideString; var ValueLength: Integer): Boolean;
  external 'JSONQueryString@files:jsonconfig.dll stdcall';
function JSONQueryBoolean(FileName, Section, Key: WideString; 
  Default: Boolean; var Value: Boolean): Boolean;
  external 'JSONQueryBoolean@files:jsonconfig.dll stdcall';
function JSONQueryInteger(FileName, Section, Key: WideString; 
  Default: Int64; var Value: Int64): Boolean;
  external 'JSONQueryInteger@files:jsonconfig.dll stdcall';
function JSONWriteString(FileName, Section, Key, 
  Value: WideString): Boolean;
  external 'JSONWriteString@files:jsonconfig.dll stdcall';
function JSONWriteBoolean(FileName, Section, Key: WideString;
  Value: Boolean): Boolean;
  external 'JSONWriteBoolean@files:jsonconfig.dll stdcall';
function JSONWriteInteger(FileName, Section, Key: WideString;
  Value: Int64): Boolean;
  external 'JSONWriteInteger@files:jsonconfig.dll stdcall';

function BoolToStr(Value: Boolean): string;
begin
  Result := 'True';
  if not Value then
    Result := 'False';
end;

procedure InitializeWizard;
var
  FileName: WideString;
  IntValue: Int64;
  StrValue: WideString;
  StrLength: Integer;
  BoolValue: Boolean;
begin
  { set the source JSON config file path }
  FileName := 'c:Example.json';
  { allocate string buffer to enough length }
  SetLength(StrValue, 16);
  { set the buffer length value }
  StrLength := Length(StrValue);
  { query string value }
  if JSONQueryString(FileName, 'Section_1', 'Key_1', 'Default', StrValue, 
    StrLength)
  then
    MsgBox('Section_1:Key_1=' + StrValue, mbInformation, MB_OK);
  { query integer value }
  if JSONQueryInteger(FileName, 'Section_1', 'Key_2', 0, IntValue) then
    MsgBox('Section_1:Key_2=' + IntToStr(IntValue), mbInformation, MB_OK);
  { query boolean value }
  if JSONQueryBoolean(FileName, 'Section_1', 'Key_3', True, BoolValue) then
    MsgBox('Section_1:Key_3=' + BoolToStr(BoolValue), mbInformation, MB_OK);
  { write string }
  if not JSONWriteString(FileName, 'Section_1', 'Key_1', 'New value!') then
    MsgBox('JSONWriteString Section_1:Key_1 failed!', mbError, MB_OK);
  { write integer }
  if not JSONWriteInteger(FileName, 'Section_1', 'Key_2', 123) then
    MsgBox('JSONWriteInteger Section_1:Key_2 failed!', mbError, MB_OK);
  { write boolean }
  if not JSONWriteBoolean(FileName, 'Section_1', 'Key_3', False) then
    MsgBox('JSONWriteBoolean Section_1:Key_3 failed!', mbError, MB_OK);
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...