2013-03-11 6 views
6

설치하는 동안 JSON 구성 파일을 어떻게로드하고 사용할 수 있습니까? 내가 파일에서 문자열을 읽고 그것을 쓸 수 있지만 내가 설정 파일에서 일부 값을 변경하려면, 내가 VBScript.RegExp COM 개체 (좋은,하지만 고통스럽고 개발 느리게)를 사용해야합니다.Inno 설치 : 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 지원 최신 버전 중 하나) 유니 코드 Inno Setup으로 사용할 수 있음을 유의 정상적인 문자열을 다시 쓰기 전에. regexp 모듈이 당신을위한 가장 쉬운 방법이라면 그렇게 될 것입니다. – 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