2013-03-11 392 views
6

如何在安裝期間加載和使用JSON配置文件?我可以從文件中讀取字符串並將其寫入,但如果我想在配置文件中更改某個值,則必須使用VBScript.RegExp COM對象(這很好,但很痛苦且開發緩慢)。Inno Setup:使用JSON

電流法:

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對象中的一些值,沒有額外的魔法。

+0

的Inno沒有任何原生JSON支持,但你可以分析和修改它一個正常的字符串,然後再寫出來。如果一個正則表達式模塊對你來說是最簡單的方法,那就這樣吧。 – Deanna 2013-03-11 17:08:40

+1

如果您只想搜索並替換已知的唯一標記,那麼使用「StringChange」或「StringChangeEx」函數。除非你不能使搜索文本唯一,否則不需要正則表達式。 – Miral 2013-03-12 08:12:50

回答

9

我設置被叫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 '[email protected]:jsonconfig.dll stdcall'; 
function JSONQueryBoolean(FileName, Section, Key: WideString; 
    Default: Boolean; var Value: Boolean): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 
function JSONQueryInteger(FileName, Section, Key: WideString; 
    Default: Int64; var Value: Int64): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 
function JSONWriteString(FileName, Section, Key, 
    Value: WideString): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 
function JSONWriteBoolean(FileName, Section, Key: WideString; 
    Value: Boolean): Boolean; 
    external '[email protected]:jsonconfig.dll stdcall'; 
function JSONWriteInteger(FileName, Section, Key: WideString; 
    Value: Int64): Boolean; 
    external '[email protected]: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; 
+1

把它作爲初始版本,如果對它有興趣,它可能會長大。 – TLama 2013-03-12 01:50:10

+0

你真棒!非常感謝你! – phantasm 2013-03-12 10:03:01

+0

以供將來參考:https://code.google.com/p/superobject/ – phantasm 2013-03-12 10:12:14