Quantcast
Channel: Latest Questions by Kansokusha
Viewing all articles
Browse latest Browse all 10

Error during deserialization. Objects 'out of scope'.

$
0
0
Greetings everyone. I am trying to use seriallization/deserialization for my project, but am running on a very frustrating problem and I cannot figure out what's going on. ***[Long problem history section - skip if you think its too long]*** First let me explain a bit of history of the problem. My first attempt was trying to use a serializer I found on CodeProject called UniversalSerializer. You can find it [here][1]. I was serializing in binary format with this. With that I run to some incompatibilities with mono (at least that is what I figured). I could find an exception and it reported an error with reflection. I decided to switch gears and try Json serialization for which I used Newtonsoft's Json.Net. *Note that I had build an indipendant .Net C# project to test serialization and there works great. everything works great. Serialization deserialization, all tested with unit test etc.* In unity though, deserialization fails (Serialization works). I thought that maybe again Json.Net was not compatible with unity. Then I found [LitJson][2] and tried to use this but it also fails Deserialization and I can't figure out what the problem is. ***[/Long problem history section]*** I have tried in the end making the simplest example I could think of just to see if the problem was deserializing my data model. Here is this code: using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using System.IO; using LitJson; public class TestSerialization : MonoBehaviour { // Use this for initialization void Start () { Player player = new Player(); player.CreateCharacter (); using(FileStream fs = new FileStream(@"D:\test.json", FileMode.Create, FileAccess.ReadWrite)) using(StreamWriter sw = new StreamWriter(fs)) { JsonWriter writer = new JsonWriter(sw); //This works JsonMapper.ToJson(player, writer); } Player loadedP; try{ using(FileStream fs = new FileStream(@"D:\test.json", FileMode.Open, FileAccess.Read)) using(StreamReader sr = new StreamReader(fs)) { JsonReader reader = new JsonReader(sr); //This fails. loadedP = JsonMapper.ToObject(reader); //At this point loadedP and reader are 'out of scope' and an exception is caught but... int wait = 1; } } catch (Exception ex) { //...Even 'ex' is 'out of scope' at this point... } } // Update is called once per frame void Update () { } public class Player{ public string Name { get; set; } public string Class { get; set; } public Dictionary AbilityScores { get; set;} public Player (){ AbilityScores = new Dictionary(); } public void CreateCharacter() { Name = "Testname"; Class = "TestClass"; AbilityScores.Add("STR", 14); AbilityScores.Add("DEX", 14); AbilityScores.Add("CON", 14); AbilityScores.Add("INT", 14); AbilityScores.Add("WIS", 14); AbilityScores.Add("CHA", 14); } } } As you can see in the comments on the code, the error occurs at deserialization and then everything goes out of scope. The same thing happens with every serializer i've tried. Because iam posting from a different pc I can't reproduce the error that was reported by UniversalSerializer, I will do so as soon as I get back to the office. That's all I think. If you need any more information just ask. All help is much appreciated and thank you in advance! [1]: http://www.codeproject.com/Articles/620154/UniversalSerializer [2]: http://lbv.github.io/litjson/

Viewing all articles
Browse latest Browse all 10

Latest Images

Trending Articles





Latest Images