Skip to content

Commit

Permalink
add pretty 💃 json serverside
Browse files Browse the repository at this point in the history
see mantis issue #9210

this change retains the old boolean functionality:
- true -> "spaced"
- false -> "plain"
- the above strings can be used instead of bools
- a new "pretty" option is now available!

see it in action: http://i.imgur.com/5F9a8HH.gifv
  • Loading branch information
qaisjp committed Apr 7, 2016
1 parent e91091d commit 5e77852
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
17 changes: 12 additions & 5 deletions MTA10_Server/mods/deathmatch/logic/CCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
*
*****************************************************************************/

#ifndef __CCOMMON_H
#define __CCOMMON_H
#pragma once

#include <cmath>
#include <cstdlib>
Expand Down Expand Up @@ -751,6 +750,16 @@ enum eWeaponFlags
WEAPONFLAGS_INSTANT_RELOAD,
};

#include "json.h"
// Custom toJSON modes (see mantis #9210)
enum eJSONMarshalType
{
TYPE_PLAIN = JSON_C_TO_STRING_PLAIN,
TYPE_SPACED = JSON_C_TO_STRING_SPACED,
TYPE_PRETTY = JSON_C_TO_STRING_PRETTY
};
DECLARE_ENUM ( eJSONMarshalType );

//////////////////////////////////////////////
// flags used to define weapon characteristics

Expand Down Expand Up @@ -780,6 +789,4 @@ enum eWeaponFlags
#define WEAPONTYPE_SLOWS_DOWN (0x010000) //
#define WEAPONTYPE_RANDOM_SPEED (0x020000) //
#define WEAPONTYPE_FORCE_FINISH_ANIM (0x040000) // force the anim to finish player after aim/fire rather than blending out
#define WEAPONTYPE_EXPANDS (0x080000) //

#endif
#define WEAPONTYPE_EXPANDS (0x080000) //
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 bCompact )
bool CLuaArguments::WriteToJSONString ( std::string& strJSON, bool bSerialize, eJSONMarshalType marshalType )
{
json_object * my_array = WriteToJSONArray ( bSerialize );
if ( my_array )
{
strJSON = json_object_to_json_string_ext ( my_array, bCompact ? JSON_C_TO_STRING_PLAIN : JSON_C_TO_STRING_SPACED );
strJSON = json_object_to_json_string_ext ( my_array, marshalType );
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 bCompact = false );
bool WriteToJSONString ( std::string& strJSON, bool bSerialize = false, eJSONMarshalType marshalType = TYPE_PLAIN );
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
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,25 @@ int CLuaFunctionDefs::toJSON ( lua_State* luaVM )

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

if ( argStream.NextIsBool () ) {
bool bCompact;
argStream.ReadBool ( bCompact );
marshalType = bCompact ? TYPE_SPACED : TYPE_PLAIN;
}
else
argStream.ReadEnumString ( marshalType, TYPE_PLAIN );

if ( !argStream.HasErrors () )
{
// Convert it to a JSON string
std::string strJSON;
if ( JSON.WriteToJSONString ( strJSON, false, bCompact ) )
if ( JSON.WriteToJSONString ( strJSON, false, marshalType ) )
{
// Return the JSON string
lua_pushstring ( luaVM, strJSON.c_str () );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE: See LICENSE in the top level directory
* FILE: MTA10_Server/mods/deathmatch/logic/lua/CLuaFunctionParseHelpers.cpp
* PURPOSE:
* DEVELOPERS: Nobody knows
*
*****************************************************************************/

Expand Down Expand Up @@ -78,6 +77,12 @@ IMPLEMENT_ENUM_BEGIN( eHudComponent )
ADD_ENUM ( HUD_ALL, "all" )
IMPLEMENT_ENUM_END( "hud-component" )

IMPLEMENT_ENUM_BEGIN ( eJSONMarshalType )
ADD_ENUM ( TYPE_PLAIN, "plain" )
ADD_ENUM ( TYPE_SPACED, "spaced" )
ADD_ENUM ( TYPE_PRETTY, "pretty" )
IMPLEMENT_ENUM_END ( "json-marshal-type" )

IMPLEMENT_ENUM_BEGIN( eWeaponType )
// Compatible with getWeaponNameFromID From setWeaponProperty before r4523
ADD_ENUM ( WEAPONTYPE_UNARMED, "fist" )
Expand Down

2 comments on commit 5e77852

@mantisbot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issues mentioned in this commit (5e77852):

@gatno
Copy link

@gatno gatno commented on 5e77852 Apr 11, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.