Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kansokusha · Sep 27, 2014 at 09:50 AM · serializationlitjson

Error during deserialization. Objects 'out of scope'.

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.
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 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<Player>(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<string, int> AbilityScores { get; set;}
         
         public Player (){
             AbilityScores = new Dictionary<string, int>();
         }
 
         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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

LitJSON Problem 1 Answer

How to export Serialized Data with my Build 0 Answers

"Serialization depth limit exceeded" with non-serializable classes 1 Answer

" Binary Save" Stuff - Workaround for Vector3 Serialization 3 Answers

Coding help: How to use xml serialization 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges