public T Deserialize <T>(SerializedValue serializedValue) { if (serializedValue == null) { throw new ArgumentNullException("serializedValue"); } var serializedType = Type.GetType(serializedValue.AssemblyQualifiedName); if (serializedType == null) { throw new InvalidOperationException(string.Format( "The type '{0}' cannot be resolved.", serializedValue.AssemblyQualifiedName)); } if (!typeof(T).IsAssignableFrom(serializedType)) { throw new InvalidOperationException(string.Format( "The type '{0}' cannot be assigned to the type '{1}'", serializedType, typeof(T))); } return((T)Deserialize(serializedType, serializedValue)); }
public void Ctor_OnlyAssemblyQualifiedName_Null() { const string assemblyQualifiedName = "some assembly qualified name"; var subject = new SerializedValue(assemblyQualifiedName); Assert.AreEqual(assemblyQualifiedName, subject.AssemblyQualifiedName); Assert.AreEqual(SerializationFormat.Null, subject.Format); }
public void Ctor_String_Json() { const string assemblyQualifiedName = "some assembly qualified name"; const string data = "some string data"; var subject = new SerializedValue(assemblyQualifiedName, data); Assert.AreEqual(assemblyQualifiedName, subject.AssemblyQualifiedName); Assert.AreEqual(data, subject.JsonData); Assert.AreEqual(SerializationFormat.UnityJson, subject.Format); }
public void Ctor_ByteArray_Binary() { const string assemblyQualifiedName = "some assembly qualified name"; var data = new byte[] {2, 3, 4, 54}; var subject = new SerializedValue(assemblyQualifiedName, data); Assert.AreEqual(assemblyQualifiedName, subject.AssemblyQualifiedName); Assert.AreEqual(data, subject.BinaryData); Assert.AreEqual(SerializationFormat.BinaryFormatter, subject.Format); }
public void Ctor_ParameterNotNull_PropertiesInitialized() { const string name = "memberName"; const MemberType memberType = MemberType.Property; var serializedValue = new SerializedValue("fef"); var subject = new SerializedMember(name, memberType, serializedValue); Assert.AreEqual(name, subject.Name); Assert.AreEqual(memberType, subject.MemberType); Assert.IsTrue(ReferenceEquals(serializedValue, subject.SerializedValue)); }
// ReSharper restore FieldCanBeMadeReadOnly.Local public SerializedMember([NotNull] string name, MemberType memberType, [NotNull] SerializedValue serializedValue) { if (name == null) { throw new ArgumentNullException("name"); } if (serializedValue == null) { throw new ArgumentNullException("serializedValue"); } _name = name; _memberType = memberType; _serializedValue = serializedValue; }
public void DeserializeGeneric_BinaryFormatted_Deserialized() { // ReSharper disable once AssignNullToNotNullAttribute var serializationInfo = new SerializedValue(typeof(TestData).AssemblyQualifiedName, new byte[0]); var subject = new Formatter( _binarySerializerMock.Object, _unityJsonSerializerMock.Object, _formatSelectorMock.Object); var testData = new TestData(); _binarySerializerMock.Setup(mock => mock.Deserialize(It.IsAny<byte[]>())) .Returns(testData); var result = subject.Deserialize<TestData>(serializationInfo); Assert.AreEqual(testData, result); }
public object Deserialize(SerializedValue serializedValue) { if (serializedValue == null) { throw new ArgumentNullException("serializedValue"); } var serializedType = Type.GetType(serializedValue.AssemblyQualifiedName); if (serializedType == null) { throw new InvalidOperationException(string.Format( "The type '{0}' cannot be resolved.", serializedValue.AssemblyQualifiedName)); } return(Deserialize(serializedType, serializedValue)); }
private object Deserialize(Type type, SerializedValue serializedValue) { switch (serializedValue.Format) { case SerializationFormat.Null: return(null); case SerializationFormat.BinaryFormatter: return(_binarySerializer.Deserialize(serializedValue.BinaryData)); case SerializationFormat.UnityJson: return(_unityJsonSerializer.Deserialize(type, serializedValue.JsonData)); default: throw new NotSupportedException(string.Format( "The serialization format '{0}' is not supported.", serializedValue.Format)); } }
public void Deserialize_TypeNotResolved_Throws() { var serializationInfo = new SerializedValue("12345", "12345"); var subject = new Formatter( _binarySerializerMock.Object, _unityJsonSerializerMock.Object, _formatSelectorMock.Object); var testData = new TestData(); _unityJsonSerializerMock.Setup(mock => mock.Deserialize(It.IsAny<Type>(), It.IsAny<string>())) .Returns(testData); Assert.Throws<InvalidOperationException>(() => subject.Deserialize(serializationInfo)); }
public void Deserialize_NullFormatted_Null() { // ReSharper disable once AssignNullToNotNullAttribute var serializationInfo = new SerializedValue(typeof(TestData).AssemblyQualifiedName); var subject = new Formatter( _binarySerializerMock.Object, _unityJsonSerializerMock.Object, _formatSelectorMock.Object); var result = subject.Deserialize(serializationInfo); Assert.IsNull(result); }
public void DeserializeGeneric_TypeNotAssignalbe_Throws() { // ReSharper disable once AssignNullToNotNullAttribute var serializationInfo = new SerializedValue(typeof(TestData).AssemblyQualifiedName, "12345"); var subject = new Formatter( _binarySerializerMock.Object, _unityJsonSerializerMock.Object, _formatSelectorMock.Object); Assert.Throws<InvalidOperationException>(() => subject.Deserialize<int>(serializationInfo)); }