Closed louis77 closed 8 years ago
I like the idea of having JSON features in Sailor. This would mean shipping a new Lua library with it though (would not be a problem I think).
I also don't have any issues with shipping a json module with it :+1:
Any preferences/experience to share with the available JSON libraries? Personally I like https://luarocks.org/modules/dhkolf/dkjson because it's free of dependencies, fast and I never had any decoding issues with it.
seems great :dancers:
Guys did I do something wrong here?
json = require "dkjson"
-- As everything in lua it's a table
local main = {}
function main.index(page)
page:render('index')
-- Send a message to the view
page:render("index", { msg = "Hello"})
end
-- http://localhost:8080/?r=main/imgCaption
-- Check how to enable friendly URLs....
function main.imgCaption(page)
print("Recebido algo")
-- http://dkolf.de/src/dkjson-lua.fsl/home
local someData = {
animals = { "dog", "cat", "aardvark" },
instruments = { "violin", "trombone", "theremin" },
bugs = json.null,
trees = nil
}
-- Encode on json
local string_json = json.encode (someData, { indent = true })
print(string_json)
-- Give back json (BAD here null json)
page:json( someData, { indent = true })
-- Write directly on the page
--page:write( "Hello world!")
--page:print( "Hello world!")
-- Render the view
--page:render('imgCaption')
end
-- Always need to return main on the end
return main
I'm getting this error
./controllers/main.lua:31: attempt to call method 'json' (a nil value)
stack traceback:
./controllers/main.lua:31: in function <./controllers/main.lua:15>
[C]: in function 'xpcall'
/home/laraujo/torch/install/share/lua/5.1/sailor.lua:196: in function 'handlefunc'
/home/laraujo/torch/install/share/lua/5.1/remy.lua:167: in function 'run'
/home/laraujo/torch/install/share/lua/5.1/sailor.lua:43: in function 'launch'
index.lua:2: in main chunk
[C]: in function 'xpcall'
/home/laraujo/torch/install/share/lua/5.1/cgilua.lua:169: in function
[C]: in function 'xpcall'
/home/laraujo/torch/install/share/lua/5.1/cgilua.lua:169: in function 'pcall'
/home/laraujo/torch/install/share/lua/5.1/cgilua.lua:630: in function 'main'
/home/laraujo/torch/install/share/lua/5.1/wsapi/sapi.lua:53: in function
[C]: in function 'run_app'
[string " local app_name, bootstrap_code, is_file = ...."]:68: in function <[string " local app_name, bootstrap_code, is_file = ...."]:65>
I've also tested the presence of dkjson itself (I'm using through torch)
laraujo@lindev:~/work/densecap/testWeb/hey_arnold$ th
______ __ | Torch7
/_ __/__ ________/ / | Scientific computing for Lua.
/ / / _ \/ __/ __/ _ \ | Type ? for help
/_/ \___/_/ \__/_//_/ | https://github.com/torch
| http://torch.ch
th> require "dkjson"
{
addnewline : function: 0x414d6358
encodeexception : function: 0x40ea83a0
use_lpeg : function: 0x417fc118
decode : function: 0x41625f30
quotestring : function: 0x414ce208
null : {...}
encode : function: 0x408b9dc8
version : "dkjson 2.5"
}
The page table is supposed to have a method json?
On Oct 16, 2016 10:35 AM, "Leonardo Araujo dos Santos" < notifications@github.com> wrote:
I've also tested the presence of dkjson itself (I'm using through torch)
laraujo@lindev:~/work/densecap/testWeb/hey_arnold$ th
__ | Torch7 / / ____/ / | Scientific computing for Lua. / / / \/ **/ / \ | Type ? for help // _**/_/ _///_/ | https://github.com/torch | http://torch.ch
th> require "dkjson" { addnewline : function: 0x414d6358 encodeexception : function: 0x40ea83a0 use_lpeg : function: 0x417fc118 decode : function: 0x41625f30 quotestring : function: 0x414ce208 null : {...} encode : function: 0x408b9dc8 version : "dkjson 2.5" }
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sailorproject/sailor/issues/86#issuecomment-254044483, or mute the thread https://github.com/notifications/unsubscribe-auth/AAxAGHI-rD4Ykl5HcJ_4m4bL8_6vWftzks5q0hotgaJpZM4Hd6cL .
Oh sorry mabe thats is the issue do you know how could i check if this page should have a json?
I didn't make a new release of sailor yet after this function was added, this is not present on 0.5. You'd have to install the dev environment of sailor to have this available!
try luarocks install https://github.com/sailorproject/sailor/blob/master/rockspecs/sailor-current-1.rockspec
Ok @Etiene thanks
Hi @Etiene I've sucessully installed after I called
apt-get install openssl libssl-dev luarocks build https://raw.githubusercontent.com/sailorproject/sailor/master/rockspecs/sailor-current-1.rockspec
Super cool now I can calculate something on the GPU and give back to the browser as JSON. Thanks a lot guys! (Obrigado @Etiene )
--json = require "dkjson"
require 'torch'
require 'nn'
require 'image'
require 'cutorch'
-- As everything in lua it's a table
local main = {}
function main.index(page)
page:render('index')
-- Send a message to the view
page:render("index", { msg = "Hello"})
end
-- http://localhost:8080/?r=main/imgCaption
-- Check how to enable friendly URLs....
function main.imgCaption(page)
-- Disable theme, layout and set content to application/json
page.theme = nil
page.layout = nil
page.r.content_type = 'application/json'
print("Something received doing some Math")
local a = torch.rand(5,3)
local b = torch.rand(3,4)
local c = torch.Tensor(5,4)
c:mm(a,b)
print("Moving to the GPU!")
local a = a:cuda()
local b = b:cuda()
local c_gpu = c:cuda()
c_gpu:mm(a,b) -- done on GPU
local c_tab = torch.totable(c)
-- Give back json
page:json( c_tab, { indent = true })
end
-- Always need to return main on the end
return main
Since returning JSON content is such a common case now, I propose to add a json function to the page module, where I just pass a variable plus options (which are passed to dkjson), like so:
This essentially should do:
What do you think?