Skip to content

Add compact argument for toJSON from ID 0008848 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions MTA10/mods/shared_logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
36 changes: 22 additions & 14 deletions MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,28 @@ int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
// Got a string argument?
CScriptArgReader argStream ( luaVM );

if ( !argStream.NextIsNil ( ) )
{
// Read the argument
CLuaArguments JSON;
JSON.ReadArgument ( luaVM, 1 );

// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
return 1;
}
if ( !argStream.NextIsNil () )
{
bool bCompact = false;
// Read the argument
CLuaArguments JSON;
JSON.ReadArgument ( luaVM, 1 );
argStream.Skip ( 1 );
argStream.ReadBool ( bCompact, false );

if ( !argStream.HasErrors () )
{
// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
return 1;
}
}
else
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
}
else
m_pScriptDebugging->LogBadType ( luaVM );
Expand Down
4 changes: 2 additions & 2 deletions MTA10_Server/mods/deathmatch/logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
35 changes: 23 additions & 12 deletions MTA10_Server/mods/deathmatch/logic/lua/CLuaFunctionDefs.Utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,29 +370,40 @@ int CLuaFunctionDefs::GetTok ( lua_State* luaVM )
return 1;
}

int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
{
int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
{
// Got a string argument?
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 ( !argStream.HasErrors () )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
return 1;
// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
return 1;
}
}
else
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
}

// Failed
lua_pushnil ( luaVM );
return 1;
else
m_pScriptDebugging->LogBadType ( luaVM );

// Failed
lua_pushnil ( luaVM );
return 1;
}


Expand Down