- Home /
Binary file Deserialize to list?
In order to deserialize a binary file into a list would you have to have saved that list into a binary file?
string Name=NameText.text;
string Image=ImageText.text;
//Grab the input from a form and save it as binary
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = File.Create(Application.persistentDataPath + "/Name.dat");
NameData nd= new NameData();
nd.Name=Name;
nd.Image= Image;
bf.Serialize(fs, nd);
fs.Close();
//This part works just fine.
I want to load the data back in as a list.
if(File.Exists(Application.persistentDataPath + "/Name.dat"))
{
BinaryFormatter bf= new BinaryFormatter();
FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , FileMode.Open);
OurNameList=(List<TheNameClass>)bf.Deserialize(fs) as List<TheNameClass>;
fs.Close();
//This throws an error
InvalidCastException: Cannot cast from source type to destination type. So do I need to save the list prior to loading it into a list or can I just load the data into a list? What am I missing in the loading part? I plan to take the list and loop through it instatiating a text prefab to show the two strings that make up the class. Thanks!
You have to cast the data into the Type that you serialized it as first. You could also use an ISerializationSurrogate to control how certain Types are serialized and deserialized.
Thanks Cherno, but I must be missing something...
if(File.Exists(Application.persistentDataPath + "/Name.dat"))
{
BinaryFormatter bf= new BinaryFormatter();
NameData nd= new NameData();
FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , File$$anonymous$$ode.Open);
OurNameList=(List<Name>)bf.Deserialize(fs) as NameData;
fs.Close();
}
This doesn't work give me the error that it can't convert the list to the NameData. When I try it with the NameData nd= new NameData(); it claims there is no namespace name nd that could be found, even though it's defined right there. Any other thoughts?
OurNameList=(List<Name>)bf.Deserialize(fs) as NameData;
You try to cast as a List but the deserialized object is of Type NameData (I assume from the "as NameData" part). Objects of Type NameData can't be implicitly cast as Lists. Either serialize a List and deserialize it, or deserialize as NameData and fill a List with the data manually in some way.
So put a list in the serializable class ins$$anonymous$$d of the two strings I have now.
[Serializable]
public class NameData
{
//public string Name;
//public string Image;
public List<OurName> OurNameList;
}
Then add the two strings to the list as they come in from the form. public string OurName{get; set;} public string Image{get; set;}
public Name( string Name, string Image)
{
this.OurName=Name;
this.Image=Image;
}
Is that right? Just trying to learn this stuff...
... It depends. At this point, it would help if you would provide as much relevant information as possible. The claees involved, what you want to do with them, and how they are (de)serialized).
Hopefully this will give a clear picture of what I'm trying to do. Let me know if there is anything unclear.
[Serializable] public class SaveClass { public List OurNameList; What is the datatype I should use here? The class with the two strings or string? Previously, I had: public string Name; public string Image;
}
public class TheName : $$anonymous$$onoBehaviour { public string Name{get; set;} public string Image{get; set;}
public TheName( string name, string image) { this.Name=name; this.Image=image; } } Do I even need this class any more?
class $$anonymous$$ain$$anonymous$$anager { public List OurNameList; Which data type do I use here? The script or string since there are two strings? This is the main part of my question.
Start() { List OurNameList = new List();
}
public void ChangeInput() { string Name=nameText.text; string Image=imageText.text; OurNameList.Add(Name); OurNameList.Add(Image); Debug.Log(OurNameList.Count); for(int i=0; i
The data is then serialized and saved via a binary formatter. Then it is read and deserialized to be displayed on screen with an instantiated prefab with the two strings as a text object.
public void ShowTheName() {
//Display The List Items
for(int i = 0; i< OurNameList.Count; i++)
{
GameObject bnObj = Instantiate(NamePrefab); (Which has two text items)
Name tmpName = OurNameList[i];
bnObj.GetComponent<NameScript>().DisplayName(tmpName.Name, tmpName.Image);
bnObj.transform.SetParent(Displayparent);
}
}
else
{
Debug.Log("File doesn't exist, please save one!");
}
}
public class NameScript : $$anonymous$$onoBehaviour { public GameObject Name; public GameObject Image;
public void DisplayName(string name, string image )
{
this.Name.GetComponent<Text>().text=name;
this.Image.GetComponent<Text>().text=image;
}
}
The Types depend on what you need to do, I can't help you with that, that's completely up to do.
Take a look at the syntax for the Generic List; they are commonly declared like this:
public List<string> myList = new List<string>();
Right. I also know that you can put a class in as a type. However when you are saving a binary file and put a list in the "save class" and you have two strings that go in it do you make a separate class and put it in there or do you, in my case, use string?
I'm getting back to this project, so let me try to explain: This is supposed to take input from two fields, one for a name and one for image, both strings. That data is added to a list and saved as a binary file. I want to display that data with a prefab.
[Serializable]
public class NameData
{
//public string Name;
//public string Image;
public List<string> NameList = new List<string>(){"Name", "Image"};
}
public void LoadName()
{
if(File.Exists(Application.persistentDataPath + "/Name.dat"))
{
BinaryFormatter bf= new BinaryFormatter();
FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , File$$anonymous$$ode.Open);
NameList=(NameData)bf.Deserialize(fs);
fs.Close();
}
So I want to take the NameData and deserialze it to a list. Does that make sense?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Serializing/deserializing List of custom class. 0 Answers
A node in a childnode? 1 Answer
Save string and list to binary file 1 Answer