Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Knot Resolver
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
140
Issues
140
List
Boards
Labels
Service Desk
Milestones
Merge Requests
9
Merge Requests
9
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Knot projects
Knot Resolver
Commits
04d9f986
Commit
04d9f986
authored
Oct 18, 2015
by
Marek Vavruša
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
daemon/engine: fixed serialization of nested tables
parent
4e0a6d2e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
23 deletions
+30
-23
daemon/engine.c
daemon/engine.c
+30
-23
No files found.
daemon/engine.c
View file @
04d9f986
...
...
@@ -214,39 +214,46 @@ static void l_unpack_json(lua_State *L, JsonNode *table)
}
}
/** @internal Recursive Lua/JSON serialization. */
static
JsonNode
*
l_pack_elem
(
lua_State
*
L
,
int
top
)
{
if
(
lua_isstring
(
L
,
top
))
{
return
json_mkstring
(
lua_tostring
(
L
,
top
));
}
if
(
lua_isnumber
(
L
,
top
))
{
return
json_mknumber
(
lua_tonumber
(
L
,
top
));
}
if
(
lua_isboolean
(
L
,
top
))
{
return
json_mkbool
(
lua_toboolean
(
L
,
top
));
switch
(
lua_type
(
L
,
top
))
{
case
LUA_TSTRING
:
return
json_mkstring
(
lua_tostring
(
L
,
top
));
case
LUA_TNUMBER
:
return
json_mknumber
(
lua_tonumber
(
L
,
top
));
case
LUA_TBOOLEAN
:
return
json_mkbool
(
lua_toboolean
(
L
,
top
));
case
LUA_TTABLE
:
break
;
/* Table, iterate it. */
default:
return
json_mknull
();
}
/* Use absolute indexes here, as the table may be nested. */
JsonNode
*
node
=
NULL
;
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
top
)
!=
0
)
{
JsonNode
*
val
=
l_pack_elem
(
L
,
top
+
2
);
const
bool
no_key
=
lua_isnumber
(
L
,
top
+
1
);
if
(
!
node
)
{
node
=
no_key
?
json_mkarray
()
:
json_mkobject
();
if
(
!
node
)
{
return
NULL
;
}
}
/* Insert to array/table */
if
(
no_key
)
{
json_append_element
(
node
,
val
);
}
else
{
json_append_member
(
node
,
lua_tostring
(
L
,
top
+
1
),
val
);
}
lua_pop
(
L
,
1
);
}
return
json_mknull
()
;
return
node
;
}
/** @internal Serialize to string */
static
char
*
l_pack_json
(
lua_State
*
L
,
int
top
)
{
JsonNode
*
root
=
json_mkobject
(
);
JsonNode
*
root
=
l_pack_elem
(
L
,
top
);
if
(
!
root
)
{
return
NULL
;
}
/* Iterate table on stack */
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
top
))
{
JsonNode
*
val
=
l_pack_elem
(
L
,
-
1
);
if
(
lua_isstring
(
L
,
-
2
))
{
json_append_member
(
root
,
lua_tostring
(
L
,
-
2
),
val
);
}
else
{
json_append_element
(
root
,
val
);
}
lua_pop
(
L
,
1
);
}
lua_pop
(
L
,
1
);
/* Serialize to string */
char
*
result
=
json_encode
(
root
);
json_delete
(
root
);
return
result
;
...
...
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