Other
Unity imposed recursion depth of Json data?
Is Unity imposing some maximum depth level on JSON objects? I will include the full code at the bottom, but essentially I have something like this:
I have a property List CustomClassA where CustomClassA also has a property that is a List CustomClassB. Both CustomClassA and CustomClassB are properly marked up for serialization.
Now if I use JsonUtility.ToJson on an individual element in List CustomClassA , in other words on a single CustomClassA, then the JSON output is as expected. It properly contains all the elements of the List CustomClassB that is contained by the CustomClassA object. So I know that the JSON serialization of a custom object in a List works.
However if I try to use JsonUtility.ToJson on the entire List CustomClassA I just get back an empty JSON object "{}" rather than a nested array of objects as expected.
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
[Serializable]
public class ComponentProperty
{
// Name of the property
public string Key;
// Units to display after the value, such as kg, m/s, m, etc.
public string Units;
[SerializeField]
private string value;
public string GetStringValue()
{
return value;
}
public float GetFloatValue()
{
float outValue = float.NaN;
float.TryParse(value, out outValue);
return outValue;
}
public int GetIntValue()
{
int outValue = int.MinValue;
int.TryParse(value, out outValue);
return outValue;
}
public bool GetBoolValue()
{
bool outValue = false;
bool.TryParse(value, out outValue);
return outValue;
}
public void SetValue(string inValue)
{
value = inValue;
}
public void SetValue(float inValue)
{
value = inValue.ToString();
}
public void SetValue(int inValue)
{
value = inValue.ToString();
}
public void SetValue(bool inValue)
{
value = inValue.ToString();
}
}
[Serializable]
public class HardwareComponent
{
public string Identifier;
public string Name;
public string Description;
[SerializeField]
private List<ComponentProperty> properties;
public List<ComponentProperty> Properties
{
get
{
if (properties == null)
properties = new List<ComponentProperty>();
return properties;
}
}
}
[Serializable]
public class JSONTest : MonoBehaviour
{
[SerializeField]
public List<HardwareComponent> Components;
// Use this for initialization
void Start ()
{
Components = new List<HardwareComponent>();
for (int i = 0; i < 10; i++)
{
HardwareComponent comp = new HardwareComponent();
comp.Identifier = string.Format("Component_{0}", i);
comp.Name = string.Format("Component {0}", i);
comp.Description = "Dummy Description";
ComponentProperty newProp = new ComponentProperty();
newProp.Key = "String Property";
newProp.Units = "kg";
newProp.SetValue("A String");
comp.Properties.Add(newProp);
ComponentProperty newProp2 = new ComponentProperty();
newProp2.Key = "Float Property";
newProp2.Units = "m";
newProp2.SetValue(1.14f);
comp.Properties.Add(newProp2);
// This will print out proper JSON including the list
print(JsonUtility.ToJson(comp));
}
// This will generate an empty JSON object
print(JsonUtility.ToJson(Components));
}
}
Follow this Question
Related Questions
JsonUtility not making JSONs 1 Answer
Armed Unity Parkour 0 Answers
Recursive JSON serialization / serialize list of custom serializable objects? 1 Answer
Deserialize Json array with mixed types 0 Answers