2013-03-11 14 views
6

के साथ काम करना मैं इंस्टॉल समय के दौरान JSON कॉन्फ़िगरेशन फ़ाइल के साथ कैसे लोड और काम कर सकता हूं? मैं फ़ाइल से स्ट्रिंग पढ़ सकता हूं और इसे लिख सकता हूं, लेकिन अगर मैं कॉन्फ़िगरेशन फ़ाइल में कुछ मान बदलना चाहता हूं, तो मुझे VBScript.RegExp COM ऑब्जेक्ट (जो अच्छा है, लेकिन दर्दनाक और विकसित करने में धीमा) का उपयोग करना होगा।इनो सेटअप: 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); 

वहाँ यह करने के लिए एक बेहतर तरीका है? मुझे बस जेएसओएन ऑब्जेक्ट में कुछ मूल्यों को प्रतिस्थापित करना है, कोई अतिरिक्त जादू नहीं है।

+0

Inno किसी भी देशी JSON समर्थन नहीं है, लेकिन आप को पार्स करने और के रूप में यह संशोधित कर सकते हैं इसे फिर से लिखने से पहले एक सामान्य स्ट्रिंग। यदि एक regexp मॉड्यूल आपके लिए सबसे आसान तरीका है तो यह हो। – Deanna

+1

यदि आप किसी ज्ञात-अद्वितीय मार्कर को खोजना और प्रतिस्थापित करना चाहते हैं, तो 'स्ट्रिंग चेंज' या 'स्ट्रिंग चेंजएक्स' फ़ंक्शन का उपयोग करें। नियमित अभिव्यक्तियों की कोई आवश्यकता नहीं है जबतक कि आप खोज टेक्स्ट को अद्वितीय नहीं बना सकते। – Miral

उत्तर

9

मैं सेटअप जो नीचे दिखाया गया है जैसे आप सरल JSON config फाइलों के साथ काम करने की अनुमति देता 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" 
    } 
} 

उपयोग काफी सरल है (यहां तक ​​कि जब मैं हैंडल समर्थन भी जोड़ रहा हूं)। ध्यान दें, कि केवल यूनिकोड Inno (एक आवश्यक 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

+0

आप बहुत बढ़िया हैं! आपका बहुत बहुत धन्यवाद! भविष्य में संदर्भ के लिए – phantasm

+0

: https://code.google.com/p/superobject/ – phantasm