public void CustomConstructorCalled(IJsonSerializerAdapter adapter) { AssertCanBeRoundtripped( adapter, new RootClassWithCustomConstructor("A", "B"), deserialized => Assert.True(deserialized.ConstructorCalled) ); }
public void Basic(IJsonSerializerAdapter adapter) { var dictionary = adapter.Deserialize <IDictionary <string, object> >("{ \"key\": \"value\" }"); Assert.NotNull(dictionary); Assert.Equal("value", dictionary.GetValueOrDefault("key")); }
public void ICollection(IJsonSerializerAdapter adapter) { AssertCanBeRoundtripped <ICollection <string>, Collection <string> >( adapter, new Collection <string> { "A", "B" } ); }
public void ISet(IJsonSerializerAdapter adapter) { AssertCanBeRoundtripped <ISet <string>, HashSet <string> >( adapter, new HashSet <string> { "A", "B" } ); }
public void MutableClass(IJsonSerializerAdapter adapter) { AssertCanBeRoundtripped <ClassWithSingleProperty <string>, ClassWithSingleProperty <string> >( adapter, new ClassWithSingleProperty <string> { Value = "A" } ); }
public void Nested(IJsonSerializerAdapter adapter) { var dictionary = adapter.Deserialize <IDictionary <string, object> >("{ \"key\": { \"nested\": \"value\" } }"); Assert.NotNull(dictionary); var nested = Assert.IsAssignableFrom <IDictionary <string, object> >(dictionary.GetValueOrDefault("key")); Assert.Equal("value", nested.GetValueOrDefault("nested")); }
// ReSharper disable once UnusedParameter.Local private void AssertCanBeRoundtripped <T>(IJsonSerializerAdapter adapter, T instance, Action <T> assertExtra = null) { var serialized = adapter.Serialize(instance); Debug.WriteLine("Serialized: " + serialized); var deserialized = adapter.Deserialize <T>(serialized); Assert.Equal(instance, deserialized); assertExtra?.Invoke(deserialized); }
// ReSharper disable once UnusedParameter.Local private void AssertCanBeRoundtripped <TPropertyType, TActualType>(IJsonSerializerAdapter adapter, TActualType value) where TActualType : TPropertyType, new() { var serialized = adapter.Serialize(new RootClassWithSingleReadOnlyProperty <TPropertyType, TActualType>(value)); Debug.WriteLine("Serialized:\r\n" + serialized); var deserialized = adapter.Deserialize <RootClassWithSingleReadOnlyProperty <TPropertyType, TActualType> >(serialized); Assert.Equal(value, deserialized.Value); }
public Injector(IRepository <T> tRepository, IDateTimeAdapter dateTimeAdapter, IAuthUserAdapter authUserAdapter, IJsonSerializerAdapter jsonSerializerAdapter, IList <IEventHandler <T> > eventHandlers) : base() { TRepository = tRepository; DateTimeAdapter = dateTimeAdapter; AuthUserAdapter = authUserAdapter; JsonSerializerAdapter = jsonSerializerAdapter; EventHandlers = eventHandlers; }
public void Uri(IJsonSerializerAdapter adapter) { var uri = new Uri("http://google.com"); AssertDeserializesTo(uri, "\"" + uri + "\"", adapter); }
// ReSharper disable once UnusedParameter.Local private void AssertDeserializesTo <T>(T expected, string json, IJsonSerializerAdapter adapter) { Assert.Equal(expected, adapter.Deserialize <T>(json)); }
public void DateTime(IJsonSerializerAdapter adapter) { var date = new DateTime(2014, 08, 02, 12, 34, 56, DateTimeKind.Utc); AssertDeserializesTo(date, "\"" + date.ToString(Iso8601WithTimeZoneFormat) + "\"", adapter); }
public void DateTimeOffset(IJsonSerializerAdapter adapter) { var date = new DateTimeOffset(2014, 08, 02, 12, 34, 56, TimeSpan.FromHours(3)); AssertDeserializesTo(date, "\"" + date.ToString(Iso8601WithTimeZoneFormat) + "\"", adapter); }
public static T Deserialize <T>(this IJsonSerializerAdapter adapter, string json) { return((T)adapter.Deserialize(typeof(T), json)); }
public void Int32(IJsonSerializerAdapter adapter) { AssertDeserializesTo(5, "5", adapter); }
public void IEnumerableOfString(IJsonSerializerAdapter adapter) { AssertDeserializesTo((IEnumerable <string>) new[] { "A", "B" }, ABListJson, adapter); }
public void CastToString(IJsonSerializerAdapter adapter) { var deserialized = adapter.Deserialize <dynamic>("\"x\""); Assert.Equal("x", (string)deserialized); }
public void CastToInt32(IJsonSerializerAdapter adapter) { var deserialized = adapter.Deserialize <dynamic>("5"); Assert.Equal(5, (int)deserialized); }
public void IReadOnlyCollectionOfString(IJsonSerializerAdapter adapter) { AssertDeserializesTo((IReadOnlyCollection <string>) new[] { "A", "B" }, ABListJson, adapter); }
public void ListOfString(IJsonSerializerAdapter adapter) { AssertDeserializesTo(new List <string> { "A", "B" }, ABListJson, adapter); }
public void ArrayOfObject(IJsonSerializerAdapter adapter) { AssertDeserializesTo(new object[] { "A", 5 }, "[\"A\", 5]", adapter); }
public void ArrayOfString(IJsonSerializerAdapter adapter) { AssertDeserializesTo(new[] { "A", "B" }, ABListJson, adapter); }
public void CastToType(IJsonSerializerAdapter adapter) { var deserialized = adapter.Deserialize <dynamic>("{ \"Value\": \"test\" }"); Assert.Equal("test", ((ClassWithSingleProperty <string>)deserialized).Value); }
// ReSharper disable once UnusedParameter.Local private void AssertDeserializesTo <TCollection>(TCollection expected, string json, IJsonSerializerAdapter adapter) where TCollection : IEnumerable { var deserialized = adapter.Deserialize <TCollection>(json); Assert.Equal( expected.Cast <object>().ToArray(), deserialized.Cast <object>().ToArray() ); }
public void PropertyAccess(IJsonSerializerAdapter adapter) { var deserialized = adapter.Deserialize <dynamic>("{ \"property\": \"value\" }"); Assert.Equal("value", (string)deserialized.property); }
public void String(IJsonSerializerAdapter adapter) { AssertDeserializesTo("ABC", "\"ABC\"", adapter); }
public void ISetOfString(IJsonSerializerAdapter adapter) { AssertDeserializesTo((ISet <string>) new HashSet <string> { "A", "B" }, ABListJson, adapter); }
public void CustomConstructorDeserialized(IJsonSerializerAdapter adapter) { AssertCanBeRoundtripped(adapter, new RootClassWithCustomConstructor("A", "B")); }