I am trying to deserialize the same object I serialized to Firebase Realtime database but am running into an issue getting it to work. Here’s the class/script breakdown:
[Serializable]
public class PuzzleSphereTarget
{
public Nullable<float> x;
public Nullable<float> y;
public Nullable<float> z;
public PuzzleSphereTarget()
{
this.x = null;
this.y = null;
this.z = null;
}
public PuzzleSphereTarget(float xParam, float yParam, float zParam)
{
this.x = xParam;
this.y = yParam;
this.z = zParam;
}
public string ToJson()
{
return JsonUtility.ToJson(this);
}
}
As well as a wrapper PuzzleSphereInformation class:
[Serializable]
public class PuzzleSphereInformation
{
public string creatorName { get; set; }
public List<PuzzleSphereTarget> puzzleSphereTarget { get; set; }
public PuzzleSphereInformation()
{
this.creatorName = null;
this.puzzleSphereTarget = new List<PuzzleSphereTarget>();
}
public PuzzleSphereInformation(string creatorName, List<PuzzleSphereTarget> puzzleSphereTarget)
{
this.creatorName = creatorName;
this.puzzleSphereTarget = puzzleSphereTarget;
}
public string ToJson()
{
return JsonUtility.ToJson(this);
}
}
How I’m saving:
//
fun SavePuzzleToFirebase() {
PuzzleSphereInformation puzzleInfo = new PuzzleSphereInformation(creatorName, puzzleTargets);
string jsonData = JsonConvert.SerializeObject(puzzleInfo);
firebaseManager.AddPlayerCreatedPuzzle(JsonConvert.SerializeObject(targetData));
}
Object in FireBase:
{"creatorName":"test user","puzzleSphereTarget":[{"x":-0.2674436,"y":0.009597826,"z":0.894937754},{"x":0.2539144,"y":0.00647806656,"z":0.8653086}]}
But when I try to Deserialize the same object, it’s blank and I don’t know why:
_databaseReference.GetValueAsync().ContinueWithOnMainThread(task =>
{
if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
if (snapshot.Value != null)
{
foreach (DataSnapshot targetSnapshot in snapshot.Children)
{
string targetSnapshotJson = targetSnapshot.GetRawJsonValue();
// Debug.Log(targetSnapshotJson);
foreach (DataSnapshot targetInfo in targetSnapshot.Children)
{
string puzzleDataJson = targetInfo.GetRawJsonValue();
PuzzleSphereInformation puzzleInformation = JsonConvert.DeserializeObject<PuzzleSphereInformation>(puzzleDataJson);
Debug.Log("targets " + puzzleInformation.creatorName); // nothing
}
}
}
}
});
What am I doing wrong in this case? I am trying to deserialize the same object I serialized and no luck. Thx for any help
I’ve tried JsonUtility.FromJson, JsonConvert and neither has worked.
Raz781 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.