From f2dcd16fc72e2f10c9504897bacf5c4c2ecff516 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 20 Oct 2021 19:36:15 +0800 Subject: +lua libs --- .../DefaultContent/Libraries/json4lua/README.md | 52 +++ .../Libraries/json4lua/doc/INSTALL.txt | 4 + .../Libraries/json4lua/doc/LICENCE.txt | 21 + .../Libraries/json4lua/doc/README.txt | 5 + .../Libraries/json4lua/doc/VERSION.txt | 4 + .../Libraries/json4lua/doc/cgilua_patch.html | 187 +++++++++ .../Libraries/json4lua/doc/index.html | 394 +++++++++++++++++++ .../Libraries/json4lua/doc/pics/json4lua.gif | Bin 0 -> 5023 bytes .../Libraries/json4lua/doc/pics/lunartone.gif | Bin 0 -> 312 bytes .../Libraries/json4lua/examples/example.lua | 23 ++ .../Libraries/json4lua/examples/jsonrpc.lua | 21 + .../Libraries/json4lua/examples/tests.lua | 223 +++++++++++ .../Libraries/json4lua/examples/timetrials.lua | 46 +++ .../Libraries/json4lua/json/json.lua | 427 +++++++++++++++++++++ .../DefaultContent/Libraries/json4lua/json/rpc.lua | 107 ++++++ .../Libraries/json4lua/json/rpcserver.lua | 78 ++++ .../Libraries/json4lua/json4lua-1.0.0-1.rockspec | 32 ++ 17 files changed, 1624 insertions(+) create mode 100644 Resources/DefaultContent/Libraries/json4lua/README.md create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/INSTALL.txt create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/LICENCE.txt create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/README.txt create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/VERSION.txt create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/cgilua_patch.html create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/index.html create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/pics/json4lua.gif create mode 100644 Resources/DefaultContent/Libraries/json4lua/doc/pics/lunartone.gif create mode 100644 Resources/DefaultContent/Libraries/json4lua/examples/example.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/examples/jsonrpc.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/examples/tests.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/examples/timetrials.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/json/json.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/json/rpc.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/json/rpcserver.lua create mode 100644 Resources/DefaultContent/Libraries/json4lua/json4lua-1.0.0-1.rockspec (limited to 'Resources/DefaultContent/Libraries/json4lua') diff --git a/Resources/DefaultContent/Libraries/json4lua/README.md b/Resources/DefaultContent/Libraries/json4lua/README.md new file mode 100644 index 0000000..b4f8015 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/README.md @@ -0,0 +1,52 @@ +# json4lua +JSON and JSONRPC for Lua + +# Installation # +``` +luarocks install --server=http://rocks.moonscript.org/manifests/amrhassan --local json4Lua +``` + +# JSON Usage # + +## Encoding ## + +```lua +json = require('json') +print(json.encode({ 1, 2, 'fred', {first='mars',second='venus',third='earth'} })) +``` +```json +[1,2,"fred", {"first":"mars","second":"venus","third":"earth"}] +``` + +## Decoding ## + +```lua +json = require("json") +testString = [[ { "one":1 , "two":2, "primes":[2,3,5,7] } ]] +decoded = json.decode(testString) +table.foreach(decoded, print) +print ("Primes are:") +table.foreach(decoded.primes,print) +``` +``` +one 1 +two 2 +primes table: 0032B928 +Primes are: +1 2 +2 3 +3 5 +4 7 +``` + +# JSONRPC Usage # +```lua +json = require('json') +require("json.rpc") +server = json.rpc.proxy("http://jsolait.net/testj.py") +result, error = server.echo('Test echo!') +print(result) +``` +``` +Test echo! +``` diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/INSTALL.txt b/Resources/DefaultContent/Libraries/json4lua/doc/INSTALL.txt new file mode 100644 index 0000000..3e7cad3 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/INSTALL.txt @@ -0,0 +1,4 @@ +INSTALLATION +============ + +See INSTALLATION section in doc/index.html \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/LICENCE.txt b/Resources/DefaultContent/Libraries/json4lua/doc/LICENCE.txt new file mode 100644 index 0000000..48f61e7 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/LICENCE.txt @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2009 Craig Mason-Jones + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/README.txt b/Resources/DefaultContent/Libraries/json4lua/doc/README.txt new file mode 100644 index 0000000..59e5b7b --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/README.txt @@ -0,0 +1,5 @@ +=================================================================================================================== +== README.txt +=================================================================================================================== + +Please see doc/index.html diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/VERSION.txt b/Resources/DefaultContent/Libraries/json4lua/doc/VERSION.txt new file mode 100644 index 0000000..deae881 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/VERSION.txt @@ -0,0 +1,4 @@ +JSON4Lua and JSONRPC4Lua +Version 1.0.0 +4 March 2015 +http://github.com/craigmj/json4lua/ \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/cgilua_patch.html b/Resources/DefaultContent/Libraries/json4lua/doc/cgilua_patch.html new file mode 100644 index 0000000..1b4d6b3 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/cgilua_patch.html @@ -0,0 +1,187 @@ + + +JSON4Lua and JSONRPC4Lua + + + + + + + + + + + +
+

+ + +

+ + + + + + + +
+
Patching CGILua to handle text/plain
+ + +JSON RPC (both the JSONRPC4Lua implementation and the jsolait Javascript implementation) send the http request with a Content-Type of text/plain.

+ +CGILua 5.0 does not accept text/plain content, and will generate an error of 'Unsupported Media Type: text/plain'.

+ +This is easily patched in CGILua 5.0 by making the following change to cgilua/post.lua, line 286:

+Change:

+	elseif strfind (contenttype, "text/xml") then
+
+to +
+	elseif strfind (contenttype, "text/xml") or strfind (contenttype, "text/plain") then
+
+This makes CGILua handle text/plain as it does text/xml, without parsing the incoming POST data.

+ +Please note: I have requested the maintainers of CGILua to make this change to CGILua, whereafter this patch will no longer be required. + + +

+ + \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/index.html b/Resources/DefaultContent/Libraries/json4lua/doc/index.html new file mode 100644 index 0000000..12ec6c7 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/doc/index.html @@ -0,0 +1,394 @@ + + +JSON4Lua and JSONRPC4Lua + + + + + + + + + + + +
+

+ + +

+ + + + + + + +
+
JSON4Lua and JSONRPC4Lua
+ +

Latest News

+

(2009-08-06) We've changed the JSON4Lua and JSONRPC4Lua licence from the GPL to the MIT licence, like Lua itself.

+

+ The 0.9.20 release fixes a bug in Lua 5.1 operation, introduces a json.null value to force null values in JSON encodings, improves performance (over 50% faster on some tests), and permits /* comments */ in the JSON string being decoded. +

+ + +

Introduction

+ JSON4Lua and JSONRPC4Lua implement JSON (JavaScript Object Notation) encoding and decoding and a JSON-RPC-over-http client for Lua.

+ +JSON is JavaScript Object Notation, a simple encoding of Javascript-like objects that is ideal for lightweight transmission of relatively weakly-typed data. +A sub-package of JSON4Lua is JSONRPC4Lua, which provides a simple JSON-RPC-over-http client and server (in a CGILua environment) for Lua. Please seen the documentation below for JSONRPC4Lua. + + +

Licence

+ JSON4Lua is licensed under the MIT Consortium licence like Lua itself. Please see LICENCE.txt for details.

+ + +

Requirements

+ JSON4Lua is a pure-Lua module that is Lua 5.0 compatible (if you have compat-5.1 for Lua 5.0). JSON4Lua also works (perfectly, I hope) under Lua 5.1, which is where I largely use it nowadays. Since Lua is platform independent, so is JSON4Lua.

+ + The JSON4RPC sub-module requires Lua Socket 2.0. It uses socket.http for for the RPC over http connection. Socket 2.0 includes ltn12, which is also used by JSON4RPC.

+ + To use json.rpcserver you need a CGILua enabled webserver. However, a quick patch is required in CGILua 5.0 to support JSON-RPC. + + +

Download

+ JSON4Lua is hosted on LuaForge. + + + + + + + + + + + + + + + +
VersionDateNotes
0.9.306 August 2009 + Changed to MIT Licence. +
0.9.204 January 2006 + Introduction of local Lua functions for private functions (removed _ function prefix).
+ Fixed Lua 5.1 compatibility issues.
+ Introduced json.null to have null values in associative arrays.
+ Performance improvement (more than 50% on some tests) through table.concat rather than .. operator.
+ json.decode now ignores /* */ comments in the JSON string.
+
0.9.1020 December 2005 + Fixes bug with array representation when nil / null values occur in the array.
+ Adds content-type header of text/plain to JSON RPC http requests.
+ Introduces json.rpcserver module with simple JSON RPC enablement for Lua objects.
+ Moved the json.lua file into the json directory. Ensure, therefore, that your LUA_PATH contains a module-finding form like LUA_PATH = c:\proj\lua\?\?.lua;?.lua.
+
0.9.0119 December 2005Minor corrections to documentation.
0.9.0019 December 2005First release
+ + + +

Installation

+ As of version 0.9.10, all the JSON4Lua files are contained in the json subdirectory in the distribution zip.

+ Simply copy the json subdirectory so that it is in your Lua path.

+ Ensure that your LUA_PATH variable permits module resolution of the form ?/?.lua.

+ Example +

+
+

Using Windows

+ Under Windows, set your Lua path as (my Lua installation is in c:\proj\lua\):

+ + set LUA_PATH=c:\proj\lua\?.lua;c:\proj\lua\?\?.lua;?.lua +

+ For compat-5.1.lua to start when Lua starts (if you're using Lua 5.0), you also need:

+ + set LUA_INIT=@c:\proj\lua\compat-5.1.lua +

+ You probably also want to set your library path:

+ set LUA_CPATH=c:\proj\lua\lib\?.dll;?.dll + + +

Usage & Reference

+ The following functions in JSON4Lua and JSONRPC4Lua are of interest:

+

+
string json.encode( lua_object )
+
Returns the Lua object JSON encoded into a string.

+ Example +

+ + json = require("json")
+ print (json.encode( { 1, 2, 'fred', {first='mars',second='venus',third='earth'} } )) +

+ prints

[1,2,"fred", {"first":"mars","second":"venus","third","earth"}] +

+ +
+
lua_object json.decode( json_string )
+
Decodes the JSON encoded data structure, and returns a Lua object with the appropriate data.

+ Example + +

+ + json = require("json")
+ + testString = [[ { "one":1 , "two":2, "primes":[2,3,5,7] } ]]
+ o = json.decode(testString)
+ table.foreach(o,print)
+ print ("Primes are:")
+ table.foreach(o.primes,print) +

+ prints:

+

+one		1
+two		2
+primes		table: 0032B928
+Primes are:
+1		2
+2		3
+3		5
+4		7
+
+ +
json.null
+
Returns a unique value that will be encoded as a null in a JSON encoding. +

This is necessary in one situation. In Lua, if a key in a table has a nil value, the key is simply discarded (since any non-existent key has a nil value). The encoding of arrays has been built to manage this nil-values in arrays, but associative arrays provide a problem. Consider: +

t = { user="test", password=nil }
+ Since Lua simply discards the password key, JSON4Lua encodes this as the JSON string +
{"user":"test"}
+ If, for some reason, your JSON RPC Server requires a defined null value, use the following code: +
t = { user="test", password=json.null }
+ This will now correctly encode to: +
{"user":"test","password":null}
+ Incidentally, json.null is simply a function that returns itself, so that you can use either json.null or json.null() as you fancy. +
+ + +
result, error json.rpc.call ( url, method, ...)
+
Calls the named method on the given url with the arg parameters. Returns the result and the error. If error is nil, no error occurred.

+ Example +

+
require ("json.rpc")
+result, error = json.rpc.call("http://jsolait.net/testj.py","echo","Test echo!")
+print(result)
+

prints

+

Test echo!
+
+
+
proxyServer = json.rpc.proxy (url)
+
Creates a proxy server object on which JSON-RPC calls can be made. Each call will return the result, error. If error is nil, no error occurred.

+ Example +

+
+require ("json.rpc")
+server = json.rpc.proxy("http://jsolait.net/testj.py")
+result, error  = server.echo('Test echo!')
+print(result)
+

prints

+

Test echo!
+
+
+ +
json.rpcserver.serve(object[, packReturn])
+
+ Handles an incoming CGILua request as a JSON RPC request and serves the request from + the given object. +

The optional packReturn parameter, if set true, will, if the requested + method returns more than one value, pack these returned values into an array. If only a single value + is returned, it is not packed into an array. If packReturn is false (or not set), only the first + return value from the requested method will be returned. This is necessitated since the JSON protocol does not permit a method call to return more than a single value. +

+ + serve returns nothing.

+ + Example +

+
+--
+-- jsonrpc.lua
+-- Installed in a CGILua webserver environment (with necessary CGI Lua 5.0 patch)
+--
+require ('json.rpcserver')
+
+-- The Lua class that is to serve JSON RPC requests
+local myServer = {
+  echo = function (msg) return msg end,
+  average = function(...)
+    local total=0
+    local count=0
+    for i=1, table.getn(arg) do
+      total = total + arg[i]
+      count = count + 1
+    end
+    return { average= total/count, sum = total, n=count }
+  end
+}
+
+json.rpcserver.serve(myServer)
+
+An example of using this JSON RPC server from a Lua file: +
+require ('json.rpc')
+local server = json.rpc.proxy('http://www.myserver.com/jsonrpc.lua')
+table.foreach(server.average(10,15,23), print)
+
+Prints: +
+average	16
+sum	48
+n	3
+
+
+ +
+
+ + +

History & Roadmap

+ The downloads sections details the versions and their related histories. I will wait for Lua 5.1 to be final (expected late January) before making the JSON4Lua module beta. If no serious bugs or objections are encountered, I will make the module 1.0 on 1 April 2006 (to coincide with April Fool's day!) +
+ + \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/pics/json4lua.gif b/Resources/DefaultContent/Libraries/json4lua/doc/pics/json4lua.gif new file mode 100644 index 0000000..938bcb9 Binary files /dev/null and b/Resources/DefaultContent/Libraries/json4lua/doc/pics/json4lua.gif differ diff --git a/Resources/DefaultContent/Libraries/json4lua/doc/pics/lunartone.gif b/Resources/DefaultContent/Libraries/json4lua/doc/pics/lunartone.gif new file mode 100644 index 0000000..e914a8f Binary files /dev/null and b/Resources/DefaultContent/Libraries/json4lua/doc/pics/lunartone.gif differ diff --git a/Resources/DefaultContent/Libraries/json4lua/examples/example.lua b/Resources/DefaultContent/Libraries/json4lua/examples/example.lua new file mode 100644 index 0000000..36497da --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/examples/example.lua @@ -0,0 +1,23 @@ +--[[ +JSON4Lua example script. +Demonstrates the simple functionality of the json module. +]]-- +json = require('json') + + +-- Object to JSON encode +test = { + one='first',two='second',three={2,3,5} +} + +jsonTest = json.encode(test) + +print('JSON encoded test is: ' .. jsonTest) + +-- Now JSON decode the json string +result = json.decode(jsonTest) + +print ("The decoded table result:") +table.foreach(result,print) +print ("The decoded table result.three") +table.foreach(result.three, print) diff --git a/Resources/DefaultContent/Libraries/json4lua/examples/jsonrpc.lua b/Resources/DefaultContent/Libraries/json4lua/examples/jsonrpc.lua new file mode 100644 index 0000000..f265b8a --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/examples/jsonrpc.lua @@ -0,0 +1,21 @@ +-- +-- jsonrpc.lua +-- Installed in a CGILua webserver environment (with necessary CGI Lua 5.0 patch) +-- +require ('json.rpcserver') + +-- The Lua class that is to serve JSON RPC requests +local myServer = { + echo = function (msg) return msg end, + average = function(...) + local total=0 + local count=0 + for i=1, table.getn(arg) do + total = total + arg[i] + count = count + 1 + end + return { average= total/count, sum = total, n=count } + end +} + +json.rpcserver.serve(myServer) \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/examples/tests.lua b/Resources/DefaultContent/Libraries/json4lua/examples/tests.lua new file mode 100644 index 0000000..2e58b6e --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/examples/tests.lua @@ -0,0 +1,223 @@ +--[[ +Some basic tests for JSON4Lua. +]]-- + +--- Compares two tables for being data-identical. +function compareData(a,b) + if (type(a)=='string' or type(a)=='number' or type(a)=='boolean' or type(a)=='nil') then return a==b end + -- After basic data types, we're only interested in tables + if (type(a)~='table') then return true end + -- Check that a has everything b has + for k,v in pairs(b) do + if (not compareData( a[k], v ) ) then return false end + end + for k,v in pairs(a) do + if (not compareData( v, b[k] ) ) then return false end + end + return true +end + +--- +-- Checks that our compareData function works properly +function testCompareData() + s = "name" + r = "name" + assert(compareData(s,r)) + assert(not compareData('fred',s)) + assert(not compareData(nil, s)) + assert(not compareData("123",123)) + assert(not compareData(false, nil)) + assert(compareData(true, true)) + assert(compareData({1,2,3},{1,2,3})) + assert(compareData({'one',2,'three'},{'one',2,'three'})) + assert(not compareData({'one',2,4},{4,2,'one'})) + assert(compareData({one='ichi',two='nichi',three='san'}, {three='san',two='nichi',one='ichi'})) + s = { one={1,2,3}, two={one='hitotsu',two='futatsu',three='mitsu'} } + assert(compareData(s,s)) + t = { one={1,2,3}, two={one='een',two='twee',three='drie'} } + assert(not compareData(s,t)) +end + +testCompareData() + +-- +-- +-- Performs some perfunctory tests on JSON module +function testJSON4Lua() + json = require('json') + + if nil then + -- Test encodeString + s = [["\" +]] + r = json._encodeString(s) + assert(r=='\\"\\\\\\"\\n') + s = [["""\\\"]] + r = json._encodeString(s) + assert(r==[[\"\"\"\\\\\\\"]]) + + end + + -- Test encode for basic strings (complicated strings) + s = [[Hello, Lua!]] + r = json.encode(s) + assert(r=='"Hello, Lua!"') + s = [["\" +]] + r = json.encode(s) + assert(r=='\"\\"\\\\\\"\\n\"') + s = [["""\\\"]] + r = json.encode(s) + assert(r==[["\"\"\"\\\\\\\""]]) + + -- Test encode for numeric values + s = 23 + r = json.encode(s) + assert(r=='23') + s=48.123 + r = json.encode(s) + assert(r=='48.123') + + -- Test encode for boolean values + assert(json.encode(true)=='true') + assert(json.encode(false)=='false') + assert(json.encode(nil)=='null') + + -- Test encode for arrays + s = {1,2,3} + r = json.encode(s) + assert(r=="[1,2,3]") + s = {9,9,9} + r = json.encode(s) + assert(r=="[9,9,9]") + + -- Complex array test + s = { 2, 'joe', false, nil, 'hi' } + r = json.encode(s) + assert(r=='[2,"joe",false,null,"hi"]') + + -- Test encode for tables + s = {Name='Craig',email='craig@lateral.co.za',age=35} + r = json.encode(s) + -- NB: This test can fail because of order: need to test further once + -- decoding is supported. + -- assert(r==[[{"age":35,"Name":"Craig","email":"craig@lateral.co.za"}]]) + + -- Test encoding tables with numeric (string) indexes + s = {} + s['1']='One' + r = json.encode(s) + -- print("r = ", r) + assert(r=='{"1":"One"}') + + s['2']= {One='Uno'} + r = json.encode(s) + assert(compareData(json.decode(r), s)) + + -- Test decode_scanWhitespace + if nil then + s = " \n \r \t " + e = json._decode_scanWhitespace(s,1) + assert(e==string.len(s)+1) + s = " \n\r\t4" + assert(json._decode_scanWhitespace(s,1)==5) + + -- Test decode_scanString + s = [["Test"]] + r,e = json._decode_scanString(s,1) + assert(r=='Test' and e==7) + s = [["This\nis a \"test"]] + r = json._decode_scanString(s,1) + assert(r=="This\nis a \"test") + + s = [["Test\u00A7\\"]] + r,e = json._decode_scanString(s,1) + assert(r=="Test\xC2\xA7\\" and e==9) + + -- Test decode_scanNumber + s = [[354]] + r,e = json._decode_scanNumber(s,1) + assert(r==354 and e==4) + s = [[ 4565.23 AND OTHER THINGS ]] + r,e = json._decode_scanNumber(s,2) + assert(r==4565.23 and e==9) + s = [[ -23.22 and ]] + r,e = json._decode_scanNumber(s,2) + assert(r==-23.22 and e==8) + + -- Test decode_scanConstant + s = "true" + r,e = json._decode_scanConstant(s,1) + assert(r==true and e==5) + s = " false " + r,e = json._decode_scanConstant(s,3) + assert(r==false and e==8) + s = "1null6" + r,e = json._decode_scanConstant(s,2) + assert(r==nil and e==6) + + -- Test decode_scanArray + s = "[1,2,3]" + r,e = json._decode_scanArray(s,1) + assert(compareData(r,{1,2,3})) + s = [[[ 1 , 3 ,5 , "Fred" , true, false, null, -23 ] ]] + r,e = json._decode_scanArray(s,1) + assert(compareData(r, {1,3,5,'Fred',true,false,nil,-23} ) ) + s = "[3,5,null,7,9]" + r,e = json._decode_scanArray(s,1) + assert(compareData(r, {3,5,nil,7,9})) + s = "[3,5,null,7,9,null,null]" + r,e = json._decode_scanArray(s,1) + assert(compareData(r, {3,5,nil,7,9,nil,nil})) + + end + + s = [["Test\u00A7\\\""]] + r,e = json.decode(s) + assert(r=="Test\xC2\xA7\\\"", r) + + -- Test decode_scanObject + s = [[ {"one":1, "two":2, "three":"three", "four":true} ]] + r,e = json.decode(s) + -- for x,y in pairs(r) do + -- print(x,y) + -- end + assert(compareData(r,{one=1,two=2,three='three',four=true})) + s = [[ { "one" : { "first":1,"second":2,"third":3}, "two":2, "three":false } ]] + r,e = json.decode(s) + assert(compareData(r, {one={first=1,second=2,third=3},two=2,three=false})) + s = [[ { "primes" : [2,3,5,7,9], "user":{"name":"craig","age":35,"programs_lua":true}, + "lua_is_great":true } ]] + r,e = json.decode(s) + assert(compareData(r, {primes={2,3,5,7,9},user={name='craig',age=35,programs_lua=true},lua_is_great=true})) + + -- Test json.null management + t = { 1,2,json.null,4 } + assert( json.encode(t)=="[1,2,null,4]" ) + t = {x=json.null } + r = json.encode(t) + assert( json.encode(t) == '{"x":null}' ) + + -- Test comment decoding + s = [[ /* A comment + that spans + a few lines + */ + "test" + ]] + r,e = json.decode(s) + assert(r=='test',"Comment decoding failed") + + -- Per error reported by M.Hund, with incorrect decoding of string-numbered tables + s = {} + subt = {a="a",b="b",c="c"} + s['1'] = subt + s['2'] = subt + s['3'] = subt + r = json.decode('{"1":{"a":"a","b":"b","c":"c"},"2":{"a":"a","b":"b","c":"c"},"3":{"a":"a","b":"b","c":"c"}}') + assert(compareData(s, r)) +end + +testJSON4Lua() + +print("JSON4Lua tests completed successfully") diff --git a/Resources/DefaultContent/Libraries/json4lua/examples/timetrials.lua b/Resources/DefaultContent/Libraries/json4lua/examples/timetrials.lua new file mode 100644 index 0000000..cbda514 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/examples/timetrials.lua @@ -0,0 +1,46 @@ +--[[ + Some Time Trails for the JSON4Lua package +]]-- + + +require('json') +require('os') +require('table') + +local t1 = os.clock() +local jstr +local v +for i=1,100 do + local t = {} + for j=1,500 do + table.insert(t,j) + end + for j=1,500 do + table.insert(t,"VALUE") + end + jstr = json.encode(t) + v = json.decode(jstr) + --print(json.encode(t)) +end + +for i = 1,100 do + local t = {} + for j=1,500 do + local m= math.mod(j,3) + if (m==0) then + t['a'..j] = true + elseif m==1 then + t['a'..j] = json.null + else + t['a'..j] = j + end + end + jstr = json.encode(t) + v = json.decode(jstr) +end + +print (jstr) +--print(type(t1)) +local t2 = os.clock() + +print ("Elapsed time=" .. os.difftime(t2,t1) .. "s") \ No newline at end of file diff --git a/Resources/DefaultContent/Libraries/json4lua/json/json.lua b/Resources/DefaultContent/Libraries/json4lua/json/json.lua new file mode 100644 index 0000000..79761b6 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/json/json.lua @@ -0,0 +1,427 @@ +----------------------------------------------------------------------------- +-- JSON4Lua: JSON encoding / decoding support for the Lua language. +-- json Module. +-- Author: Craig Mason-Jones +-- Homepage: http://github.com/craigmj/json4lua/ +-- Version: 1.0.0 +-- This module is released under the MIT License (MIT). +-- Please see LICENCE.txt for details. +-- +-- USAGE: +-- This module exposes two functions: +-- json.encode(o) +-- Returns the table / string / boolean / number / nil / json.null value as a JSON-encoded string. +-- json.decode(json_string) +-- Returns a Lua object populated with the data encoded in the JSON string json_string. +-- +-- REQUIREMENTS: +-- compat-5.1 if using Lua 5.0 +-- +-- CHANGELOG +-- 0.9.20 Introduction of local Lua functions for private functions (removed _ function prefix). +-- Fixed Lua 5.1 compatibility issues. +-- Introduced json.null to have null values in associative arrays. +-- json.encode() performance improvement (more than 50%) through table.concat rather than .. +-- Introduced decode ability to ignore /**/ comments in the JSON string. +-- 0.9.10 Fix to array encoding / decoding to correctly manage nil/null values in arrays. +----------------------------------------------------------------------------- + +----------------------------------------------------------------------------- +-- Imports and dependencies +----------------------------------------------------------------------------- +local math = require('math') +local string = require("string") +local table = require("table") + +----------------------------------------------------------------------------- +-- Module declaration +----------------------------------------------------------------------------- +local json = {} -- Public namespace +local json_private = {} -- Private namespace + +-- Public constants +json.EMPTY_ARRAY={} +json.EMPTY_OBJECT={} + +-- Public functions + +-- Private functions +local decode_scanArray +local decode_scanComment +local decode_scanConstant +local decode_scanNumber +local decode_scanObject +local decode_scanString +local decode_scanWhitespace +local encodeString +local isArray +local isEncodable + +----------------------------------------------------------------------------- +-- PUBLIC FUNCTIONS +----------------------------------------------------------------------------- +--- Encodes an arbitrary Lua object / variable. +-- @param v The Lua object / variable to be JSON encoded. +-- @return String containing the JSON encoding in internal Lua string format (i.e. not unicode) +function json.encode (v) + -- Handle nil values + if v==nil then + return "null" + end + + local vtype = type(v) + + -- Handle strings + if vtype=='string' then + return '"' .. json_private.encodeString(v) .. '"' -- Need to handle encoding in string + end + + -- Handle booleans + if vtype=='number' or vtype=='boolean' then + return tostring(v) + end + + -- Handle tables + if vtype=='table' then + local rval = {} + -- Consider arrays separately + local bArray, maxCount = isArray(v) + if bArray then + for i = 1,maxCount do + table.insert(rval, json.encode(v[i])) + end + else -- An object, not an array + for i,j in pairs(v) do + if isEncodable(i) and isEncodable(j) then + table.insert(rval, '"' .. json_private.encodeString(i) .. '":' .. json.encode(j)) + end + end + end + if bArray then + return '[' .. table.concat(rval,',') ..']' + else + return '{' .. table.concat(rval,',') .. '}' + end + end + + -- Handle null values + if vtype=='function' and v==json.null then + return 'null' + end + + assert(false,'encode attempt to encode unsupported type ' .. vtype .. ':' .. tostring(v)) +end + + +--- Decodes a JSON string and returns the decoded value as a Lua data structure / value. +-- @param s The string to scan. +-- @param [startPos] Optional starting position where the JSON string is located. Defaults to 1. +-- @param Lua object, number The object that was scanned, as a Lua table / string / number / boolean or nil, +-- and the position of the first character after +-- the scanned JSON object. +function json.decode(s, startPos) + startPos = startPos and startPos or 1 + startPos = decode_scanWhitespace(s,startPos) + assert(startPos<=string.len(s), 'Unterminated JSON encoded object found at position in [' .. s .. ']') + local curChar = string.sub(s,startPos,startPos) + -- Object + if curChar=='{' then + return decode_scanObject(s,startPos) + end + -- Array + if curChar=='[' then + return decode_scanArray(s,startPos) + end + -- Number + if string.find("+-0123456789.e", curChar, 1, true) then + return decode_scanNumber(s,startPos) + end + -- String + if curChar==[["]] or curChar==[[']] then + return decode_scanString(s,startPos) + end + if string.sub(s,startPos,startPos+1)=='/*' then + return json.decode(s, decode_scanComment(s,startPos)) + end + -- Otherwise, it must be a constant + return decode_scanConstant(s,startPos) +end + +--- The null function allows one to specify a null value in an associative array (which is otherwise +-- discarded if you set the value with 'nil' in Lua. Simply set t = { first=json.null } +function json.null() + return json.null -- so json.null() will also return null ;-) +end +----------------------------------------------------------------------------- +-- Internal, PRIVATE functions. +-- Following a Python-like convention, I have prefixed all these 'PRIVATE' +-- functions with an underscore. +----------------------------------------------------------------------------- + +--- Scans an array from JSON into a Lua object +-- startPos begins at the start of the array. +-- Returns the array and the next starting position +-- @param s The string being scanned. +-- @param startPos The starting position for the scan. +-- @return table, int The scanned array as a table, and the position of the next character to scan. +function decode_scanArray(s,startPos) + local array = {} -- The return value + local stringLen = string.len(s) + assert(string.sub(s,startPos,startPos)=='[','decode_scanArray called but array does not start at position ' .. startPos .. ' in string:\n'..s ) + startPos = startPos + 1 + -- Infinite loop for array elements + local index = 1 + repeat + startPos = decode_scanWhitespace(s,startPos) + assert(startPos<=stringLen,'JSON String ended unexpectedly scanning array.') + local curChar = string.sub(s,startPos,startPos) + if (curChar==']') then + return array, startPos+1 + end + if (curChar==',') then + startPos = decode_scanWhitespace(s,startPos+1) + end + assert(startPos<=stringLen, 'JSON String ended unexpectedly scanning array.') + object, startPos = json.decode(s,startPos) + array[index] = object + index = index + 1 + until false +end + +--- Scans a comment and discards the comment. +-- Returns the position of the next character following the comment. +-- @param string s The JSON string to scan. +-- @param int startPos The starting position of the comment +function decode_scanComment(s, startPos) + assert( string.sub(s,startPos,startPos+1)=='/*', "decode_scanComment called but comment does not start at position " .. startPos) + local endPos = string.find(s,'*/',startPos+2) + assert(endPos~=nil, "Unterminated comment in string at " .. startPos) + return endPos+2 +end + +--- Scans for given constants: true, false or null +-- Returns the appropriate Lua type, and the position of the next character to read. +-- @param s The string being scanned. +-- @param startPos The position in the string at which to start scanning. +-- @return object, int The object (true, false or nil) and the position at which the next character should be +-- scanned. +function decode_scanConstant(s, startPos) + local consts = { ["true"] = true, ["false"] = false, ["null"] = nil } + local constNames = {"true","false","null"} + + for i,k in pairs(constNames) do + if string.sub(s,startPos, startPos + string.len(k) -1 )==k then + return consts[k], startPos + string.len(k) + end + end + assert(nil, 'Failed to scan constant from string ' .. s .. ' at starting position ' .. startPos) +end + +--- Scans a number from the JSON encoded string. +-- (in fact, also is able to scan numeric +- eqns, which is not +-- in the JSON spec.) +-- Returns the number, and the position of the next character +-- after the number. +-- @param s The string being scanned. +-- @param startPos The position at which to start scanning. +-- @return number, int The extracted number and the position of the next character to scan. +function decode_scanNumber(s,startPos) + local endPos = startPos+1 + local stringLen = string.len(s) + local acceptableChars = "+-0123456789.e" + while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true) + and endPos<=stringLen + ) do + endPos = endPos + 1 + end + local stringValue = 'return ' .. string.sub(s,startPos, endPos-1) + local stringEval = load(stringValue) + assert(stringEval, 'Failed to scan number [ ' .. stringValue .. '] in JSON string at position ' .. startPos .. ' : ' .. endPos) + return stringEval(), endPos +end + +--- Scans a JSON object into a Lua object. +-- startPos begins at the start of the object. +-- Returns the object and the next starting position. +-- @param s The string being scanned. +-- @param startPos The starting position of the scan. +-- @return table, int The scanned object as a table and the position of the next character to scan. +function decode_scanObject(s,startPos) + local object = {} + local stringLen = string.len(s) + local key, value + assert(string.sub(s,startPos,startPos)=='{','decode_scanObject called but object does not start at position ' .. startPos .. ' in string:\n' .. s) + startPos = startPos + 1 + repeat + startPos = decode_scanWhitespace(s,startPos) + assert(startPos<=stringLen, 'JSON string ended unexpectedly while scanning object.') + local curChar = string.sub(s,startPos,startPos) + if (curChar=='}') then + return object,startPos+1 + end + if (curChar==',') then + startPos = decode_scanWhitespace(s,startPos+1) + end + assert(startPos<=stringLen, 'JSON string ended unexpectedly scanning object.') + -- Scan the key + key, startPos = json.decode(s,startPos) + assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + startPos = decode_scanWhitespace(s,startPos) + assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + assert(string.sub(s,startPos,startPos)==':','JSON object key-value assignment mal-formed at ' .. startPos) + startPos = decode_scanWhitespace(s,startPos+1) + assert(startPos<=stringLen, 'JSON string ended unexpectedly searching for value of key ' .. key) + value, startPos = json.decode(s,startPos) + object[key]=value + until false -- infinite loop while key-value pairs are found +end + +-- START SoniEx2 +-- Initialize some things used by decode_scanString +-- You know, for efficiency +local escapeSequences = { + ["\\t"] = "\t", + ["\\f"] = "\f", + ["\\r"] = "\r", + ["\\n"] = "\n", + ["\\b"] = "\b" +} +setmetatable(escapeSequences, {__index = function(t,k) + -- skip "\" aka strip escape + return string.sub(k,2) +end}) +-- END SoniEx2 + +--- Scans a JSON string from the opening inverted comma or single quote to the +-- end of the string. +-- Returns the string extracted as a Lua string, +-- and the position of the next non-string character +-- (after the closing inverted comma or single quote). +-- @param s The string being scanned. +-- @param startPos The starting position of the scan. +-- @return string, int The extracted string as a Lua string, and the next character to parse. +function decode_scanString(s,startPos) + assert(startPos, 'decode_scanString(..) called without start position') + local startChar = string.sub(s,startPos,startPos) + -- START SoniEx2 + -- PS: I don't think single quotes are valid JSON + assert(startChar == [["]] or startChar == [[']],'decode_scanString called for a non-string') + --assert(startPos, "String decoding failed: missing closing " .. startChar .. " for string at position " .. oldStart) + local t = {} + local i,j = startPos,startPos + while string.find(s, startChar, j+1) ~= j+1 do + local oldj = j + i,j = string.find(s, "\\.", j+1) + local x,y = string.find(s, startChar, oldj+1) + if not i or x < i then + i,j = x,y-1 + end + table.insert(t, string.sub(s, oldj+1, i-1)) + if string.sub(s, i, j) == "\\u" then + local a = string.sub(s,j+1,j+4) + j = j + 4 + local n = tonumber(a, 16) + assert(n, "String decoding failed: bad Unicode escape " .. a .. " at position " .. i .. " : " .. j) + -- math.floor(x/2^y) == lazy right shift + -- a % 2^b == bitwise_and(a, (2^b)-1) + -- 64 = 2^6 + -- 4096 = 2^12 (or 2^6 * 2^6) + local x + if n < 0x80 then + x = string.char(n % 0x80) + elseif n < 0x800 then + -- [110x xxxx] [10xx xxxx] + x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40)) + else + -- [1110 xxxx] [10xx xxxx] [10xx xxxx] + x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40)) + end + table.insert(t, x) + else + table.insert(t, escapeSequences[string.sub(s, i, j)]) + end + end + table.insert(t,string.sub(j, j+1)) + assert(string.find(s, startChar, j+1), "String decoding failed: missing closing " .. startChar .. " at position " .. j .. "(for string at position " .. startPos .. ")") + return table.concat(t,""), j+2 + -- END SoniEx2 +end + +--- Scans a JSON string skipping all whitespace from the current start position. +-- Returns the position of the first non-whitespace character, or nil if the whole end of string is reached. +-- @param s The string being scanned +-- @param startPos The starting position where we should begin removing whitespace. +-- @return int The first position where non-whitespace was encountered, or string.len(s)+1 if the end of string +-- was reached. +function decode_scanWhitespace(s,startPos) + local whitespace=" \n\r\t" + local stringLen = string.len(s) + while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true) and startPos <= stringLen) do + startPos = startPos + 1 + end + return startPos +end + +--- Encodes a string to be JSON-compatible. +-- This just involves back-quoting inverted commas, back-quotes and newlines, I think ;-) +-- @param s The string to return as a JSON encoded (i.e. backquoted string) +-- @return The string appropriately escaped. + +local escapeList = { + ['"'] = '\\"', + ['\\'] = '\\\\', + ['/'] = '\\/', + ['\b'] = '\\b', + ['\f'] = '\\f', + ['\n'] = '\\n', + ['\r'] = '\\r', + ['\t'] = '\\t' +} + +function json_private.encodeString(s) + local s = tostring(s) + return s:gsub(".", function(c) return escapeList[c] end) -- SoniEx2: 5.0 compat +end + +-- Determines whether the given Lua type is an array or a table / dictionary. +-- We consider any table an array if it has indexes 1..n for its n items, and no +-- other data in the table. +-- I think this method is currently a little 'flaky', but can't think of a good way around it yet... +-- @param t The table to evaluate as an array +-- @return boolean, number True if the table can be represented as an array, false otherwise. If true, +-- the second returned value is the maximum +-- number of indexed elements in the array. +function isArray(t) + -- Next we count all the elements, ensuring that any non-indexed elements are not-encodable + -- (with the possible exception of 'n') + if (t == json.EMPTY_ARRAY) then return true, 0 end + if (t == json.EMPTY_OBJECT) then return false end + + local maxIndex = 0 + for k,v in pairs(t) do + if (type(k)=='number' and math.floor(k)==k and 1<=k) then -- k,v is an indexed pair + if (not isEncodable(v)) then return false end -- All array elements must be encodable + maxIndex = math.max(maxIndex,k) + else + if (k=='n') then + if v ~= (t.n or #t) then return false end -- False if n does not hold the number of elements + else -- Else of (k=='n') + if isEncodable(v) then return false end + end -- End of (k~='n') + end -- End of k,v not an indexed pair + end -- End of loop across all pairs + return true, maxIndex +end + +--- Determines whether the given Lua object / table / variable can be JSON encoded. The only +-- types that are JSON encodable are: string, boolean, number, nil, table and json.null. +-- In this implementation, all other types are ignored. +-- @param o The object to examine. +-- @return boolean True if the object should be JSON encoded, false if it should be ignored. +function isEncodable(o) + local t = type(o) + return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or + (t=='function' and o==json.null) +end + +return json diff --git a/Resources/DefaultContent/Libraries/json4lua/json/rpc.lua b/Resources/DefaultContent/Libraries/json4lua/json/rpc.lua new file mode 100644 index 0000000..952c5b5 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/json/rpc.lua @@ -0,0 +1,107 @@ +----------------------------------------------------------------------------- +-- JSONRPC4Lua: JSON RPC client calls over http for the Lua language. +-- json.rpc Module. +-- Author: Craig Mason-Jones +-- Homepage: http://github.com/craigmj/json4lua/ +-- Version: 1.0.0 +-- This module is released under the MIT License (MIT). +-- Please see LICENCE.txt for details. +-- +-- USAGE: +-- This module exposes two functions: +-- proxy( 'url') +-- Returns a proxy object for calling the JSON RPC Service at the given url. +-- call ( 'url', 'method', ...) +-- Calls the JSON RPC server at the given url, invokes the appropriate method, and +-- passes the remaining parameters. Returns the result and the error. If the result is nil, an error +-- should be there (or the system returned a null). If an error is there, the result should be nil. +-- +-- REQUIREMENTS: +-- Lua socket 2.0 (http://www.cs.princeton.edu/~diego/professional/luasocket/) +-- json (The JSON4Lua package with which it is bundled) +-- compat-5.1 if using Lua 5.0. +----------------------------------------------------------------------------- + +local json = require('json') +json.rpc = {} -- Module public namespace + +----------------------------------------------------------------------------- +-- Imports and dependencies +----------------------------------------------------------------------------- +local json = require('json') +local http = require("socket.http") + +----------------------------------------------------------------------------- +-- PUBLIC functions +----------------------------------------------------------------------------- + +--- Creates an RPC Proxy object for the given Url of a JSON-RPC server. +-- @param url The URL for the JSON RPC Server. +-- @return Object on which JSON-RPC remote methods can be called. +-- EXAMPLE Usage: +-- local jsolait = json.rpc.proxy('http://jsolait.net/testj.py') +-- print(jsolait.echo('This is a test of the echo method!')) +-- print(jsolait.args2String('first','second','third')) +-- table.foreachi( jsolait.args2Array(5,4,3,2,1), print) +function json.rpc.proxy(url) + local serverProxy = {} + local proxyMeta = { + __index = function(self, key) + return function(...) + return json.rpc.call(url, key, ...) + end + end + } + setmetatable(serverProxy, proxyMeta) + return serverProxy +end + +--- Calls a JSON RPC method on a remote server. +-- Returns a boolean true if the call succeeded, false otherwise. +-- On success, the second returned parameter is the decoded +-- JSON object from the server. +-- On http failure, returns nil and an error message. +-- On success, returns the result and nil. +-- @param url The url of the JSON RPC server. +-- @param method The method being called. +-- @param ... Parameters to pass to the method. +-- @return result, error The JSON RPC result and error. One or the other should be nil. If both +-- are nil, this means that the result of the RPC call was nil. +-- EXAMPLE Usage: +-- print(json.rpc.call('http://jsolait.net/testj.py','echo','This string will be returned')) +function json.rpc.call(url, method, ...) + local JSONRequestArray = { + id=tostring(math.random()), + ["method"]=method, + ["jsonrpc"]="2.0", + params = ... + } + local httpResponse, result , code + local jsonRequest = json.encode(JSONRequestArray) + -- We use the sophisticated http.request form (with ltn12 sources and sinks) so that + -- we can set the content-type to text/plain. While this shouldn't strictly-speaking be true, + -- it seems a good idea (Xavante won't work w/out a content-type header, although a patch + -- is needed to Xavante to make it work with text/plain) + local ltn12 = require('ltn12') + local resultChunks = {} + httpResponse, code = http.request( + { ['url'] = url, + sink = ltn12.sink.table(resultChunks), + method = 'POST', + headers = { ['content-type']='application/json-rpc', ['content-length']=string.len(jsonRequest) }, + source = ltn12.source.string(jsonRequest) + } + ) + httpResponse = table.concat(resultChunks) + -- Check the http response code + if (code~=200) then + return nil, "HTTP ERROR: " .. code + end + -- And decode the httpResponse and check the JSON RPC result code + result = json.decode( httpResponse ) + if result.result then + return result.result, nil + else + return nil, result.error + end +end diff --git a/Resources/DefaultContent/Libraries/json4lua/json/rpcserver.lua b/Resources/DefaultContent/Libraries/json4lua/json/rpcserver.lua new file mode 100644 index 0000000..e01f1f8 --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/json/rpcserver.lua @@ -0,0 +1,78 @@ +----------------------------------------------------------------------------- +-- JSONRPC4Lua: JSON RPC server for exposing Lua objects as JSON RPC callable +-- objects via http. +-- json.rpcserver Module. +-- Author: Craig Mason-Jones +-- Homepage: http://github.com/craigmj/json4lua/ +-- Version: 1.0.0 +-- This module is released under the MIT License (MIT). +-- Please see LICENCE.txt for details. +-- +-- USAGE: +-- This module exposes one function: +-- server(luaClass, packReturn) +-- Manages incoming JSON RPC request forwarding the method call to the given +-- object. If packReturn is true, multiple return values are packed into an +-- array on return. +-- +-- IMPORTANT NOTES: +-- 1. This version ought really not be 0.9.10, since this particular part of the +-- JSONRPC4Lua package is very first-draft. However, the JSON4Lua package with which +-- it comes is quite solid, so there you have it :-) +-- 2. This has only been tested with Xavante webserver, with which it works +-- if you patch CGILua to accept 'text/plain' content type. See doc\cgilua_patch.html +-- for details. +---------------------------------------------------------------------------- + +module ('json.rpcserver') + +--- +-- Implements a JSON RPC Server wrapping for luaClass, exposing each of luaClass's +-- methods as JSON RPC callable methods. +-- @param luaClass The JSON RPC class to expose. +-- @param packReturn If true, the server will automatically wrap any +-- multiple-value returns into an array. Single returns remain single returns. If +-- false, when a function returns multiple values, only the first of these values will +-- be returned. +-- +function serve(luaClass, packReturn) + cgilua.contentheader('text','plain') + require('cgilua') + require ('json') + local postData = "" + + if not cgilua.servervariable('CONTENT_LENGTH') then + cgilua.put("Please access JSON Request using HTTP POST Request") + return 0 + else + postData = cgi[1] -- SAPI.Request.getpostdata() --[[{ "id":1, "method":"echo","params":["Hi there"]}]] -- + end + -- @TODO Catch an error condition on decoding the data + local jsonRequest = json.decode(postData) + local jsonResponse = {} + jsonResponse.id = jsonRequest.id + local method = luaClass[ jsonRequest.method ] + + if not method then + jsonResponse.error = 'Method ' .. jsonRequest.method .. ' does not exist at this server.' + else + local callResult = { pcall( method, unpack( jsonRequest.params ) ) } + if callResult[1] then -- Function call successfull + table.remove(callResult,1) + if packReturn and table.getn(callResult)>1 then + jsonResponse.result = callResult + else + jsonResponse.result = unpack(callResult) -- NB: Does not support multiple argument returns + end + else + jsonResponse.error = callResult[2] + end + end + + -- Output the result + -- TODO: How to be sure that the result and error tags are there even when they are nil in Lua? + -- Can force them by hand... ? + cgilua.contentheader('text','plain') + cgilua.put( json.encode( jsonResponse ) ) +end + diff --git a/Resources/DefaultContent/Libraries/json4lua/json4lua-1.0.0-1.rockspec b/Resources/DefaultContent/Libraries/json4lua/json4lua-1.0.0-1.rockspec new file mode 100644 index 0000000..b3b737a --- /dev/null +++ b/Resources/DefaultContent/Libraries/json4lua/json4lua-1.0.0-1.rockspec @@ -0,0 +1,32 @@ +package="JSON4Lua" +version="1.0.0" +source = { + url = "git://github.com/craigmj/json4lua.git", + tag = "1.0.0" +} +description = { + summary = "JSON4Lua and JSONRPC4Lua implement JSON (JavaScript Object Notation) encoding and decoding and a JSON-RPC-over-http client for Lua.", + detailed = [[ + JSON4Lua and JSONRPC4Lua implement JSON (JavaScript Object Notation) + encoding and decoding and a JSON-RPC-over-http client for Lua. + JSON is JavaScript Object Notation, a simple encoding of + Javascript-like objects that is ideal for lightweight transmission + of relatively weakly-typed data. A sub-package of JSON4Lua is + JSONRPC4Lua, which provides a simple JSON-RPC-over-http client and server + (in a CGILua environment) for Lua. + ]], + homepage = "http://github.com/craigmj/json4lua/", + license = "GPL" +} +dependencies = { + "lua >= 5.2", + "luasocket", +} + +build = { + type = "builtin", + modules = { + ["json"] = "json/json.lua", + ["json.rpc"] = "json/rpc.lua" + } +} -- cgit v1.1-26-g67d0