XScript マニュアル · Chapter 34
JSON オブジェクト — JSON ドキュメント処理
スクリプトでキーワード JSON を通じてアクセスする JSON ユーティリティ。1 つのプロセスで 複数の JSON 文書 を名前で識別し、それぞれを path 表記(/root/items/0/name)で自由に読み書きします。
| 概念 | 説明 |
|---|---|
jsonName | ユーザが付ける文書識別子("recipe"、"mes_response" 等) |
path | 文書内の位置 — /key/key2/0/name(オブジェクトはキー、配列はインデックス) |
| 結果 / エラー | 全ての呼出が内部フラグを更新 — GetResult / GetErrorMessage |
基本フロー
// 1) 空の文書を生成
JSON.CreateJson("recipe");
// 2) 値を書く(中間 path は自動生成)
JSON.SetValue("recipe", "/title", "AlignRecipe");
JSON.SetValue("recipe", "/version", 2);
JSON.SetValue("recipe", "/exposure_ms", 18.5);
JSON.SetValue("recipe", "/use_filter", true);
// 3) シリアライズ
string text = JSON.ToJsonString("recipe");
Log(text);
// {"title":"AlignRecipe","version":2,"exposure_ms":18.5,"use_filter":true}
// 4) ディスクに保存
JSON.SaveJsonFile("recipe", "D:/Recipes/align.json");
// 5) 不要になったら除去
JSON.RemoveJson("recipe");文書ライフサイクル
JSON.LoadJsonFile("mes", "D:/Mes/inbound.json");
JSON.LoadJson ("inline", "{\"a\":1,\"b\":[2,3]}");
if( JSON.Contains("mes") ) JSON.ClearJsonData("mes");
JSON.RemoveJson("mes");
JSON.RemoveAll(); // 登録済の全文書を除去値の書き込み — SetValue(4 オーバーロード)
JSON.SetValue("doc", "/name", "Alice"); // string
JSON.SetValue("doc", "/age", 30); // int
JSON.SetValue("doc", "/weight", 62.4); // double
JSON.SetValue("doc", "/active", true); // bool
JSON.SetNullValue("doc", "/parent_id"); // 明示的に null存在しない path は 自動的に作成 されます — 先に AddObject を呼ぶ必要はありません。ただし配列インデックスに書く場合、配列自体は 事前に存在 している必要があります。
値の読み取り — GetXxxValue
string n = JSON.GetStringValue("doc", "/name"); // "Alice"
int a = JSON.GetIntValue ("doc", "/age"); // 30
double w = JSON.GetDoubleValue("doc", "/weight", -1.0); // 無ければ -1.0
bool v = JSON.GetBoolValue ("doc", "/active", false); // 既定値フォールバック第 3 引数(default value) — キーが無い・型が違うときその値を返します。
部分木の文字列取得
// 指定 path のノード以下の全 child key と値をまとめて文字列で
string sub = JSON.GetJsonStringByPath("doc", "/items/0");オブジェクト / 配列の追加
JSON.AddObject("doc", "/", "items"); // /items を空オブジェクトに
JSON.AddObject("doc", "/items", "first"); // /items/first を空オブジェクトに
JSON.AddArray ("doc", "/", "tags"); // /tags を空配列に
JSON.AddObjectToArray("doc", "/items", 1); // 空オブジェクトを 1 個 push
JSON.AddArrayToArray ("doc", "/tags", 1); // 空配列を 1 個 push
JSON.SetValue("doc", "/tags/0", "auto");
JSON.SetValue("doc", "/tags/1", "vision");配列操作
int n = JSON.GetItemCount("doc", "/tags");
JSON.RemoveFromArray("doc", "/tags", 0);
JSON.MoveArrayItem ("doc", "/tags", 1, true); // index 1 を上に
JSON.MoveArrayItem ("doc", "/tags", 0, false); // index 0 を下に
JSON.ClearArray ("doc", "/tags");結果 / エラー処理
すべての呼び出しは内部結果フラグを更新します。
JSON.SetValue("doc", "/x/y/z", 1);
if( JSON.GetResult("doc") == false )
{
LogError($"JSON SetValue failed : {JSON.GetErrorMessage("doc")}");
JSON.ResetResult("doc");
}複数呼出のあと一括検証パターン:
JSON.ResetResult("doc");
JSON.SetValue("doc", "/a", 1);
JSON.SetValue("doc", "/b/c", 2);
JSON.SetValue("doc", "/b/arr/0", "x");
if( JSON.GetResult("doc") == false )
{
ShowMessage(EB_Ok, JSON.GetErrorMessage("doc"));
return false;
}シリアライズ / 整形
string compact = JSON.ToJsonString("doc"); // 1 行
string pretty = JSON.FormatJson(compact); // インデントFormatJson は入力文字列を変換するだけで、登録済の文書には触れません。
よく使うパターン
レシピ保存 / 読込
FUNCTION SaveRecipe(string filePath)
{
JSON.CreateJson("rec");
JSON.SetValue("rec", "/exposure_ms", Data::Exposure);
JSON.SetValue("rec", "/light_pct", Data::Light);
JSON.AddArray("rec", "/", "trims");
for(i, 0, 9)
{
JSON.AddObjectToArray("rec", "/trims", 1);
JSON.SetValue("rec", $"/trims/{i}/x", Data::TrimX[i]);
JSON.SetValue("rec", $"/trims/{i}/y", Data::TrimY[i]);
}
JSON.SaveJsonFile("rec", filePath);
JSON.RemoveJson("rec");
}
FUNCTION LoadRecipe(string filePath)
{
if( JSON.LoadJsonFile("rec", filePath) == false )
{
ShowMessage(EB_Ok, $"Load failed : {JSON.GetErrorMessage("rec")}");
return false;
}
Data::Exposure = JSON.GetDoubleValue("rec", "/exposure_ms", 0);
Data::Light = JSON.GetIntValue ("rec", "/light_pct", 0);
int n = JSON.GetItemCount("rec", "/trims");
for(i, 0, n - 1)
{
Data::TrimX[i] = JSON.GetDoubleValue("rec", $"/trims/{i}/x", 0);
Data::TrimY[i] = JSON.GetDoubleValue("rec", $"/trims/{i}/y", 0);
}
JSON.RemoveJson("rec");
return true;
}MES レスポンスのパース
JSON.LoadJson("resp", responseString);
if( JSON.GetBoolValue("resp", "/ok", false) == false )
{
ShowError(EB_Reset, 1, JSON.GetStringValue("resp", "/message"));
return false;
}
int lotSeq = JSON.GetIntValue("resp", "/data/lot_seq", 0);関数一覧
| 分類 | 関数 |
|---|---|
| ライフサイクル | CreateJson · LoadJson · LoadJsonFile · SaveJsonFile · RemoveJson · RemoveAll · ClearJsonData · Contains |
| 結果 | GetResult · GetErrorMessage · ResetResult |
| 書き | SetValue(string/int/double/bool)・ SetNullValue |
| 読み | GetStringValue · GetIntValue · GetDoubleValue · GetBoolValue · GetJsonStringByPath |
| 木構築 | AddObject · AddArray |
| 配列 | AddObjectToArray · AddArrayToArray · RemoveFromArray · ClearArray · GetItemCount · MoveArrayItem |
| シリアライズ | ToJsonString · FormatJson |