Showing with 117 additions and 44 deletions.
  1. +7 −0 CMakeLists.txt
  2. +5 −4 Makefile
  3. +27 −0 README.adoc
  4. +2 −0 TODO
  5. +1 −1 build-packages.sh
  6. +2 −2 dtoa.c
  7. +1 −1 fpconv.c
  8. +1 −1 fpconv.h
  9. +1 −1 lua-cjson.spec
  10. +5 −0 lua/cjson/util.lua
  11. +24 −10 lua_cjson.c
  12. +12 −10 manual.txt → manual.adoc
  13. +1 −1 performance.txt → performance.adoc
  14. +7 −6 strbuf.c
  15. +1 −0 tests/genutf8.pl
  16. +20 −7 tests/test.lua
@@ -68,6 +68,13 @@ else()
set(_lua_module_dir "${_lua_lib_dir}/lua/5.1")
endif()

if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
add_definitions(-Dinline=__inline)
add_definitions(-Dsnprintf=_snprintf)
add_definitions(-Dstrncasecmp=_strnicmp)
endif()

add_library(cjson MODULE lua_cjson.c strbuf.c ${FPCONV_SOURCES})
set_target_properties(cjson PROPERTIES PREFIX "")
target_link_libraries(cjson ${_MODULE_LINK})
@@ -11,7 +11,7 @@
## multi-threaded application. Requries _pthreads_.

##### Build defaults #####
LUA_VERSION = 5.1
LUA_VERSION = 5.3
TARGET = cjson.so
PREFIX = /usr/local
#CFLAGS = -g -Wall -pedantic -fno-inline
@@ -40,7 +40,8 @@ LUA_BIN_DIR = $(PREFIX)/bin
#CJSON_LDFLAGS = -bundle -undefined dynamic_lookup

## Solaris
#CC = gcc
#PREFIX = /home/user/opt
#CC = gcc
#CJSON_CFLAGS = -fpic -DUSE_INTERNAL_ISINF

## Windows (MinGW)
@@ -83,12 +84,12 @@ OBJS = lua_cjson.o strbuf.o $(FPCONV_OBJS)

.PHONY: all clean install install-extra doc

.SUFFIXES: .html .txt
.SUFFIXES: .html .adoc

.c.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(BUILD_CFLAGS) -o $@ $<

.txt.html:
.adoc.html:
$(ASCIIDOC) -n -a toc $<

all: $(TARGET)
@@ -0,0 +1,27 @@
= Lua CJSON =
Mark Pulford <mark@kyne.com.au>

The Lua CJSON module provides JSON support for Lua.

*Features*::
- Fast, standards compliant encoding/parsing routines
- Full support for JSON with UTF-8, including decoding surrogate pairs
- Optional run-time support for common exceptions to the JSON
specification (infinity, NaN,..)
- No dependencies on other libraries

*Caveats*::
- UTF-16 and UTF-32 are not supported

Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for
details.

Please read +manual.adoc+ for installation instructions and the API
manual.

The current stable version of this software is available from the
http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CJSON website].

Feel free to email me if you have any patches, suggestions, or comments.

// vi:ft=asciidoc tw=72:
2 TODO
@@ -0,0 +1,2 @@
- Improve Windows build support.
- Provide option to force/record whether a table is an object or array.
@@ -5,7 +5,7 @@
# Build packages. Use current checked out version, or a specific tag/commit.

# Files requiring a version bump
VERSION_FILES="lua-cjson-2.1devel-1.rockspec lua-cjson.spec lua_cjson.c manual.txt runtests.sh tests/test.lua"
VERSION_FILES="lua-cjson-2.1devel-1.rockspec lua-cjson.spec lua_cjson.c manual.adoc runtests.sh tests/test.lua"

[ "$1" ] && BRANCH="$1" || BRANCH="`git describe --match '[1-3].[0-9]*'`"
VERSION="`git describe --match '[1-3].[0-9]*' $BRANCH`"
4 dtoa.c
@@ -3748,9 +3748,9 @@ dtoa
*decpt = 9999;
#ifdef IEEE_Arith
if (!word1(&u) && !(word0(&u) & 0xfffff))
return nrv_alloc("inf", rve, 8);
return nrv_alloc("Infinity", rve, 8);
#endif
return nrv_alloc("nan", rve, 3);
return nrv_alloc("NaN", rve, 3);
}
#endif
#ifdef IBM
@@ -124,7 +124,7 @@ double fpconv_strtod(const char *nptr, char **endptr)
/* Duplicate number into buffer */
if (buflen >= FPCONV_G_FMT_BUFSIZE) {
/* Handle unusually large numbers */
buf = malloc(buflen + 1);
buf = (char *)malloc(buflen + 1);
if (!buf) {
fprintf(stderr, "Out of memory");
abort();
@@ -12,7 +12,7 @@ static inline void fpconv_init()
/* Do nothing - not required */
}
#else
extern inline void fpconv_init();
extern void fpconv_init();
#endif

extern int fpconv_g_fmt(char*, double, int);
@@ -50,7 +50,7 @@ rm -rf "$RPM_BUILD_ROOT"

%files
%defattr(-,root,root,-)
%doc LICENSE NEWS performance.html performance.txt manual.html manual.txt rfc4627.txt THANKS
%doc LICENSE NEWS performance.html performance.adoc manual.html manual.adoc rfc4627.txt THANKS
%{lualibdir}/*
%{luadatadir}/*
%{_bindir}/*
@@ -10,6 +10,11 @@ local json = require "cjson"
-- -1 Not an array
-- 0 Empty table
-- >0 Highest index in the array

-- Provide unpack for Lua 5.3+ built without LUA_COMPAT_UNPACK
local unpack = unpack
if table.unpack then unpack = table.unpack end

local function is_array(table)
local max = 0
local count = 0
@@ -54,6 +54,12 @@
#define CJSON_VERSION "2.1devel"
#endif

#ifdef _MSC_VER
#define CJSON_EXPORT __declspec(dllexport)
#else
#define CJSON_EXPORT extern
#endif

/* Workaround for Solaris platforms missing isinf() */
#if !defined(isinf) && (defined(USE_INTERNAL_ISINF) || defined(MISSING_ISINF))
#define isinf(x) (!isnan(x) && isnan((x) - (x)))
@@ -193,7 +199,7 @@ static json_config_t *json_fetch_config(lua_State *l)
{
json_config_t *cfg;

cfg = lua_touserdata(l, lua_upvalueindex(1));
cfg = (json_config_t *)lua_touserdata(l, lua_upvalueindex(1));
if (!cfg)
luaL_error(l, "BUG: Unable to fetch CJSON configuration");

@@ -360,7 +366,7 @@ static int json_destroy_config(lua_State *l)
{
json_config_t *cfg;

cfg = lua_touserdata(l, 1);
cfg = (json_config_t *)lua_touserdata(l, 1);
if (cfg)
strbuf_free(&cfg->encode_buf);
cfg = NULL;
@@ -373,7 +379,7 @@ static void json_create_config(lua_State *l)
json_config_t *cfg;
int i;

cfg = lua_newuserdata(l, sizeof(*cfg));
cfg = (json_config_t *)lua_newuserdata(l, sizeof(*cfg));

/* Create GC method to clean up strbuf */
lua_newtable(l);
@@ -461,9 +467,9 @@ static void json_encode_exception(lua_State *l, json_config_t *cfg, strbuf_t *js
static void json_append_string(lua_State *l, strbuf_t *json, int lindex)
{
const char *escstr;
int i;
const char *str;
size_t len;
size_t i;

str = lua_tolstring(l, lindex, &len);

@@ -592,12 +598,20 @@ static void json_append_number(lua_State *l, json_config_t *cfg,
if (cfg->encode_invalid_numbers == 0) {
/* Prevent encoding invalid numbers */
if (isinf(num) || isnan(num))
json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf");
json_encode_exception(l, cfg, json, lindex,
"must not be NaN or Infinity");
} else if (cfg->encode_invalid_numbers == 1) {
/* Encode invalid numbers, but handle "nan" separately
* since some platforms may encode as "-nan". */
/* Encode NaN/Infinity separately to ensure Javascript compatible
* values are used. */
if (isnan(num)) {
strbuf_append_mem(json, "nan", 3);
strbuf_append_mem(json, "NaN", 3);
return;
}
if (isinf(num)) {
if (num < 0)
strbuf_append_mem(json, "-Infinity", 9);
else
strbuf_append_mem(json, "Infinity", 8);
return;
}
} else {
@@ -1399,7 +1413,7 @@ static int lua_cjson_safe_new(lua_State *l)
return 1;
}

int luaopen_cjson(lua_State *l)
CJSON_EXPORT int luaopen_cjson(lua_State *l)
{
lua_cjson_new(l);

@@ -1413,7 +1427,7 @@ int luaopen_cjson(lua_State *l)
return 1;
}

int luaopen_cjson_safe(lua_State *l)
CJSON_EXPORT int luaopen_cjson_safe(lua_State *l)
{
lua_cjson_safe_new(l);

@@ -1,6 +1,6 @@
= Lua CJSON 2.1devel Manual =
Mark Pulford <mark@kyne.com.au>
:revdate: 1st March 2012
:revdate: August 2016

Overview
--------
@@ -20,7 +20,7 @@ The Lua CJSON module provides JSON support for Lua.
Lua CJSON is covered by the MIT license. Review the file +LICENSE+ for
details.

The latest version of this software is available from the
The current stable version of this software is available from the
http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CJSON website].

Feel free to email me if you have any patches, suggestions, or comments.
@@ -29,8 +29,8 @@ Feel free to email me if you have any patches, suggestions, or comments.
Installation
------------

Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, or
http://www.luajit.org[LuaJIT] to build.
Lua CJSON requires either http://www.lua.org[Lua] 5.1, Lua 5.2, Lua 5.3,
or http://www.luajit.org[LuaJIT] to build.

The build method can be selected from 4 options:

@@ -203,8 +203,8 @@ Import Lua CJSON via the Lua +require+ function. Lua CJSON does not
register a global module table.

The +cjson+ module will throw an error during JSON conversion if any
invalid data is encountered. Refer to <<cjson_encode,+cjson.encode+>>
and <<cjson_decode,+cjson.decode+>> for details.
invalid data is encountered. Refer to <<encode,+cjson.encode+>> and
<<decode,+cjson.decode+>> for details.

The +cjson.safe+ module behaves identically to the +cjson+ module,
except when errors are encountered during JSON conversion. On error, the
@@ -238,6 +238,7 @@ workaround if required. Lua CJSON should be reinitialised via
different locale per thread is not supported.


[[decode]]
decode
~~~~~~

@@ -290,7 +291,7 @@ Lua CJSON may generate an error when trying to decode numbers not
supported by the JSON specification. _Invalid numbers_ are defined as:

- infinity
- not-a-number (NaN)
- NaN
- hexadecimal
Available settings:
@@ -438,12 +439,13 @@ Lua CJSON may generate an error when encoding floating point numbers not
supported by the JSON specification (_invalid numbers_):

- infinity
- not-a-number (NaN)
- NaN
Available settings:

+true+:: Allow _invalid numbers_ to be encoded. This will generate
non-standard JSON, but this output is supported by some libraries.
+true+:: Allow _invalid numbers_ to be encoded using the Javascript
compatible values +NaN+ and +Infinity+. This will generate
non-standard JSON, but these values are supported by some libraries.
+"null"+:: Encode _invalid numbers_ as a JSON +null+ value. This allows
infinity and NaN to be encoded into valid JSON.
+false+:: Throw an error when attempting to encode _invalid numbers_.
@@ -26,7 +26,7 @@ http://chiselapp.com/user/dhkolf/repository/dkjson/[DKJSON 2.1]::
https://github.com/brimworks/lua-yajl[Lua YAJL 2.0]::
- C wrapper for the YAJL library

http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CSJON 2.0.0]::
http://www.kyne.com.au/%7Emark/software/lua-cjson.php[Lua CJSON 2.0.0]::
- C implementation with no dependencies on other libraries


@@ -58,7 +58,7 @@ void strbuf_init(strbuf_t *s, int len)
s->reallocs = 0;
s->debug = 0;

s->buf = malloc(size);
s->buf = (char *)malloc(size);
if (!s->buf)
die("Out of memory");

@@ -69,7 +69,7 @@ strbuf_t *strbuf_new(int len)
{
strbuf_t *s;

s = malloc(sizeof(strbuf_t));
s = (strbuf_t*)malloc(sizeof(strbuf_t));
if (!s)
die("Out of memory");

@@ -173,7 +173,7 @@ void strbuf_resize(strbuf_t *s, int len)
}

s->size = newsize;
s->buf = realloc(s->buf, s->size);
s->buf = (char *)realloc(s->buf, s->size);
if (!s->buf)
die("Out of memory");
s->reallocs++;
@@ -221,12 +221,13 @@ void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...)
void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
{
va_list arg;
int fmt_len, try;
int fmt_len;
int empty_len;
int t;

/* If the first attempt to append fails, resize the buffer appropriately
* and try again */
for (try = 0; ; try++) {
for (t = 0; ; t++) {
va_start(arg, fmt);
/* Append the new formatted string */
/* fmt_len is the length of the string required, excluding the
@@ -238,7 +239,7 @@ void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)

if (fmt_len <= empty_len)
break; /* SUCCESS */
if (try > 0)
if (t > 0)
die("BUG: length of formatted string changed");

strbuf_resize(s, s->length + fmt_len);
@@ -6,6 +6,7 @@
# cff03b039d850f370a7362f3313e5268

use strict;
no warnings 'nonchar';

# 0xD800 - 0xDFFF are used to encode supplementary codepoints
# 0x10000 - 0x10FFFF are supplementary codepoints