C
C#2y ago
Nyfos

✅ What is DataContractSerializer capable of serializing?

Hi! <:cat_hi:1123628423944867931> I'm currently using DataContractSerializer.WriteObject(...) and DataContractSerializer.ReadObject(...) to save and load my game data. I was wondering if there was a list of all serializable types somewhere. It would be perfect if I was able to serialize things like Dictionaries, Vector2/Vector3, instead of converting them to multiple arrays... Thank you for your help!
12 Replies
Nyfos
NyfosOP2y ago
P.S.: please mention me along with your response
friedice
friedice2y ago
what about json?
Nyfos
NyfosOP2y ago
Wdym? Oh, yeah, I understand. I said "game data", but wanted to say "player saves", sorry. 😅
friedice
friedice2y ago
Vector2 vector = new Vector2(10.5f, 7.2f);

string jsonString = JsonSerializer.Serialize(vector);
Console.WriteLine(jsonString);

Vector2 deserializedVector = JsonSerializer.Deserialize<Vector2>(jsonString);
Console.WriteLine($"Deserialized Vector: {deserializedVector}");
Vector2 vector = new Vector2(10.5f, 7.2f);

string jsonString = JsonSerializer.Serialize(vector);
Console.WriteLine(jsonString);

Vector2 deserializedVector = JsonSerializer.Deserialize<Vector2>(jsonString);
Console.WriteLine($"Deserialized Vector: {deserializedVector}");
is this what you're looking for?
Nyfos
NyfosOP2y ago
This can solve my problem, but I wasn't able to use that one... If I remember correctly, it was due to security problems, or something else.. I can check, but there is no way to do it using DataContractSerializer, instead of convertir dictionaries to arrays?
friedice
friedice2y ago
security problems?
Nyfos
NyfosOP2y ago
Yeah, something I found on https://learn.microsoft.com/
Microsoft Learn: Build skills that open doors in your career
Gain technical skills through documentation and training, earn certifications and connect with the community
Nyfos
NyfosOP2y ago
Alright. Is JsonSerializer capable of Serializing classes?
Nyfos
NyfosOP2y ago
Ok, was something else:
friedice
friedice2y ago
yeah it should
public class PlayerPosition
{
public Vector2 Position { get; set; }
public Vector2 Direction { get; set; }
public Vector2 Target { get; set; }
}
public class Player
{
public PlayerPosition Position {get;set;}
}
var player = new Player(){//blabla data here}

string jsonString = JsonSerializer.Serialize(player);
Console.WriteLine(jsonString);

Player deserializedPlayer = JsonSerializer.Deserialize<Player>(jsonString);
public class PlayerPosition
{
public Vector2 Position { get; set; }
public Vector2 Direction { get; set; }
public Vector2 Target { get; set; }
}
public class Player
{
public PlayerPosition Position {get;set;}
}
var player = new Player(){//blabla data here}

string jsonString = JsonSerializer.Serialize(player);
Console.WriteLine(jsonString);

Player deserializedPlayer = JsonSerializer.Deserialize<Player>(jsonString);
im sure there's other ways to store save data aside from json as well not a game dev though <a:cat_no:768672517325717544>
Nyfos
NyfosOP2y ago
Oh, good to know! Thanks!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server