26 lines
633 B
C#
26 lines
633 B
C#
|
|
#if TOOLS
|
||
|
|
|
||
|
|
namespace Rokojori.Tools
|
||
|
|
{
|
||
|
|
using Rokojori;
|
||
|
|
|
||
|
|
// JSONObject.GetString/GetNumber/GetBoolean etc. call Get(key) and dereference the result
|
||
|
|
// without a null check - they throw if the key is simply absent, which is the normal case
|
||
|
|
// for optional/missing request fields. This is the null-safe version for that situation.
|
||
|
|
public static class JsonUtil
|
||
|
|
{
|
||
|
|
public static string GetStringOrNull( JSONObject obj, string key )
|
||
|
|
{
|
||
|
|
if ( obj == null )
|
||
|
|
{
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
var data = obj.Get( key );
|
||
|
|
return data != null && data.isString ? data.stringValue : null;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif
|