Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a ton of bugs an problems encountered by trying to use json4lua #13

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 15 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,22 @@
# json4lua
JSON and JSONRPC for Lua
# jsonrpc4lua
JSON RPC over HTTP Lua module. Fork of [json4lua](https://github.com/craigmj/json4lua), refactored to use [CJSON](http://www.kyne.com.au/~mark/software/lua-cjson.php).

# Installation #
```
luarocks install --server=http://rocks.moonscript.org/manifests/amrhassan --local json4Lua
```

# JSON Usage #
## Dependencies
* lua >= 5.1
* luasocket
* cgilua (patch required)
* lua-cjson

## Encoding ##
Optional requirement to run the server-side example:
* xavante

```lua
json = require('json')
print(json.encode({ 1, 2, 'fred', {first='mars',second='venus',third='earth'} }))
## Installation
```
```json
[1,2,"fred", {"first":"mars","second":"venus","third","earth"}]
luarocks --local install jsonrpc4lua
```

## 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(o.primes,print)
```
```
one 1
two 2
primes table: 0032B928
Primes are:
1 2
2 3
3 5
4 7
```
## Required CGILua Fix
See https://github.com/pdxmeshnet/cgilua/commit/1b35d812c7d637b91f2ac0a8d91f9698ba84d8d9

# 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!
```
## Usage
See examples directory.
6 changes: 3 additions & 3 deletions doc/VERSION.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
JSON4Lua and JSONRPC4Lua
JSONRPC4Lua
Version 1.0.0
4 March 2015
http://github.com/craigmj/json4lua/
31 July 2015
http://github.com/pdxmeshnet/jsonrpc4lua/
17 changes: 11 additions & 6 deletions doc/cgilua_patch.html
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,24 @@ <h1>JSON</h1>
<div class="titleBar">Patching CGILua to handle <code>text/plain</code></div>

<!-- cgilua_patch.html -->
JSON RPC (both the JSONRPC4Lua implementation and the <a href="http://www.jsolait.net/">jsolait Javascript</a> implementation) send the http request with a Content-Type of <code>text/plain</code>.<p/>
JSON RPC (both the JSONRPC4Lua implementation and the <a href="http://www.jsolait.net/">jsolait Javascript</a> implementation) send the http request with a Content-Type of <code>application/json-rpc</code>.<p/>

CGILua 5.0 does not accept <code>text/plain</code> content, and will generate an error of 'Unsupported Media Type: text/plain'.<p/>
CGILua does not accept <code>application/json-rpc</code> content, and will generate 'Unsupported Media Type' error.<p/>

This is easily patched in CGILua 5.0 by making the following change to <code>cgilua/post.lua</code>, line 286:<p/>
This is easily patched in CGILua by making the following change to <code>cgilua/post.lua</code>, line 289:<p/>
Change:<pre>
elseif strfind (contenttype, "text/xml") then
elseif strfind (contenttype, "application/xml", 1, true) or strfind (contenttype, "text/xml", 1, true) or strfind (contenttype, "text/plain", 1, true) then
tinsert (defs.args, read (inputsize))
else
error("Unsupported Media Type: "..contenttype)
</pre>
to
<pre>
elseif strfind (contenttype, "text/xml") or strfind (contenttype, "text/plain") then
else
local input = read(inputsize)
tinsert (defs.args, input)
</pre>
This makes CGILua handle <code>text/plain</code> as it does <code>text/xml</code>, without parsing the incoming POST data.<p/>
This makes CGILua handle all unsupported content types as <code>text/plain</code>, without parsing the incoming POST data.<p/>

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

Expand Down
23 changes: 0 additions & 23 deletions examples/example.lua

This file was deleted.

12 changes: 12 additions & 0 deletions examples/rpcclient.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

-- this example will work only if cgilua module has this fix https://github.com/pdxmeshnet/cgilua/commit/1b35d812c7d637b91f2ac0a8d91f9698ba84d8d9

local rpc = require("json.rpc")

server = rpc.proxy("http://localhost:8080/jsonrpc")
result, error = server.average(10,15,23)
if error then
print(error)
else
table.foreach(result, print)
end
52 changes: 52 additions & 0 deletions examples/rpcserver/rpcserver.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

local xavante = require "xavante"
local filehandler = require "xavante.filehandler"
local cgiluahandler = require "xavante.cgiluahandler"
local redirecthandler = require "xavante.redirecthandler"


-- Define here where Xavante HTTP documents scripts are located
local webDir = "./www"

local rules = {

-- redirect
{
match = "^[^%./]*/$",
with = redirecthandler,
params = {"index.lua"}
},
{
match = "^[^%./]*/jsonrpc/?$",
with = redirecthandler,
params = {"jsonrpc.lua"}
},

-- cgi
{
match = {"%.lp$", "%.lp/.*$", "%.lua$", "%.lua/.*$" },
with = cgiluahandler.makeHandler (webDir)
},

-- static content
{
match = ".",
with = filehandler,
params = {baseDir = webDir}
},
}

xavante.HTTP{
server = {host = "*", port = 8080},

defaultHost = {
rules = rules
},
}

print "\nStarting server...\n";

xavante.start();

print "exiting...\n"

1 change: 1 addition & 0 deletions examples/rpcserver/www/index.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print "index"
5 changes: 3 additions & 2 deletions examples/jsonrpc.lua → examples/rpcserver/www/jsonrpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
-- jsonrpc.lua
-- Installed in a CGILua webserver environment (with necessary CGI Lua 5.0 patch)
--
require ('json.rpcserver')

local rpcserver = require('json.rpcserver')

-- The Lua class that is to serve JSON RPC requests
local myServer = {
Expand All @@ -18,4 +19,4 @@ local myServer = {
end
}

json.rpcserver.serve(myServer)
rpcserver.serve(myServer)
Loading