Skip to content
Snippets Groups Projects
Commit 97afe4fd authored by Konstantin Osipov's avatar Konstantin Osipov
Browse files

Add lua-cjson support in box.cjson

Code review of cjson branch by Dmitry E. Oboukhov.
Hand-picking the changes since the original branch
is based on import of lua-cjson into Tarantool
revision tree, and this branch is based on a git
submodule.

This patch implements
https://blueprints.launchpad.net/tarantool/+spec/lua-cjson
parent 8e03cec0
No related branches found
No related tags found
No related merge requests found
...@@ -327,6 +327,14 @@ include(BuildLibGOPT) ...@@ -327,6 +327,14 @@ include(BuildLibGOPT)
libgopt_build() libgopt_build()
add_dependencies(build_bundled_libs gopt) add_dependencies(build_bundled_libs gopt)
#
# LibCJSON
#
include(BuildLibCJSON)
libcjson_build()
add_dependencies(build_bundled_libs cjson)
# #
# Third-Party misc # Third-Party misc
# #
......
#
# A macro to build the bundled liblua-cjson
macro(libcjson_build)
set(cjson_src ${PROJECT_SOURCE_DIR}/third_party/lua-cjson/lua_cjson.c
${PROJECT_SOURCE_DIR}/third_party/lua-cjson/strbuf.c
${PROJECT_SOURCE_DIR}/third_party/lua-cjson/fpconv.c)
add_library(cjson STATIC ${cjson_src})
set(LIBCJSON_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/third_party/lua-cjson)
set(LIBCJSON_LIBRARIES cjson)
message(STATUS "Use bundled Lua-CJSON library: ${LIBCJSON_LIBRARIES}")
unset(lua_cjson_src)
endmacro(libcjson_build)
#ifndef TARANTOOL_LUA_CJSON_H_INCLUDED
#define TARANTOOL_LUA_CJSON_H_INCLUDED
/*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
struct lua_State;
/**
* Initialize box.cjson system
*/
int tarantool_lua_cjson_init(struct lua_State *L);
#endif /* TARANTOOL_LUA_CJSON_H_INCLUDED */
...@@ -105,6 +105,7 @@ set (common_sources ...@@ -105,6 +105,7 @@ set (common_sources
lua/lua_ipc.m lua/lua_ipc.m
lua/lua_socket.m lua/lua_socket.m
lua/session.m lua/session.m
lua/cjson.m
) )
if (ENABLE_TRACE) if (ENABLE_TRACE)
...@@ -122,6 +123,7 @@ list(APPEND common_libraries ...@@ -122,6 +123,7 @@ list(APPEND common_libraries
${LIBEIO_LIBRARIES} ${LIBEIO_LIBRARIES}
${LIBCORO_LIBRARIES} ${LIBCORO_LIBRARIES}
${LIBGOPT_LIBRARIES} ${LIBGOPT_LIBRARIES}
${LIBCJSON_LIBRARIES}
${LUAJIT_LIB} ${LUAJIT_LIB}
${LIBOBJC_LIBRARIES} ${LIBOBJC_LIBRARIES}
misc misc
......
/*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "lua/cjson.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int luaopen_cjson(lua_State *l);
int
tarantool_lua_cjson_init(struct lua_State *L)
{
lua_getfield(L, LUA_GLOBALSINDEX, "box");
lua_pushstring(L, "cjson");
luaopen_cjson(L);
lua_settable(L, -3);
lua_pop(L, 1);
return 0;
}
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
#include "lua/stat.h" #include "lua/stat.h"
#include "lua/uuid.h" #include "lua/uuid.h"
#include "lua/session.h" #include "lua/session.h"
#include "lua/cjson.h"
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
...@@ -1111,6 +1112,7 @@ tarantool_lua_init() ...@@ -1111,6 +1112,7 @@ tarantool_lua_init()
lua_register(L, "pcall", lbox_pcall); lua_register(L, "pcall", lbox_pcall);
lua_register(L, "tonumber64", lbox_tonumber64); lua_register(L, "tonumber64", lbox_tonumber64);
tarantool_lua_cjson_init(L);
tarantool_lua_info_init(L); tarantool_lua_info_init(L);
tarantool_lua_slab_init(L); tarantool_lua_slab_init(L);
tarantool_lua_stat_init(L); tarantool_lua_stat_init(L);
......
cjson tests
lua type(box.cjson)
---
- table
...
lua box.cjson.encode(123)
---
- 123
...
lua box.cjson.encode({123})
---
- [123]
...
lua box.cjson.encode({123, 234, 345})
---
- [123,234,345]
...
lua box.cjson.encode({abc = 234, cde = 345})
---
- {"cde":345,"abc":234}
...
lua box.cjson.encode({Метапеременная = { 'Метазначение' } })
---
- {"Метапеременная":["Метазначение"]}
...
lua box.cjson.decode('123')
---
- 123
...
lua box.cjson.decode('[123, "Кудыкины горы"]')[2]
---
- Кудыкины горы
...
lua box.cjson.decode('{"test": "Результат"}').test
---
- Результат
...
# encoding: tarantool
#
print("cjson tests")
exec admin "lua type(box.cjson)"
exec admin "lua box.cjson.encode(123)"
exec admin "lua box.cjson.encode({123})"
exec admin "lua box.cjson.encode({123, 234, 345})"
exec admin "lua box.cjson.encode({abc = 234, cde = 345})"
exec admin "lua box.cjson.encode({Метапеременная = { 'Метазначение' } })"
exec admin "lua box.cjson.decode('123')"
exec admin "lua box.cjson.decode('[123, \"Кудыкины горы\"]')[2]"
exec admin "lua box.cjson.decode('{\"test\": \"Результат\"}').test"
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment