I have a list of float vector List<Vector3> club3Dpts = new List<Vector3>();
and like to serialize to Json object to store in MySql.
club3Dpts has float vector.
club3Dpts.Add (new Vector3 (1.25f, 1.335f, 1.425f));//chipping
club3Dpts.Add (new Vector3 (1.26f, 1.331f, 1.625f));//chipping
club3Dpts.Add (new Vector3 (1.232f, 1.135f, 1.125f));//chipping
I like to serialize to json and it is implemented as
//Serialize to Json
string json = JsonUtility.ToJson(club3Dpts);
Debug.Log("json " + json);
But json has no data and it shows {} only in Debug.
How can I serialize list of float vector in Unity?
EDIT: I did the same thing as described in the similar post.
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
Then Serialize to Json as
string json = JsonHelper.ToJson(club3Dpts.ToArray());
Debug.Log("json " + json);
that json is saved in MySql database.
In retrieving back json data from MySql,
SqliteDatabase sqlDB = new SqliteDatabase (dbPath);
var result = sqlDB.ExecuteQuery("SELECT * FROM Record WHERE ID = (SELECT MAX(ID) FROM Record)");
var row = result.Rows [0];
BLEBridge.club3Dpts = JsonHelper.FromJson(row ["Club3DPoints"].ToString());
BLEBridge.club3Dpts is List<Vector3> type.
I have error as The type arguments for method JsonHelper.FromJson<T>(string) cannot be inferred from the usage. Try specifying the type arguments explicitly.
What could be wrong?