spa-json: add wp_spa_json_to_string() API
Since the string length returned by wp_spa_pod_get_data() does not always match the size of the actual json object because it is stored in contiguous memory, we cannot always rely on that API to get the json string data. The new wp_spa_pod_to_string() always allocates a new string with the same length as the json size, guaranteeing that the string returned always represents the json object, regardless of whether it is nested or not. It is always recommented to use wp_spa_pod_to_string() unless you know what you are doing.
This commit is contained in:

committed by
George Kiagiadakis

parent
a19c7f3d2f
commit
0996f5a5ca
@@ -27,6 +27,17 @@ spa_json_get_size (lua_State *L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_json_to_string (lua_State *L)
|
||||
{
|
||||
WpSpaJson *json = wplua_checkboxed (L, 1, WP_TYPE_SPA_JSON);
|
||||
/* Instead of using wp_spa_json_to_string() and lua_pushstring, we can avoid
|
||||
* an extra allocation if we use lua_pushlstring with wp_spa_json_get_data()
|
||||
* and wp_spa_json_get_size () */
|
||||
lua_pushlstring (L, wp_spa_json_get_data (json), wp_spa_json_get_size (json));
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
spa_json_is_null (lua_State *L)
|
||||
{
|
||||
@@ -312,6 +323,7 @@ spa_json_object_new (lua_State *L)
|
||||
static const luaL_Reg spa_json_methods[] = {
|
||||
{ "get_data", spa_json_get_data },
|
||||
{ "get_size", spa_json_get_size },
|
||||
{ "to_string", spa_json_to_string },
|
||||
{ "is_null", spa_json_is_null },
|
||||
{ "is_boolean", spa_json_is_boolean },
|
||||
{ "is_int", spa_json_is_int },
|
||||
|
Reference in New Issue
Block a user