Skip to content

Commit feaabe9

Browse files
committed
Merge pull request #7 from Audifire/json
Compact "toJson" http://bugs.mtasa.com/view.php?id=8848
2 parents 75d1a36 + 846703c commit feaabe9

File tree

6 files changed

+51
-32
lines changed

6 files changed

+51
-32
lines changed

MTA10/mods/shared_logic/lua/CLuaArguments.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,12 @@ bool CLuaArguments::WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHa
529529
}
530530

531531

532-
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize )
532+
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize, bool bCompact )
533533
{
534534
json_object * my_array = WriteToJSONArray ( bSerialize );
535535
if ( my_array )
536536
{
537-
strJSON = json_object_get_string ( my_array );
537+
strJSON = json_object_to_json_string_ext ( my_array, bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED );
538538
json_object_put ( my_array ); // dereference - causes a crash, is actually commented out in the example too
539539
return true;
540540
}

MTA10/mods/shared_logic/lua/CLuaArguments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CLuaArguments
7575
bool WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL ) const;
7676
void ValidateTableKeys ( void );
7777
bool ReadFromJSONString ( const char* szJSON );
78-
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false );
78+
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false, bool bCompact = false );
7979
json_object * WriteTableToJSONObject ( bool bSerialize = false, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL );
8080
json_object * WriteToJSONArray ( bool bSerialize );
8181
bool ReadFromJSONObject ( json_object * object, std::vector < CLuaArguments* > * pKnownTables = NULL );

MTA10/mods/shared_logic/lua/CLuaFunctionDefs.Commands.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,28 @@ int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
119119
// Got a string argument?
120120
CScriptArgReader argStream ( luaVM );
121121

122-
if ( !argStream.NextIsNil ( ) )
123-
{
124-
// Read the argument
125-
CLuaArguments JSON;
126-
JSON.ReadArgument ( luaVM, 1 );
127-
128-
// Convert it to a JSON string
129-
std::string strJSON;
130-
if ( JSON.WriteToJSONString ( strJSON ) )
131-
{
132-
// Return the JSON string
133-
lua_pushstring ( luaVM, strJSON.c_str () );
134-
return 1;
135-
}
122+
if ( !argStream.NextIsNil () )
123+
{
124+
bool bCompact = false;
125+
// Read the argument
126+
CLuaArguments JSON;
127+
JSON.ReadArgument ( luaVM, 1 );
128+
argStream.Skip ( 1 );
129+
argStream.ReadBool ( bCompact, false );
130+
131+
if ( !argStream.HasErrors () )
132+
{
133+
// Convert it to a JSON string
134+
std::string strJSON;
135+
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
136+
{
137+
// Return the JSON string
138+
lua_pushstring ( luaVM, strJSON.c_str () );
139+
return 1;
140+
}
141+
}
142+
else
143+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
136144
}
137145
else
138146
m_pScriptDebugging->LogBadType ( luaVM );

MTA10_Server/mods/deathmatch/logic/lua/CLuaArguments.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,12 +589,12 @@ bool CLuaArguments::WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHa
589589
}
590590

591591

592-
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize )
592+
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize, bool bCompact )
593593
{
594594
json_object * my_array = WriteToJSONArray ( bSerialize );
595595
if ( my_array )
596596
{
597-
strJSON = json_object_get_string ( my_array );
597+
strJSON = json_object_to_json_string_ext ( my_array, bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED );
598598
json_object_put ( my_array ); // dereference - causes a crash, is actually commented out in the example too
599599
return true;
600600
}

MTA10_Server/mods/deathmatch/logic/lua/CLuaArguments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class CLuaArguments
9393
bool ReadFromBitStream ( NetBitStreamInterface& bitStream, std::vector < CLuaArguments* > * pKnownTables = NULL );
9494
bool ReadFromJSONString ( const char* szJSON );
9595
bool WriteToBitStream ( NetBitStreamInterface& bitStream, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL ) const;
96-
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false );
96+
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false, bool bCompact = false );
9797
json_object * WriteTableToJSONObject ( bool bSerialize = false, CFastHashMap < CLuaArguments*, unsigned long > * pKnownTables = NULL );
9898
json_object * WriteToJSONArray ( bool bSerialize );
9999
bool ReadFromJSONObject ( json_object * object, std::vector < CLuaArguments* > * pKnownTables = NULL );

MTA10_Server/mods/deathmatch/logic/lua/CLuaFunctionDefs.Utility.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -370,29 +370,40 @@ int CLuaFunctionDefs::GetTok ( lua_State* luaVM )
370370
return 1;
371371
}
372372

373-
int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
374-
{
373+
int CLuaFunctionDefs::toJSON ( lua_State* luaVM )
374+
{
375375
// Got a string argument?
376376
CScriptArgReader argStream ( luaVM );
377+
377378
if ( !argStream.NextIsNil () )
378379
{
380+
bool bCompact = false;
379381
// Read the argument
380382
CLuaArguments JSON;
381383
JSON.ReadArgument ( luaVM, 1 );
384+
argStream.Skip ( 1 );
385+
argStream.ReadBool ( bCompact, false );
382386

383-
// Convert it to a JSON string
384-
std::string strJSON;
385-
if ( JSON.WriteToJSONString ( strJSON ) )
387+
if ( !argStream.HasErrors () )
386388
{
387-
// Return the JSON string
388-
lua_pushstring ( luaVM, strJSON.c_str () );
389-
return 1;
389+
// Convert it to a JSON string
390+
std::string strJSON;
391+
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
392+
{
393+
// Return the JSON string
394+
lua_pushstring ( luaVM, strJSON.c_str () );
395+
return 1;
396+
}
390397
}
398+
else
399+
m_pScriptDebugging->LogCustom ( luaVM, argStream.GetFullErrorMessage () );
391400
}
392-
393-
// Failed
394-
lua_pushnil ( luaVM );
395-
return 1;
401+
else
402+
m_pScriptDebugging->LogBadType ( luaVM );
403+
404+
// Failed
405+
lua_pushnil ( luaVM );
406+
return 1;
396407
}
397408

398409

0 commit comments

Comments
 (0)