Last active
May 26, 2023 02:26
-
-
Save stuncloud/38786fec654b0522839ca764aa6c97d8 to your computer and use it in GitHub Desktop.
UWSC用Jsonモジュールjson2.jsはその都度ファイルから読み出すか、textblockにコピペしとくと良い
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 使用方法 | |
// 1. json2.jsを以下からダウンロードしてください | |
// https://github.com/douglascrockford/JSON-js | |
// 2. 以下のいずれかの方法でjson2.jsを読み取れるようにする | |
// - json2.jsを実行スクリプトと同じフォルダに置く (逐次読み込み) | |
// - json2.jsの内容をjson2というtextblockにコピペする (埋め込み) | |
// 埋め込む場合はこの中にjson2.jsの中身をペースト | |
textblock json2 | |
endtextblock | |
module JSON | |
procedure JSON | |
ScriptControl = createoleobj("ScriptControl") | |
json2_code = _load_json2() | |
with ScriptControl | |
.Language = "JScript" | |
.ExecuteStatement(json2_code) | |
.ExecuteStatement(jsStatement) | |
CodeObject = .CodeObject | |
endwith | |
fend | |
dim ScriptControl,CodeObject | |
function Parse(str) | |
try | |
result = CodeObject.JSON.parse(str) | |
except | |
result = NOTHING | |
endtry | |
fend | |
function Stringify(json, indent = "", CRLF = FALSE) | |
try | |
result = CodeObject.JSON.stringify(json, null, indent) | |
if CRLF then | |
result = replace(result, chr(10), "<#CR>") | |
endif | |
except | |
result = EMPTY | |
endtry | |
fend | |
function AddObject(base, name, value) | |
try | |
CodeObject.Add(base, name, value) | |
result = TRUE | |
except | |
result = FALSE | |
endtry | |
fend | |
function ReadFromFile(path) | |
result = EMPTY | |
if ! fopen(path, F_EXISTS) then exit | |
fid = fopen(path, F_READ) | |
str = fget(fid, F_ALLTEXT) | |
fclose(fid) | |
result = Parse(str) | |
fend | |
function SaveToFile(path, json, indent = "", writemode = F_WRITE8) | |
result = FALSE | |
str = Stringify(json, indent) | |
if str = EMPTY then exit | |
fid = fopen(path, writemode) | |
if fid = -1 then exit | |
fput(fid, str, F_ALLTEXT) | |
if ! fclose(fid) then exit | |
result = TRUE | |
fend | |
textblock jsStatement | |
// 配列アクセス用 | |
// 第二引数に値を渡した場合は代入、省略した場合は値取得 | |
Array.prototype.Item = function(i, value) | |
{ | |
if (value === undefined) | |
return this[i]; | |
this[i] = value; | |
} | |
Array.prototype.item = Array.prototype.Item; | |
Array.prototype.ToSafeArray = function() | |
{ | |
var dictionary = new ActiveXObject("Scripting.Dictionary"); | |
for (var i = 0; i < this.length; i++) { | |
dictionary.add(i, this[i]); | |
} | |
return dictionary.Items(); | |
} | |
// Object追加用、AddObject()で使う | |
function Add(obj, name, value) | |
{ | |
obj[name] = value; | |
} | |
endtextblock | |
const json2_file = "json2.js" | |
function _load_json2() | |
if length(trim(json2)) > 0 then | |
result = json2 | |
else | |
if fopen(json2_file, F_EXISTS) then | |
fid = fopen(json2_file, F_READ) | |
result = fget(fid, F_ALLTEXT) | |
fclose(fid) | |
else | |
ws = createoleobj("WScript.Shell") | |
text = "json2.jsファイルを実行スクリプトと同じフォルダに置くか<#CR>内容をjson2というtextblockにコピーしてください" | |
ws.popup(text, 0, "JSONモジュールエラー: json2がありません", 16) | |
exitexit 1 | |
endif | |
endif | |
fend | |
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
call Module_Json.uws | |
textblock jsonString | |
{ | |
"foo":{ | |
"bar":"abcde", | |
"baz":[ | |
123, | |
456 | |
] | |
} | |
} | |
endtextblock | |
// 文字列をパース | |
obj = JSON.Parse(jsonString) | |
print obj.foo.bar | |
obj.foo.bar = "vwxyz" // 代入 | |
print obj.foo.bar | |
print obj.foo.baz.Item(0) // 配列の読み書きはItem()メソッドを使う | |
obj.foo.baz.Item(0, 789) // 配列の要素に代入する場合は第二引数を渡す | |
print obj.foo.baz.item(0) // Item()は頭文字を小文字にしてもOK | |
// 配列をSafeArrayにする | |
for v in obj.foo.baz.ToSafeArray() | |
print v | |
next | |
// オブジェクトの追加 | |
JSON.AddObject(obj.foo, "qux", "( ‘(I¥‘)") | |
// 文字列に戻す | |
print JSON.Stringify(obj, 2, TRUE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment