- Home /
Deserializing byte array to POCO from asp.net webapi
Hey Devs,
I have spent the past couple of days trying to figure an issue I am having with deserializing a bytes array into a POCO dataset.
I have an asp.net webapi project that I am trying return an object as a result dataset. The dataset class is in a seperate project that is just a class library that holds POCOs that the webapi will use to return. The returned object is serialized into bytes using the following class, which exists in my class library.
public class ObjectConverter
{
public static T BytesToObject<T>(byte[] bytes)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
ms.Write(bytes, 0, bytes.Length);
ms.Seek(0, SeekOrigin.Begin);
var obj = bf.Deserialize(ms);
var retObj = (T)obj;
return retObj;
}
public static byte[] ObjectToBytes(object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
}
I include this class library into Unity and perform an Api request but the deserialization fails on the line bf.Serialize(ms, obj); with the exception:
Serialization Exception: Unexpected binary format at 65;
Now when I create a test project in the same WebApi solution and hit the same requests, the deserialization is fine.
On the Unity side, I am using the following script to make the GET request
public class HttpHandler : MonoBehaviour
{
public bool UseLocalAddress;
private string _baseUrl;
/// <summary>
/// </summary>
/// <typeparam name="T">Object type to be serialized to JSON</typeparam>
/// <typeparam name="K">Object type your expecting back</typeparam>
/// <param name="controller">The controller to use</param>
/// <param name="obj">Object to be serialized to JSON</param>
/// <param name="callback">The callback function when response is finished</param>
public void GET<T, K>(ControllerTypes controller, T obj, Action<K> callback)
{
var jsonObj = JsonUtility.ToJson(obj);
string ApiAddress = string.Format("{0}{1}?obj={2}", _baseUrl, controller, jsonObj);
UnityWebRequest request = UnityWebRequest.Get(ApiAddress);
StartCoroutine(WaitForRequest(request, callback));
}
private IEnumerator WaitForRequest<T>(UnityWebRequest request, Action<T> callback)
{
yield return request.Send();
T response = default(T);
if (string.IsNullOrEmpty(request.error))
{
Debug.Log(string.Format("REQUEST COMPLETED - Response: {0}", request.downloadHandler.text));
if (typeof(T).IsPrimitive)
{
response = (T)Convert.ChangeType(request.downloadHandler.text, typeof(T));
}
else
{
response = ObjectConverter.BytesToObject<T>(request.downloadHandler.data);
}
callback(response);
}
else
{
Debug.Log(string.Format("WaitForRequest() ERROR: {0}", request.error));
}
}
And the Class I am trying to serialize is the following
[Serializable]
public class UserInfo
{
public int UserId;
public string Username;
public string Password;
public string Email;
}
Does anyone have any ideas?
Your answer
Follow this Question
Related Questions
Binary serialization layout on different platforms 0 Answers
Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers
How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers
What is the best/easiest way to save a game (or scene) at runtime? 1 Answer