-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://api.playfab.com/releaseNotes/#161107
- Loading branch information
Showing
78 changed files
with
20,633 additions
and
7,677 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
namespace PlayFab.Json | ||
{ | ||
public interface ISerializer | ||
{ | ||
T DeserializeObject<T>(string json); | ||
T DeserializeObject<T>(string json, object jsonSerializerStrategy); | ||
object DeserializeObject(string json); | ||
|
||
string SerializeObject(object json); | ||
string SerializeObject(object json, object jsonSerializerStrategy); | ||
} | ||
|
||
|
||
public static class JsonWrapper | ||
{ | ||
private static ISerializer _instance = new SimpleJsonInstance(); | ||
|
||
/// <summary> | ||
/// Use this property to override the Serialization for the SDK. | ||
/// </summary> | ||
public static ISerializer Instance | ||
{ | ||
get { return _instance; } | ||
set { _instance = value; } | ||
} | ||
|
||
public static T DeserializeObject<T>(string json) | ||
{ | ||
return _instance.DeserializeObject<T>(json); | ||
} | ||
|
||
public static T DeserializeObject<T>(string json, object jsonSerializerStrategy) | ||
{ | ||
return _instance.DeserializeObject<T>(json, jsonSerializerStrategy); | ||
} | ||
|
||
public static object DeserializeObject(string json) | ||
{ | ||
return _instance.DeserializeObject(json); | ||
} | ||
|
||
public static string SerializeObject(object json) | ||
{ | ||
return _instance.SerializeObject(json); | ||
} | ||
|
||
public static string SerializeObject(object json, object jsonSerializerStrategy) | ||
{ | ||
return _instance.SerializeObject(json, jsonSerializerStrategy); | ||
} | ||
} | ||
|
||
public class SimpleJsonInstance : ISerializer | ||
{ | ||
public static PlayFabJsonSerializerStrategy ApiSerializerStrategy = new PlayFabJsonSerializerStrategy(); | ||
private static DateTimeStyles _dateTimeStyles = DateTimeStyles.RoundtripKind; | ||
public class PlayFabJsonSerializerStrategy : PocoJsonSerializerStrategy | ||
{ | ||
/// <summary> | ||
/// Convert the json value into the destination field/property | ||
/// </summary> | ||
public override object DeserializeObject(object value, Type type) | ||
{ | ||
var valueStr = value as string; | ||
if (valueStr == null) // For all of our custom conversions, value is a string | ||
return base.DeserializeObject(value, type); | ||
|
||
var underType = Nullable.GetUnderlyingType(type); | ||
if (underType != null) | ||
return DeserializeObject(value, underType); | ||
else if (type.GetTypeInfo().IsEnum) | ||
return Enum.Parse(type, (string)value, true); | ||
else if (type == typeof(DateTime)) | ||
{ | ||
DateTime output; | ||
var result = DateTime.TryParseExact(valueStr, PlayFabUtil.DefaultDateTimeFormats, CultureInfo.CurrentCulture, _dateTimeStyles, out output); | ||
if (result) | ||
return output; | ||
} | ||
else if (type == typeof(DateTimeOffset)) | ||
{ | ||
DateTimeOffset output; | ||
var result = DateTimeOffset.TryParseExact(valueStr, PlayFabUtil.DefaultDateTimeFormats, CultureInfo.CurrentCulture, _dateTimeStyles, out output); | ||
if (result) | ||
return output; | ||
} | ||
else if (type == typeof(TimeSpan)) | ||
{ | ||
double seconds; | ||
if (double.TryParse(valueStr, out seconds)) | ||
return TimeSpan.FromSeconds(seconds); | ||
} | ||
return base.DeserializeObject(value, type); | ||
} | ||
|
||
/// <summary> | ||
/// Set output to a string that represents the input object | ||
/// </summary> | ||
protected override bool TrySerializeKnownTypes(object input, out object output) | ||
{ | ||
if (input.GetType().GetTypeInfo().IsEnum) | ||
{ | ||
output = input.ToString(); | ||
return true; | ||
} | ||
else if (input is DateTime) | ||
{ | ||
output = ((DateTime)input).ToString(PlayFabUtil.DefaultDateTimeFormats[PlayFabUtil.DEFAULT_UTC_OUTPUT_INDEX], CultureInfo.CurrentCulture); | ||
return true; | ||
} | ||
else if (input is DateTimeOffset) | ||
{ | ||
output = ((DateTimeOffset)input).ToString(PlayFabUtil.DefaultDateTimeFormats[PlayFabUtil.DEFAULT_UTC_OUTPUT_INDEX], CultureInfo.CurrentCulture); | ||
return true; | ||
} | ||
else if (input is TimeSpan) | ||
{ | ||
output = ((TimeSpan)input).TotalSeconds; | ||
return true; | ||
} | ||
return base.TrySerializeKnownTypes(input, out output); | ||
} | ||
} | ||
|
||
public T DeserializeObject<T>(string json) | ||
{ | ||
return PlayFabSimpleJson.DeserializeObject<T>(json, ApiSerializerStrategy); | ||
} | ||
|
||
public T DeserializeObject<T>(string json, object jsonSerializerStrategy) | ||
{ | ||
return PlayFabSimpleJson.DeserializeObject<T>(json, (IJsonSerializerStrategy)jsonSerializerStrategy); | ||
} | ||
|
||
public object DeserializeObject(string json) | ||
{ | ||
return PlayFabSimpleJson.DeserializeObject(json, typeof(object), ApiSerializerStrategy); | ||
} | ||
|
||
public string SerializeObject(object json) | ||
{ | ||
return PlayFabSimpleJson.SerializeObject(json, ApiSerializerStrategy); | ||
} | ||
|
||
public string SerializeObject(object json, object jsonSerializerStrategy) | ||
{ | ||
return PlayFabSimpleJson.SerializeObject(json, (IJsonSerializerStrategy)jsonSerializerStrategy); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#if USING_NEWTONSOFT | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters; | ||
using System.IO; | ||
|
||
namespace PlayFab.Json | ||
{ | ||
public class NewtonsofJsonInstance : ISerializer | ||
{ | ||
public static JsonSerializerSettings JsonSettings = new JsonSerializerSettings | ||
{ | ||
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore, | ||
Converters = { new StringEnumConverter(), new TimeSpanFloatSeconds(), new IsoDateTimeConverter { DateTimeFormat = PlayFabUtil.DefaultDateTimeFormats[0] } }, | ||
}; | ||
private static Formatting JsonFormatting = Formatting.None; | ||
|
||
private readonly JsonSerializer _serializer = JsonSerializer.Create(JsonSettings); | ||
|
||
public T DeserializeObject<T>(string json) | ||
{ | ||
return _serializer.Deserialize<T>(new JsonTextReader(new StringReader(json))); | ||
} | ||
|
||
public T DeserializeObject<T>(string json, object jsonSerializerStrategy) | ||
{ | ||
var customSerializer = JsonSerializer.Create((JsonSerializerSettings)jsonSerializerStrategy); | ||
return customSerializer.Deserialize<T>(new JsonTextReader(new StringReader(json))); | ||
} | ||
|
||
public object DeserializeObject(string json) | ||
{ | ||
return _serializer.Deserialize(new JsonTextReader(new StringReader(json))); | ||
} | ||
|
||
public string SerializeObject(object json) | ||
{ | ||
var jsonString = new StringWriter(); | ||
var writer = new JsonTextWriter(jsonString) { Formatting = JsonFormatting }; | ||
_serializer.Serialize(writer, json); | ||
return jsonString.ToString(); | ||
} | ||
|
||
public string SerializeObject(object json, object jsonSerializerStrategy) | ||
{ | ||
var customSerializer = JsonSerializer.Create((JsonSerializerSettings)jsonSerializerStrategy); | ||
var jsonString = new StringWriter(); | ||
var writer = new JsonTextWriter(jsonString) { Formatting = JsonFormatting }; | ||
customSerializer.Serialize(writer, json); | ||
return jsonString.ToString(); | ||
} | ||
} | ||
|
||
public class TimeSpanFloatSeconds : JsonConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var timeSpan = (TimeSpan)value; | ||
serializer.Serialize(writer, timeSpan.TotalSeconds); | ||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
return TimeSpan.FromSeconds(serializer.Deserialize<float>(reader)); | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(TimeSpan); | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.