1

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?

CocoaMilka
  • 25
  • 7
  • have you tried flushing it after the write? – BugFinder Jul 05 '23 at 17:09
  • in general I wouldn't use `new FileStream` but rather go through e.g. [File.Open](https://learn.microsoft.com/dotnet/api/system.io.file.open#system-io-file-open(system-string-system-io-filemode-system-io-fileaccess-system-io-fileshare)) ... have you checked for any exceptions in the [player log](https://lh3.googleusercontent.com/-hdZDQ7vM1mM/W5AqSPbDKwI/AAAAAAAAQFE/jpDacnOjGOwAQKLgIa_gS8mh_29xVn6EQCHMYCw/s1600-h/image%255B17%255D) (replace `17852LocalJoost....` with your app - app needs to be closed before the log is flushed)? – derHugo Jul 06 '23 at 07:47

1 Answers1

0

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

CocoaMilka
  • 25
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '23 at 22:31