加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

inno-setup – Inno Setup:使用JSON

发布时间:2020-12-15 04:08:18 所属栏目:大数据 来源:网络整理
导读:如何在安装期间加载和使用 JSON配置文件?我可以从文件中读取字符串并写入它,但如果我想在配置文件中更改某些值,我必须使用VBScript.RegExp COM对象(这很好,但开发很痛苦但很慢). 目前的方法: ExtractTemporaryFile('config.json');filename := ExpandConst
如何在安装期间加载和使用 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配置文件,它允许您读取和写入字符串,整数和布尔值:
{
    "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,Key: WideString; 
  Default: Boolean; var Value: Boolean): Boolean;
  external 'JSONQueryBoolean@files:jsonconfig.dll stdcall';
function JSONQueryInteger(FileName,Key: WideString; 
  Default: Int64; var Value: Int64): Boolean;
  external 'JSONQueryInteger@files:jsonconfig.dll stdcall';
function JSONWriteString(FileName,Value: WideString): Boolean;
  external 'JSONWriteString@files:jsonconfig.dll stdcall';
function JSONWriteBoolean(FileName,Key: WideString;
  Value: Boolean): Boolean;
  external 'JSONWriteBoolean@files:jsonconfig.dll stdcall';
function JSONWriteInteger(FileName,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,'Key_2',IntValue) then
    MsgBox('Section_1:Key_2=' + IntToStr(IntValue),MB_OK);
  { query boolean value }
  if JSONQueryBoolean(FileName,'Key_3',True,BoolValue) then
    MsgBox('Section_1:Key_3=' + BoolToStr(BoolValue),MB_OK);
  { write string }
  if not JSONWriteString(FileName,'New value!') then
    MsgBox('JSONWriteString Section_1:Key_1 failed!',mbError,MB_OK);
  { write integer }
  if not JSONWriteInteger(FileName,123) then
    MsgBox('JSONWriteInteger Section_1:Key_2 failed!',MB_OK);
  { write boolean }
  if not JSONWriteBoolean(FileName,False) then
    MsgBox('JSONWriteBoolean Section_1:Key_3 failed!',MB_OK);
end;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读