Serialize whole instance with child instances (class serialization) and save to file - c#

I'm building a game with Unity3D. I have a class called GameInstance containing all the current game instance data in it. It contains multiple sub classes for instance the Player property would return a Player object.
Yet all objects are simple key/values objects, it's only data. Now I'd need to serialize all of this and save it to a file so I can reload it to restart the game where it left.
That's what I basically intent to do, maybe somebody would have a better suggestion but yet that's the most flexible option I found.
I used .NET XML object serialization in the past but it's been a while and I'd need to have a more direct advice on this. Should I serialize to XML or JSON for example?
TL;DR: I want to serialize a whole class with all its content with C#/.NET in a Unity3D project. How should I proceed?
Thanks!

I personally prefer json. If you're using json.NET this will be as simple as;
string json = JsonConvert.SerializeObject(MyObject);
or to compact it;
File.WriteAllText("./myfile", JsonConvert.SerializeObject(MyObject));
Then to deserialize you would just do;
MyObject obj = JsonConvert.DeserializeObject<MyObject>(File.ReadAllText("./myfile"));
EDIT: In response to the exception, you want to use this overload which allows you to change the serilization settings;
JsonConvert.SerializeObject(ResultGroups,
new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});

Related

Serializing an object frequently whenever there is a value change in properties

I want to serialize an object whenever there is a change in the property value. Currently, INotifyPropertyChanged is not implemented in the class. What is the best practice to serialize the object frequently? Serialization should not hamper the application's performance. Please suggest NuGet packages if they can handle serializing the object frequently
Kindly Check Newtonsoft.Json
var jsonString = JsonConvert.SerializeObject(model);

Serialize "JsValue" in Jint

At the moment, I'm trying to figure out how to save a Jint.NET JavaScript environment to file, so I can load it again later.
However, I'm having major trouble trying to serialize/deserialize the Jint.Native.JsValue class. Since it has no constructor, Newtonsoft doesn't like deserializing to it, and when serializing, it ignores all private properties, resulting in only the Type field being saved.
string saved = JsonConvert.SerializeObject(someJsValue); //output: {Type: x}
JsonConvert.DeserializeObject<JsValue>(saved); //error: no constructor
Is there any way around this so I can load/save this properly?

How to deserialize json and not make multiple instance of the class, in unity 3d and simplejson?

I am trying to parse a json or deserialize a json of the following format. Because of brain fart or not--I cant seem to get it to deserialize--basically I would create a class object with the json structure. Now I want to keep updating this object with new data from next json I receive from server call and now I don't want to create a new object every time...how do I do it? What am I missing?
I am doing this in C# (new to it) and Unity 3D, not using any serialization lib, just simplejson and JsonUtility (yes I know it sucks). I have complex inner structure.
{
JsonArray[5] -> more jsonArray inside
JsonArray[5]
JsonArray[5]
JsonArray[5]
JsonArray[5]
}
SimpleJson hasn't this feature, you need write your own implemention.
JsonUtility has one, use JsonUtility.FromJsonOverwrite

NewtonSoft.Json error

Hi I'm quite new to C# and I'm trying to make a text editor that saves and loads Plaintext formats. I've used NewtonSoft.Json NuGet package, but I'm getting an error. I've stated a string called textToLoad, which is set to a JsonConvert.DeserializeObject. Only thing is, it says it can't convert an object to a string! I tried toString(); but it still had the same error.
It is kind of hard without the code. The process of serializing and deserializing is pretty straight forward using Json.Net. So this is an example from their documentation:
YourType yourObject= new YourType();
yourObject.Property="something";
string output = JsonConvert.SerializeObject(yourObject);
//For some reason you want this to be string, but is the type you serialized in the first place
YourType textToLoad= JsonConvert.DeserializeObject<YourType>(output);
This outlines the basic works of serializing and deserializing. But we don't really know the details of your implementation.
Hope it helps.
You can't deserialize into a string like that. At simplest form you started with JSON in the form of:
{ value: "someString" }
If you want something out of it, you must deserialize and then get the value from it.
dynamic foo = JsonConvert.DeserializeObject<dynamic>(theJson);
var textToLoad = foo.value.ToString();
You must deserialize to something in order to inspect and get properties from it.
[Edit] - Perhaps I'm not understanding. But if you share code, I'll update my answer.

c# How to deserialise from an XML file and display the properties in a windows form?

I have an XML file and and a class representing the XML structure. I have deserialized the XML file into the class. I want to display all the properties into the form. Can anyone suggest if it is possible and how to deserialize the XML into datagridview?
For the most part, de-serialization involves have a valid class structure and or object to de-serialize the thing into, whether that is Xml deserialization, or Binary, or SOAP...etc etc. You have the class, so the de-serialize step will be simple enough, just create the serializer and call deserialize(xml). As for the second part...well, you have to create a datagridview and bind to the object.
so, yes, totally possible. without access to the object, the class definition, or the xml, this is all the information I can give you.
Some basic code structure.
XmlSerializer ser = new XmlSerializer(typeof(MyClass));
MyClass obj = ser.DeSerialize(xmlDoc);
MyDataGridView.DataSource = obj;

Categories

Resources