2013-03-11 11 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

イノは任意のネイティブJSONをサポートしていませんが、構文解析して、それを修正することができます(これは必要なInt64サポートへの最新バージョンのいずれかで)唯一のUnicode Inno Setupのを使用できることに、注意してください通常の文字列を返してから再度書き出します。正規表現モジュールがあなたのための最も簡単な方法であれば、そうなるでしょう。 – Deanna

+1

既知の一意マーカーを検索して置き換えたい場合は、 'StringChange'または' StringChangeEx'関数を使用してください。検索テキストを一意にすることができない限り、正規表現は必要ありません。 – Miral

答えて

9

私はセットアップ新しいプロジェクトは、あなたが以下に示し、あなたは、文字列、整数およびブール値を読み書きすることができますどのような単純なJSON設定ファイルを操作することを可能にする、Inno JSON Configと呼ばれました:

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

使い方はかなり簡単です(ハンドルのサポートも追加しています)。

[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

+0

あなたはとても素晴らしいです!どうもありがとうございました! – phantasm

+0

今後の参考として:https://code.google.com/p/superobject/ – phantasm

関連する問題