NSJSON是NSIS操作JavaScript对象表示法的语法分析器插件

/*
NSJSON是NSIS操作JavaScript对象表示法的语法分析器插件
NSJSON将格式不规范的文本字符进行语法分析然后生成规范的JSON文本
NSJSON直接将二进制流(nsdata)转化成字典1,字典1的value也是字典,设为字典2. 利用for in 可以遍历输出字典2中所有的value值.
*/
!AddPluginDir "../../Plugins"
!include MUI2.nsh
Name `nsJSON plug-in`
OutFile Example.exe
RequestExecutionLevel user
ShowInstDetails show
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_LANGUAGE English
Section
 CreateDirectory $EXEDIROutput
SectionEnd
Section `Parse Example1.json` EXAMPLE1
 ; Input: Example1.json; output: Example1.json.
 nsJSON::Set /file $EXEDIRExample1.json
 nsJSON::Serialize /format /file $EXEDIROutputExample1.json
 DetailPrint `Generate: $EXEDIROutputExample1.json`
SectionEnd
Section `Convert Example1_Unicode.json` EXAMPLE1B
 ; Use the /unicode switch if the input file is Unicode.
 nsJSON::Set /file /unicode $EXEDIRExample1_Unicode.json
 ; Generate an ANSII output file.
 nsJSON::Serialize /format /file $EXEDIROutputExample1_ASCII.json
 DetailPrint `Generate: $EXEDIROutputExample1_ASCII.json`
 ; Generate a Unicode output file.
 nsJSON::Serialize /format /file /unicode $EXEDIROutputExample1_Unicode.json
 DetailPrint `Generate: $EXEDIROutputExample1_Unicode.json`
SectionEnd
Section `Parse Example2.json` EXAMPLE2
 ; Input: Example2.json; output: Example2.json.
 nsJSON::Set /file $EXEDIRExample2.json
 nsJSON::Serialize /format /file $EXEDIROutputExample2.json
 DetailPrint `Generate: $EXEDIROutputExample2.json`
SectionEnd
Section `Parse Example3.json` EXAMPLE3
 ; Input: Example3.json; output: Example3.json.
 nsJSON::Set /file $EXEDIRExample3.json
 nsJSON::Serialize /format /file $EXEDIROutputExample3.json
 DetailPrint `Generate: $EXEDIROutputExample3.json`
SectionEnd
Section `Parse Example4.json` EXAMPLE4
 ; Input: Example4.json; output: Example4.json.
 nsJSON::Set /file $EXEDIRExample4.json
 nsJSON::Serialize /format /file $EXEDIROutputExample4.json
 DetailPrint `Generate: $EXEDIROutputExample4.json`
SectionEnd
Section `Parse Example5.json` EXAMPLE5
 ; Input: Example5.json; output: Example5.json.
 nsJSON::Set /file $EXEDIRExample5.json
 nsJSON::Serialize /format /file $EXEDIROutputExample5.json
 DetailPrint `Generate: $EXEDIROutputExample5.json`
SectionEnd
Section `Generate Example6.json` EXAMPLE6
 nsJSON::Set /value `{}`
 nsJSON::Set `html` `head` `title` /value `"Example6"`
 ; Build an array using individual calls.
 nsJSON::Set `html` `body` `h1` /value `[]`
 nsJSON::Set `html` `body` `h1` /value `"Hello, my name is"`
 nsJSON::Set `html` `body` `h1` `i` /value `"Stuart."`
 nsJSON::Set `html` `body` `h1` /value `"Howdy"`
 nsJSON::Set `html` `body` `h1` /value `"!"`
 ; Build an array using a JSON string.
 nsJSON::Set `html` `body` `h2` /value `[ "I like", { "u": { "i" : "programming" } }, "very much!" ]`
 ; Quotes in node keys are allowed; they are escaped automatically.
 nsJSON::Set `html` `body` `a href="http://www.afrowsoft.co.uk"` /value `"My website!"`
 ; Open the file below in Notepad.
 nsJSON::Serialize /format /file $EXEDIROutputExample6.json
 DetailPrint `Generate: $EXEDIROutputExample6.json`
SectionEnd
Section `Reading from Example1.json` EXAMPLE7
 nsJSON::Set /file $EXEDIRExample1.json
 ; Read quoted string value.
 ClearErrors
 nsJSON::Get `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossTerm` /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossTerm = $R0`
 ${EndIf}
 ; Read quoted string value with escaping.
 ClearErrors
 nsJSON::Set `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `para2` /value `"A meta-markup language, used to create markup languagesrnsuch as DocBook."`
 nsJSON::Get `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `para2` /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->para2 = $R0`
 ${EndIf}
 ; Read quoted string value without expanding escape sequences.
 ClearErrors
 nsJSON::Set `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `para3` /value `"A meta-markup language, used to create markup languagesrnsuch as DocBook."`
 nsJSON::Get /noexpand `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `para3` /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->para3 = $R0`
 ${EndIf}
 ; Read the value of an array (returns a comma delimited list).
 ClearErrors
 nsJSON::Get `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `GlossSeeAlso` /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->GlossSeeAlso = $R0`
 ${EndIf}
 ; Try reading a node that does not exist.
 ClearErrors
 nsJSON::Get `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `GlossSeeAlso2` /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->GlossSeeAlso2 = $R0`
 ${Else}
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->GlossSeeAlso2 = Does not exist!`
 ${EndIf}
 ; Try reading an array element that does not exist.
 ClearErrors
 nsJSON::Get `glossary` `GlossDiv` `GlossList` `GlossEntry` `GlossDef` `GlossSeeAlso` 99 /end
 ${IfNot} ${Errors}
 Pop $R0
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->GlossSeeAlso->#99 = $R0`
 ${Else}
 DetailPrint `glossary->GlossDiv->GlossList->GlossEntry->GlossDef->GlossSeeAlso->#99 = Does not exist!`
 ${EndIf}
SectionEnd
Section `Arrays test` EXAMPLE8
 nsJSON::Set /value `{}`
 ; You can add an array this way.
 nsJSON::Set `array1` /value `[ "value 1", "value 2", { "value 3": "node value" }, "value 4", 5 ]`
 ; Inspect array1.
 nsJSON::Get `array1` /end
 Pop $R0
 DetailPrint `array1 = [$R0]`
 ; Or you can build it this way.
 nsJSON::Set `array2` /value `[]`
 nsJSON::Set `array2` /value `"value 1"`
 nsJSON::Set `array2` /value `"value 2"`
 nsJSON::Set `array2` `value 3` /value `"node value"`
 nsJSON::Set `array2` /value `"value 4"`
 nsJSON::Set `array2` /value `5`
 ; You cannot add the same value again.
 nsJSON::Set `array2` /value `5`
 ; Inspect array2.
 nsJSON::Get `array2` /end
 Pop $R0
 DetailPrint `array2 = [$R0]`
 ; Does an array element exist at the given index?
 nsJSON::Get /exists `array2` /index 0 /end
 Pop $R0
 DetailPrint `array2[0] exists? = $R0`
 ; Does an array element exist that matches the value?
 nsJSON::Get /exists `array2` `5` /end
 Pop $R0
 DetailPrint `array2->5 exists? = $R0`
 ; Does an array element exist at the given index?
 nsJSON::Get /exists `array2` /index 6 /end
 Pop $R0
 DetailPrint `array2[6] exists? = $R0`
 ; Open Example8_1.json to see what it now looks like.
 nsJSON::Serialize /format /file $EXEDIROutputExample8_1.json
 DetailPrint `Generate: $EXEDIROutputExample8_1.json`
 ; Now delete the element at the given index.
 nsJSON::Delete `array1` /index 2 `value 3` /end
 DetailPrint `Delete: array1[2]->value 3`
 nsJSON::Delete `array2` /index 5 /end
 DetailPrint `Delete: array2[5]`
 ; Now delete the elements with the given values.
 nsJSON::Delete `array1` `value 1` /end
 DetailPrint `Delete: array1->value 1`
 nsJSON::Delete `array2` `value 2` /end
 DetailPrint `Delete: array2->value 2`
 ; Inspect array1.
 nsJSON::Get `array1` /end
 Pop $R0
 DetailPrint `array1 = [$R0]`
 ; Inspect array2.
 nsJSON::Get `array2` /end
 Pop $R0
 DetailPrint `array2 = [$R0]`
 ; Open Example8_2.json to see what it now looks like.
 nsJSON::Serialize /format /file $EXEDIROutputExample8_2.json
 DetailPrint `Generate: $EXEDIROutputExample8_2.json`
SectionEnd
Section `Node iteration test` EXAMPLE9
 nsJSON::Set /file $EXEDIRExample4.json
 ; Get the node count.
 nsJSON::Get /count `web-app` `servlet` /index 0 `init-param` /end
 Pop $R0
 DetailPrint `Node web-app->servlet[0]->init-param contains $R0 children:`
 ${For} $R1 0 $R0
 nsJSON::Get /key `web-app` `servlet` /index 0 `init-param` /index $R1 /end
 Pop $R2
 nsJSON::Get `web-app` `servlet` /index 0 `init-param` /index $R1 /end
 Pop $R3
 nsJSON::Get /type `web-app` `servlet` /index 0 `init-param` /index $R1 /end
 Pop $R4
 DetailPrint `$R2 = $R3 (type: $R4)`
 ${Next}
SectionEnd
Section `Load Example5.json into Example4.json` EXAMPLE10
 ; Input: Example5.json; output: Example5.json.
 nsJSON::Set /file $EXEDIRExample5.json
 nsJSON::Set `menu` `example4` /file $EXEDIRExample4.json
 nsJSON::Serialize /format /file $EXEDIROutputExample10.json
 DetailPrint `Generate: $EXEDIROutputExample10.json`
SectionEnd
Section `Copies Preferences.json into PreferencesNew.json` EXAMPLE11
 ; Input: Preferences.json; output: PreferencesNew.json.
 nsJSON::Set /file $EXEDIRPreferences.json
 nsJSON::Serialize /format /file $EXEDIROutputPreferencesNew.json
 DetailPrint `Generate: $EXEDIROutputPreferencesNew.json`
SectionEnd
Section `Copies Preferences2.json into Preferences2New.json` EXAMPLE12
 ; Input: PreferencesNew.json; output: Preferences2New.json.
 nsJSON::Set /file $EXEDIRPreferences2.json
 nsJSON::Serialize /format /file $EXEDIROutputPreferences2New.json
 DetailPrint `Generate: $EXEDIROutputPreferences2New.json`
SectionEnd
LangString Example1Desc ${LANG_ENGLISH} `Parses Example1.json and then generates OutputExample1.json`
LangString Example1BDesc ${LANG_ENGLISH} `Parses Example1_Unicode.json and then generates Unicode and ASCII copies`
LangString Example2Desc ${LANG_ENGLISH} `Parses Example2.json and then generates OutputExample2.json`
LangString Example3Desc ${LANG_ENGLISH} `Parses Example3.json and then generates OutputExample3.json`
LangString Example4Desc ${LANG_ENGLISH} `Parses Example4.json and then generates OutputExample4.json`
LangString Example5Desc ${LANG_ENGLISH} `Parses Example5.json and then generates OutputExample5.json`
LangString Example6Desc ${LANG_ENGLISH} `Generates OutputExample6.json using Parse, Set and Serialize`
LangString Example7Desc ${LANG_ENGLISH} `Parses Example1.json and then reads values from the tree using Get`
LangString Example8Desc ${LANG_ENGLISH} `Tests JSON array manipulation while generating OutputExample8.json`
LangString Example9Desc ${LANG_ENGLISH} `Iterates through some nodes in Example4.json by index`
LangString Example10Desc ${LANG_ENGLISH} `Parses Example5.json into Example4.json and then generates OutputExample10.json`
LangString Example11Desc ${LANG_ENGLISH} `Parses Preferences.json and then generates OutputPreferencesNew.json`
LangString Example12Desc ${LANG_ENGLISH} `Parses Preferences2.json and then generates OutputPreferences2New.json`
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE1} $(Example1Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE1B} $(Example1BDesc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE2} $(Example2Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE3} $(Example3Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE4} $(Example4Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE5} $(Example5Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE6} $(Example6Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE7} $(Example7Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE8} $(Example8Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE9} $(Example9Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE10} $(Example10Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE11} $(Example11Desc)
 !insertmacro MUI_DESCRIPTION_TEXT ${EXAMPLE12} $(Example12Desc)
!insertmacro MUI_FUNCTION_DESCRIPTION_END
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容