inno-setup - 创新设置 : Working with JSON

标签 inno-setup pascalscript

如何在安装期间加载和使用 JSON 配置文件?我可以从文件中读取字符串并写入它,但是如果我想更改配置文件中的某些值,我必须使用 VBScript.RegExp COM 对象(这很好,但开发起来很痛苦且缓慢)。

当前方法:

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);

有一个更好的方法吗?我所需要的只是替换 JSON 对象中的一些值,没有额外的魔法。

最佳答案

我已经设置了名为 Inno JSON Config 的新项目,它允许您使用如下所示的简单 JSON 配置文件,并允许您读取和写入字符串、整数和 bool 值:

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

用法非常简单(即使我还要添加句柄支持)。请注意,只能使用 Unicode Inno Setup(由于需要 Int64 支持,在最新版本之一中)可以使用:

[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;

关于inno-setup - 创新设置 : Working with JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15335340/

相关文章:

inno-setup - Inno Setup 在appendChild msxml 中崩溃

inno-setup - 如何从 Inno Setup Pascal 脚本设置版本号

installation - 在Inno Setup中执行已安装的批处理文件

inno-setup - 获取临时文件夹

.net - “检查”功能在 Inno Setup 中多次执行

inno-setup - 下次执行 Inno Setup 制作的安装程序时,恢复之前在自定义页面上输入的数据

c# - 你知道我可以在 C# 中使用的 Inno Setup 包装器吗?

delphi - TStringList ValueFromIndex 在 PascalScript 中不起作用

windows - Inno Setup 脚本中的基本 IP 验证

delphi - 无法从 PascalScript 调用 Get_ProcAddress