Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add compact argument for toJSON from ID #0008848
  • Loading branch information
Audifire committed May 10, 2015
1 parent 7aeb6c5 commit c30efc1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions MTA10/mods/shared_logic/lua/CLuaArguments.cpp
Expand Up @@ -529,12 +529,12 @@ bool CLuaArguments::WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHa
}


bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize )
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize, bool bCompact )
{
json_object * my_array = WriteToJSONArray ( bSerialize );
if ( my_array )
{
strJSON = json_object_get_string ( my_array );
strJSON = json_object_to_json_string_ext ( my_array, bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED );
json_object_put ( my_array ); // dereference - causes a crash, is actually commented out in the example too
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion MTA10/mods/shared_logic/lua/CLuaArguments.h
Expand Up @@ -75,7 +75,7 @@ class CLuaArguments
bool WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL ) const;
void ValidateTableKeys ( void );
bool ReadFromJSONString ( const char* szJSON );
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false );
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false, bool bCompact = false );
json_object * WriteTableToJSONObject ( bool bSerialize = false, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL );
json_object * WriteToJSONArray ( bool bSerialize );
bool ReadFromJSONObject ( json_object * object, std::vector < CLuaArguments* > * pKnownTables = NULL );
Expand Down
5 changes: 4 additions & 1 deletion MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Commands.cpp
Expand Up @@ -121,13 +121,16 @@ int CLuaFunctionDefs::toJSON ( lua_State* luaVM )

if ( !argStream.NextIsNil ( ) )
{
bool bCompact = false;
// Read the argument
CLuaArguments JSON;
JSON.ReadArgument ( luaVM, 1 );
argStream.Skip ( 1 );
argStream.ReadBool ( bCompact, false );

// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON ) )
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
Expand Down
4 changes: 2 additions & 2 deletions MTA10_Server/mods/deathmatch/logic/lua/CLuaArguments.cpp
Expand Up @@ -589,12 +589,12 @@ bool CLuaArguments::WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHa
}


bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize )
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize, bool bCompact )
{
json_object * my_array = WriteToJSONArray ( bSerialize );
if ( my_array )
{
strJSON = json_object_get_string ( my_array );
strJSON = json_object_to_json_string_ext ( my_array, bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED );
json_object_put ( my_array ); // dereference - causes a crash, is actually commented out in the example too
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion MTA10_Server/mods/deathmatch/logic/lua/CLuaArguments.h
Expand Up @@ -93,7 +93,7 @@ class CLuaArguments
bool ReadFromBitStream ( NetBitStreamInterface& bitStream, std::vector < CLuaArguments* > * pKnownTables = NULL );
bool ReadFromJSONString ( const char* szJSON );
bool WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL ) const;
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false );
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false, bool bCompact = false );
json_object * WriteTableToJSONObject ( bool bSerialize = false, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL );
json_object * WriteToJSONArray ( bool bSerialize );
bool ReadFromJSONObject ( json_object * object, std::vector < CLuaArguments* > * pKnownTables = NULL );
Expand Down
Expand Up @@ -376,13 +376,16 @@ int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
CScriptArgReader argStream ( luaVM );
if ( !argStream.NextIsNil () )
{
bool bCompact = false;
// Read the argument
CLuaArguments JSON;
JSON.ReadArgument ( luaVM, 1 );
argStream.Skip ( 1 );
argStream.ReadBool ( bCompact, false );

// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON ) )
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
Expand Down

0 comments on commit c30efc1

Please sign in to comment.