1

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?

derHugo
  • 83,094
  • 9
  • 75
  • 115
batuman
  • 7,066
  • 26
  • 107
  • 229
  • It's an array which is not supported out of the box abd you need a wrapper for that .. To save you some time, that should be `JsonHelper.ToJson(club3Dpts.ToArray());`. Grab `JsonHelper` from the duplicate. – Programmer Apr 16 '18 at 03:42
  • 1
    @Programmer Thanks it is really helpful. – batuman Apr 16 '18 at 05:12
  • @Programmer I updated in EDIT. I still have {} in Debug log. What could be wrong? – batuman Apr 16 '18 at 14:58
  • `JsonHelper.ToJson` **not** `JsonUtility.ToJson` – Programmer Apr 16 '18 at 14:58
  • @Programmer sorry if I use JsonHelper, i have parameter not matching error. – batuman Apr 17 '18 at 04:57
  • 1
    All these comments are unnecessary. Please see my first comment in this post. That should have solved your problem in one try. Just copy what's in that comment directly and paste it if typing it is causing error. – Programmer Apr 17 '18 at 05:04
  • @Programmer yes it worked. – batuman Apr 17 '18 at 14:57
  • @Programmer I change back to `List from string as BLEBridge.club3Dpts = JsonHelper.FromJson(row ["Club3DPoints"].ToString());` Then I have error as `The type arguments for method JsonHelper.FromJson(string) cannot be inferred from the usage. Try specifying the type arguments explicitly. `row ["Club3DPoints"].ToString() is Json data converted before and retrieved from database. I like to change back to `List`. Why I have this error? – batuman Apr 18 '18 at 15:23
  • Sorry you have to edit your question and add that code there because I have no idea what you're doing. I don't even know how `row` declared. – Programmer Apr 18 '18 at 15:37
  • @Programmer I have edited the question. And updated in EDIT. Thank you. – batuman Apr 19 '18 at 06:36
  • Again, I don't know what type `row` is. Instead of writing `var row` change that to something like `string row ` or whatever it is. By the way, your database seems more like SQLite than MySql – Programmer Apr 19 '18 at 06:44
  • @Programmer Yes sorry, sqlite. – batuman Apr 19 '18 at 08:49
  • @Programmer strange. I even do as `string a = "test";` `BLEBridge.club3Dpts = JsonHelper.FromJson(a);` so string is parameter. still have same error. – batuman Apr 19 '18 at 09:16
  • @Programmer i need to change as `Vector3[] BLEBridge.club3Dpts = JsonHelper.FromJson(row ["Club3DPoints"].ToString ());'. Then it works. – batuman Apr 19 '18 at 09:51

0 Answers0