'JsonSerializationException: Unable to find a constructor for unity webgl build
I serialize a class in an azure function app and collect it via response to post in Unity3d. It's a WebGL project and it works fine on Editor but throws exception on browser (chrome/firefox):
using Newtonsoft.Json;
namespace ARTFunctions
{
public class LoginResponse
{
public string status { get; set; }
public string token { get; set; }
public string clientDB { get; set; }
public string clientTable { get; set; }
[JsonConstructor]
public LoginResponse() { }
}
public async Task<System.String> MyFunctionApp([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "head", Route = null)] HttpRequest req, ILogger log)
{
LoginResponse logResp = new LoginResponse();
logResp.status = "Success";
logResp.clientDB = "userDB";
logResp.clientTable = "thisTable";
logResp.token="abc";
return JsonConvert.SerializeObject(logResp);
}
In Unity3d, I collect the serialized object but get constructor error:
using Newtonsoft.Json;
public class C_UnityClass: MonoBehaviour
{
public void AtButtonPressed()
{
UnityWebRequest www = UnityWebRequest.Post("https://xxx.azurewebsites.net/api/MyFunctionApp", formData);
yield return www.SendWebRequest();
string queryResult = www.downloadHandler.text;
var logResp = JsonConvert.DeserializeObject<LoginResponse>(queryResult);
debug.log("logResp"+logResp.token);
}
}
And create non-Mono LoginResponse class with JsonContructor:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LoginResponse
{
public string status { get; set; }
public string token { get; set; }
public string clientDB { get; set; }
public string clientTable { get; set; }
[JsonConstructor]
public LoginResponse (){}
//I also tried adding [JsonConstructor] to this constructor but throws error too
public LoginResponse(string status, string token, string clientDB, string clientTable)
{
this.status = status;
this.token = token;
this.clientDB = clientDB;
this.clientTable = clientTable;
}
}
Error: JsonSerializationException: Unable to find a constructor to use for type LoginResponse. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'status', line 1, position 10. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonObjectContract objectContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, Newtonsoft.Json.Serialization.JsonProperty containerProperty, System.String id, System.Boolean& createdFromNonDefaultCreator) [0x00000] in <00000000000000000000000000000000>:0
EDIT: If I use a single empty constructor for LoginResponse labelled at [JsonConstructor] I get exception as if constructor fails at azure function app. Worth noting this all works when run from Unity editor, fails on browser (tested for chrome/firefox):
"Unexpected end of Stream, the content may have already been read by another component. ","token":null,"clientDB":null,"clientTable":null}"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class LoginResponse
{
public string status { get; set; }
public string token { get; set; }
public string clientDB { get; set; }
public string clientTable { get; set; }
[JsonConstructor]
public LoginResponse (){}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|