Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Lua Json
Commits
c7633030
Commit
c7633030
authored
May 28, 2019
by
seny
Browse files
Cosmetics & minor optimizations
parent
e59ca80a
Changes
2
Show whitespace changes
Inline
Side-by-side
src/json-decode.c
View file @
c7633030
...
...
@@ -173,6 +173,10 @@ static size_t decodeArray(lua_State *L, const char *buf, size_t pos, size_t size
done:
lua_pushinteger
(
L
,
len
);
lua_setfield
(
L
,
-
2
,
"__array"
);
if
(
lua_isnil
(
L
,
hidx
))
return
pos
;
lua_pushvalue
(
L
,
hidx
);
lua_insert
(
L
,
-
2
);
lua_call
(
L
,
1
,
1
);
/* Transform value */
return
pos
;
}
...
...
@@ -181,7 +185,7 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz
pos
=
decodeDelimiter
(
L
,
buf
,
pos
,
size
,
'{'
,
0
);
pos
=
decodeDelimiter
(
L
,
buf
,
pos
,
size
,
'}'
,
&
res
);
lua_newtable
(
L
);
if
(
res
)
return
pos
;
if
(
res
)
goto
done
;
checkStack
(
L
);
do
{
pos
=
decodeString
(
L
,
buf
,
pos
,
size
);
...
...
@@ -191,6 +195,11 @@ static size_t decodeObject(lua_State *L, const char *buf, size_t pos, size_t siz
pos
=
decodeDelimiter
(
L
,
buf
,
pos
,
size
,
','
,
&
res
);
}
while
(
res
);
pos
=
decodeDelimiter
(
L
,
buf
,
pos
,
size
,
'}'
,
0
);
done:
if
(
lua_isnil
(
L
,
hidx
))
return
pos
;
lua_pushvalue
(
L
,
hidx
);
lua_insert
(
L
,
-
2
);
lua_call
(
L
,
1
,
1
);
/* Transform value */
return
pos
;
}
...
...
@@ -281,23 +290,14 @@ static size_t decodeValue(lua_State *L, const char *buf, size_t pos, size_t size
if
(
pos
>=
size
)
luaL_error
(
L
,
"value expected at position %d"
,
pos
+
1
);
switch
(
buf
[
pos
])
{
case
'"'
:
pos
=
decodeString
(
L
,
buf
,
pos
,
size
);
break
;
return
decodeString
(
L
,
buf
,
pos
,
size
);
case
'['
:
pos
=
decodeArray
(
L
,
buf
,
pos
,
size
,
hidx
);
break
;
return
decodeArray
(
L
,
buf
,
pos
,
size
,
hidx
);
case
'{'
:
pos
=
decodeObject
(
L
,
buf
,
pos
,
size
,
hidx
);
break
;
return
decodeObject
(
L
,
buf
,
pos
,
size
,
hidx
);
default:
pos
=
decodeLiteral
(
L
,
buf
,
pos
,
size
);
break
;
return
decodeLiteral
(
L
,
buf
,
pos
,
size
);
}
if
(
!
lua_istable
(
L
,
-
1
)
||
lua_isnil
(
L
,
hidx
))
return
pos
;
lua_pushvalue
(
L
,
hidx
);
lua_insert
(
L
,
-
2
);
lua_call
(
L
,
1
,
1
);
/* Call handler */
return
pos
;
}
int
json__decode
(
lua_State
*
L
)
{
...
...
src/json-encode.c
View file @
c7633030
...
...
@@ -20,7 +20,6 @@
** THE SOFTWARE.
*/
#include <stdarg.h>
#include <string.h>
#include <locale.h>
#include "json.h"
...
...
@@ -237,14 +236,12 @@ static int encodeValue(lua_State *L, Box *box, int idx, const char *ev, int ridx
int
json__encode
(
lua_State
*
L
)
{
Box
*
box
;
const
char
*
ev
;
int
nerr
=
0
;
const
char
*
ev
=
luaL_optstring
(
L
,
2
,
"__toJSON"
);
luaL_checkany
(
L
,
1
);
ev
=
luaL_optstring
(
L
,
2
,
"__toJSON"
);
lua_settop
(
L
,
2
);
lua_newtable
(
L
);
box
=
newBox
(
L
);
if
(
!
encodeValue
(
L
,
box
,
1
,
ev
,
3
,
&
nerr
))
{
if
(
!
encodeValue
(
L
,
box
=
newBox
(
L
),
1
,
ev
,
3
,
&
nerr
))
{
lua_concat
(
L
,
nerr
);
return
luaL_argerror
(
L
,
1
,
lua_tostring
(
L
,
-
1
));
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment