C
C#7mo ago
koray bey

Saving Feature Help

I got a list I use in my app want to add a saving feature but don't know which filetype to use besides txt whats your recommendation?
21 Replies
𝗝𝗮𝗺𝗲𝘀
It depends heavily on the file's content encoding .dat/.bin for Binary files .config/.cfg/.env for environmental configuration (generaly in plain text) .json/.xml/.html for structured readable information (commonly serializable) NO EXTENSION for special files like lockfile What do you think? @naber top
koray bey
koray beyOP7mo ago
probably i will use .json im trying to learn how to use it actually rn
𝗝𝗮𝗺𝗲𝘀
Take a look on how to use json with a Newtonsoft.json (library) @naber top
var myjObject = new
{
Name = "Dummy",
IsHappy = true
Value = 10000
};

var json = JsonConvert.SerializeObject(myjObject, Formatting.None);
// will result in something like { "Name": "Dummy", "IsHappy": true, "Value": 10000 }

var bringBack = JsonConvert.DeserializeAnonymousType(json, myjObject);

bringBack == myjObject; // true
var myjObject = new
{
Name = "Dummy",
IsHappy = true
Value = 10000
};

var json = JsonConvert.SerializeObject(myjObject, Formatting.None);
// will result in something like { "Name": "Dummy", "IsHappy": true, "Value": 10000 }

var bringBack = JsonConvert.DeserializeAnonymousType(json, myjObject);

bringBack == myjObject; // true
if you want a prettified json output you can use Formatting.Idented
koray bey
koray beyOP7mo ago
would this work this the observable collections?
Jimmacle
Jimmacle7mo ago
newtonsoft.json is more or less obsolete system.text.json is what you should be using unless it can't do what you want for some reason
mindhardt
mindhardt7mo ago
And please don't use anonymous objects
Mustafa
Mustafa7mo ago
you could use System.Text.Json to save content (should be able to install it through nuget)
mindhardt
mindhardt7mo ago
For netfx yes, for .net it comes as a part of the bcl
Mustafa
Mustafa7mo ago
for whatever reason it wasn't included by default in my .net project
𝗝𝗮𝗺𝗲𝘀
If it doesn't work with the observable collections and you don't want to implement it manually I don't see anything wrong with extracting the collection content to an array then serializing to the file, you can after retrieve the objects when deserializing and recreating the collection from the start I use anonymous types when desserializing responses from client's APIs, they can be pretty gigantic, creating classes to them would be a waste of time just for a single piece of information needed.
var definition = new[] {
new {
OrderId = (string) default!,
ClientName = (string) default!,
OrderPrice = (float) 0
}
}.ToList();


// api response | i dont know exacly why but it's not that uncommon to see a json string text inside a json from an api response
// probably shouldn't be like that but it is what it is
var ordersJsonText = JsonConvert.DeserializeObject<JObject>()!.SelectToken("response.invoicing.orders")
var orders = JsonConvert.DeserializeAnonymousType(ordersJsonText, definition);
var definition = new[] {
new {
OrderId = (string) default!,
ClientName = (string) default!,
OrderPrice = (float) 0
}
}.ToList();


// api response | i dont know exacly why but it's not that uncommon to see a json string text inside a json from an api response
// probably shouldn't be like that but it is what it is
var ordersJsonText = JsonConvert.DeserializeObject<JObject>()!.SelectToken("response.invoicing.orders")
var orders = JsonConvert.DeserializeAnonymousType(ordersJsonText, definition);
Jimmacle
Jimmacle7mo ago
you don't need to model every property, just the ones you need there is no reason to use an anonymous type for ths, it just makes the code more error prone
koray bey
koray beyOP7mo ago
what do you mean by modeling mvvm? :oh:
Jimmacle
Jimmacle7mo ago
no, i mean making a class that matches the shape of the json
koray bey
koray beyOP7mo ago
oh
Jimmacle
Jimmacle7mo ago
public record OrderInfo(string OrderId, string ClientName, float OrderPrice);

JsonSerializer.Deserialize<List<OrderInfo>>(json);
public record OrderInfo(string OrderId, string ClientName, float OrderPrice);

JsonSerializer.Deserialize<List<OrderInfo>>(json);
would be the safe way to do james's example the code is actually shorter than the "convenient" anonymous method 😛
koray bey
koray beyOP7mo ago
what is anonymous in this context i still don't understand :Smadge:
Jimmacle
Jimmacle7mo ago
new {
OrderId = (string) default!,
ClientName = (string) default!,
OrderPrice = (float) 0
}
new {
OrderId = (string) default!,
ClientName = (string) default!,
OrderPrice = (float) 0
}
is an anonymous object, because it isn't a named type
𝗝𝗮𝗺𝗲𝘀
YEA! Records. SCIENCE!
koray bey
koray beyOP7mo ago
is it normal that stuff kind of overwhelms me to get it together am I in the wrong path or its just a part of the process
Jimmacle
Jimmacle7mo ago
depends how much experience you have with C#, if not a lot then it's understandable
koray bey
koray beyOP7mo ago
well im a beginner I just know the foundational c# stuff (the only thing I completed as a course)
Want results from more Discord servers?
Add your server