Search code examples
c#jsonunity-game-enginefilestreamhololens

Unity UWP filestream creating incomplete JSON file


I have a Unity project for the Hololens 2 where I need to be able to save user data to a JSON file and later retrieve it. The code I have currently works perfectly when emulating in Unity, however when I run it on the Hololens the JSON file it produces is incomplete. About halfway through it simply cuts off and isn't written to the file, I've also noticed it cuts off at the same place every time.

Code for writing file:

    // Save the tree structure to JSON format using DataContract.
    public void SerializeReportToJSON()
    {
        // traverse up to the root node
        while (currentCategory.parentCategory != null)
        {
            currentCategory = currentCategory.parentCategory;
        }
        DataContractJsonSerializer dcs = new DataContractJsonSerializer(typeof(DataFieldCategory));

        // Saves file to persistent data location on Hololens (and Desktop)
        using (FileStream fs = new FileStream(Path.Combine(Application.persistentDataPath, "InspectionData" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" +
            DateTime.Now.Day + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + ".json"), FileMode.Create))
        {
            dcs.WriteObject(fs, currentCategory);
        }  
    }

I've tried calling this function as a coroutine and as an async function but still run into the same issue. Perhaps I shouldn't use FileStream and replace it with whatever UWP uses?


Solution

  • Did some digging through the player log and found that the issue had nothing to do with how the file was being created or written to. Unity was attempting to serialize Vector3 which didn't have ahead of time code generated for, I created a new post here focused on the correct issue.

    Unity UWP Vector3 AOT code not generated, JSON serialization