Hi everyone i have a problem with my json serealization. I'm using the Json.NET package under Unity: I'm searching to make a Database, editable on my application and stored on my server through wwwForm and a php file. I have no problem to create it and to push it on the net. The problem is, when i load it, the database has a new entry at the end. The Database Class is this:
public class Database {
public List<Army> armies { get; set;}
public Database() {
armies = new List<Army>();
armies.Add (new Army());
}
}
public class Army
{
public string faction { get; set;}
public List<Unit> army { get; set;}
public Army()
{
faction = "RED";
army = new List<Unit>();
army.Add (new Unit());
}
}
public class Unit
{
public string name { get; set;}
public float fissionTimer { get; set;}
public float HP { get; set;}
public int shield { get; set;}
public float strenght { get; set;}
public float movSpeed { get; set;}
public float attackSpeed { get; set;}
public float farmAggro { get; set;}
public int criticPossibility { get; set;}
public int armorPenetration { get; set;}
public bool isContagious { get; set;}
public int contagePossibility { get; set;}
public string imgName {get;set;}
public Unit()
{
name = "standard";
fissionTimer = 8;
HP = 100;
shield = 0;
strenght = 10;
movSpeed = 5;
attackSpeed = 0.1f;
farmAggro = 0.1f;
criticPossibility = 0;
armorPenetration = 0;
isContagious = false;
contagePossibility = 0;
imgName = "Red";
}
public Unit(string _name, float _fissionTimer, float _HP, int _shield, float _strenght, float _movSpeed, float _attackSpeed,
float _farmAggro, int _criticPossibility, int _armorPen, bool _iscontagious, int _contagePos, string _imgName)
{
name = _name;
fissionTimer = _fissionTimer;
HP = _HP;
shield = _shield;
strenght = _strenght;
movSpeed = _movSpeed;
attackSpeed = _attackSpeed;
farmAggro = _farmAggro;
criticPossibility = _criticPossibility;
armorPenetration = _armorPen;
isContagious = _iscontagious;
contagePossibility = _contagePos;
imgName = _imgName;
}
}
to serialize and deserialize i use those 2 methods:
IEnumerator LoadFile()
{
WWW www = new WWW(dbPath);
yield return www;
var _database = JsonConvert.DeserializeObject<Database> (www.text);
db = _database;
SendMessage ("loaded", SendMessageOptions.DontRequireReceiver);
}
IEnumerator SaveFile(Database db)
{
WWWForm form = new WWWForm();
string serialized = JsonConvert.SerializeObject (db);
form.AddField("theDatabase", serialized);
WWW www = new WWW(phpPath, form);
yield return www;
if (www.error == null)
Debug.Log ("saved" + serialized);
else
Debug.LogError ("error saving database");
}
The result of using the default constructor, serialized and deserialized is this:
{
"armies": [
{
"faction": "RED",
"army": [
{
"name": "standard",
"fissionTimer": 8,
"HP": 100,
"shield": 0,
"strenght": 10,
"movSpeed": 5,
"attackSpeed": 0.1,
"farmAggro": 0.1,
"criticPossibility": 0,
"armorPenetration": 0,
"isContagious": false,
"contagePossibility": 0,
"imgName": "Red"
}
]
},
{
"faction": "RED",
"army": [
{
"name": "standard",
"fissionTimer": 8,
"HP": 100,
"shield": 0,
"strenght": 10,
"movSpeed": 5,
"attackSpeed": 0.1,
"farmAggro": 0.1,
"criticPossibility": 0,
"armorPenetration": 0,
"isContagious": false,
"contagePossibility": 0,
"imgName": "Red"
}
]
}
]
}
There are 2 armies and 2 units. Where i am doing wrong? Thanks in advance
Data serialization is the process of converting an object into a stream of bytes to more easily save or transmit it. The reverse process—constructing a data structure or object from a series of bytes—is deserialization.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
Creating an object via deserialization is usually faster than initializing from a class. Storing the state of an object in a file or database can save time to process huge datasets in many data science projects. For example, you only need to pre-process the dataset once and save the model into a disk.
Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.
The reason this is happening is due to the combination of two things:
To fix this, you can either change your code such that your constructors do not automatically add default items to your lists, or you can configure Json.Net to replace the lists on deserialization rather than reusing them. The latter by can be done by changing the ObjectCreationHandling
setting to Replace
as shown below:
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ObjectCreationHandling = ObjectCreationHandling.Replace;
var database = JsonConvert.DeserializeObject<Database>(www.text, settings);
Best way would be to configure JSON.Net to replace the default values by
JsonSerializerSettings jsSettings = new JsonSerializerSettings
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
};
JsonConvert.DeserializeObject<Army>(jsonString, jsSettings);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With