アプリケーションの作成 2
クライアントサイドの作成
まずは、実行結果を見てください。
JSONのAPIを利用するためには、CURL.IO.JSONパッケージのインポートが必要です。そして、JsonValue-parseプロシージャを使用してアクセス先のURLデータを受け取り、JsonValueオブジェクトへ変換します。
{import * from CURL.IO.JSON}
|| サーバからのJSONデータの受信
{let empJsonValue: JsonValue =
{JsonValue-parse
{url "http://localhost:8080/curl_server/emp.json"}
}
}
実行結果のようなグリッドテーブル表示を行うには、RecordGridを使用します。
RecordGridを使用するには、まず、各列に対する名前と型、それにcaptionで見出し(実行結果のヘッダー)を宣言します。
{let employees: RecordSet =
{RecordSet
{RecordFields
{RecordField "empno", caption = "empno", domain = int}
, {RecordField "ename", caption = "ename", domain = String}
, {RecordField "job", caption = "job", domain = String}
, {RecordField "mgr", caption = "mgr", domain = int}
, {RecordField "hiredate", caption = "hiredate", domain = double}
, {RecordField "sal", caption = "sal", domain = double}
, {RecordField "deptno", caption = "deptno", domain = int}
}
}
}
サーバから受信したJsonValueオブジェクトの値を、RecordSetにセットして最後に表示を行えば、先ほどの実行結果を表示できます。
|| Jsonデータをcustomersに追加する
{define-proc public {add-rec
empno:int
, ename: String
, job: String
, mgr: int
, hiredate: double
, sal: double
, deptno: int
}: void
let new-rec: Record = {employees.new-record}
{new-rec.set "empno", empno}
{new-rec.set "ename", ename}
{new-rec.set "job", job}
{new-rec.set "mgr", mgr}
{new-rec.set "hiredate", hiredate}
{new-rec.set "sal", sal}
{new-rec.set "deptno", deptno}
{employees.append new-rec}
}
{for emp: JsonObject in empJsonValue do
let found?: bool = false
let empno: int = 0
let ename: String = {String}
let job: String = {String}
let mgr: int = 0
let hiredate: double = 0
let sal: double = 0
let deptno: int = 0
set (empno, found?) = {emp.get-if-exists "empno"}
set (ename, found?) = {emp.get-if-exists "ename"}
set (job, found?) = {emp.get-if-exists "job"}
set (mgr, found?) = {emp.get-if-exists "mgr"}
set (hiredate, found?) = {emp.get-if-exists "hiredate"}
set (sal, found?) = {emp.get-if-exists "sal"}
set (deptno, found?) = {emp.get-if-exists "deptno"}
{if found? then
{add-rec empno, ename, job, mgr, hiredate, sal, deptno}
}
}
{value
{RecordGrid
width=700
, record-source = employees
}
}
以上で、DBのデータをそのまま表示するというシンプルなアプリケーションを作成しました。非常に簡単なコーディングでグリッドテーブルの表示ができています。また、クライアントサイドでヘッダーをクリックすれば、ソートをすることも可能です。
最後に、start.curlのコード全体を掲載します。
{curl 6.0 applet}
{curl-file-attributes character-encoding = "shift-jis"}
{import * from CURL.IO.JSON}
|| サーバからのJSONデータの受信
{let empJsonValue: JsonValue =
{JsonValue-parse
{url "http://localhost:8080/curl_server/emp.json"}
}
}
{let employees: RecordSet =
{RecordSet
{RecordFields
{RecordField "empno", caption = "empno", domain = int}
, {RecordField "ename", caption = "ename", domain = String}
, {RecordField "job", caption = "job", domain = String}
, {RecordField "mgr", caption = "mgr", domain = int}
, {RecordField "hiredate", caption = "hiredate", domain = double}
, {RecordField "sal", caption = "sal", domain = double}
, {RecordField "deptno", caption = "deptno", domain = int}
}
}
}
|| Jsonデータをcustomersに追加する
{define-proc public {add-rec
empno:int
, ename: String
, job: String
, mgr: int
, hiredate: double
, sal: double
, deptno: int
}: void
let new-rec: Record = {employees.new-record}
{new-rec.set "empno", empno}
{new-rec.set "ename", ename}
{new-rec.set "job", job}
{new-rec.set "mgr", mgr}
{new-rec.set "hiredate", hiredate}
{new-rec.set "sal", sal}
{new-rec.set "deptno", deptno}
{employees.append new-rec}
}
{for emp: JsonObject in empJsonValue do
let found?: bool = false
let empno: int = 0
let ename: String = {String}
let job: String = {String}
let mgr: int = 0
let hiredate: double = 0
let sal: double = 0
let deptno: int = 0
set (empno, found?) = {emp.get-if-exists "empno"}
set (ename, found?) = {emp.get-if-exists "ename"}
set (job, found?) = {emp.get-if-exists "job"}
set (mgr, found?) = {emp.get-if-exists "mgr"}
set (hiredate, found?) = {emp.get-if-exists "hiredate"}
set (sal, found?) = {emp.get-if-exists "sal"}
set (deptno, found?) = {emp.get-if-exists "deptno"}
{if found? then
{add-rec empno, ename, job, mgr, hiredate, sal, deptno}
}
}
{value
{RecordGrid
width=700
, record-source = employees
}
}
まとめ
Curlはバージョン6.0から、JSONをサポートするようになりました。その紹介も兼ねて、今回はJavaで作成したサーバーサイドとJSON形式で通信を行うアプリケーションの作成を行いました。
次回はJSON形式ではなく、「Curl ORB for Java」というOSSのライブラリを使用して、CurlアプリケーションからPOJO(Plain Old Java Object)のメソッドをコールし、戻り値をCurlアプリケーションで受け取るアプリケーションを作成する手順を紹介する予定です。



