❔ Deserializing Json to class

I have a text file wich I save my json to but when I try to deserialize it it never works. My json that is serialized looks fine bcs it even works when I run my json through an online converter. My Library class has 1 property that is an array of Movies.
json = File.ReadAllText(FileName);
Library test = JsonConvert.DeserializeObject<Library>(json);
Debug.WriteLine(test._Movies.Length);

**Structure classes**
public class Library
{
public Movie[] _Movies { private set; get; }
public Library(Movie[] movies)
{

_Movies = movies;
}
public Library()
{
_Movies = new Movie[0];
}
}
public class Movie
{
public string _title { private set; get; }

[JsonIgnore]
public Image _Picture { private set; get; }
public string _BasePic { private set; get; }
public Movie(string title, Image picture)
{
_title = title;
_Picture = picture;
using (_Picture)
{
using (MemoryStream m = new MemoryStream())
{
_Picture.Save(m, _Picture.RawFormat);
byte[] imageBytes = m.ToArray();

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
_BasePic = base64String;
}
}

}
}
json = File.ReadAllText(FileName);
Library test = JsonConvert.DeserializeObject<Library>(json);
Debug.WriteLine(test._Movies.Length);

**Structure classes**
public class Library
{
public Movie[] _Movies { private set; get; }
public Library(Movie[] movies)
{

_Movies = movies;
}
public Library()
{
_Movies = new Movie[0];
}
}
public class Movie
{
public string _title { private set; get; }

[JsonIgnore]
public Image _Picture { private set; get; }
public string _BasePic { private set; get; }
public Movie(string title, Image picture)
{
_title = title;
_Picture = picture;
using (_Picture)
{
using (MemoryStream m = new MemoryStream())
{
_Picture.Save(m, _Picture.RawFormat);
byte[] imageBytes = m.ToArray();

// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
_BasePic = base64String;
}
}

}
}
15 Replies
hanslanda5019
hanslanda5019OP2y ago
And my debug always returns 0
Google
Google2y ago
I ran some non-JSON through an online converter and it worked fine. When I tried it in C#, I got frustrated because it didn't work... because it wasn't JSON. Example:
{"character":"⺀","definition":"ice","pinyin":[],"decomposition":"?","radical":"⺀","matches":[null,null]}
{"character":"⺈","pinyin":[],"decomposition":"?","radical":"⺈","matches":[null,null]}
{"character":"⺊","pinyin":[],"decomposition":"⿰丨?","etymology":{"type":"ideographic","hint":"A crack on an oracle bone; compare 卜"},"radical":"⺊","matches":[[0],null]}
{"character":"⺀","definition":"ice","pinyin":[],"decomposition":"?","radical":"⺀","matches":[null,null]}
{"character":"⺈","pinyin":[],"decomposition":"?","radical":"⺈","matches":[null,null]}
{"character":"⺊","pinyin":[],"decomposition":"⿰丨?","etymology":{"type":"ideographic","hint":"A crack on an oracle bone; compare 卜"},"radical":"⺊","matches":[[0],null]}
in https://json2csharp.com/ works just fine, but it's not JSON. So you're probably best off checking the JSON.
hanslanda5019
hanslanda5019OP2y ago
This is the Json I am trying to convert
hanslanda5019
hanslanda5019OP2y ago
I Serialized it like this File.WriteAllText(FileName, JsonConvert.SerializeObject(Lib)); @JamesK.Polk I am really stuck do you have any idea?
Google
Google2y ago
I don't know how to deserialize to a class like that. My models are usually just super simple/dumb classes. You're opening a stream during deserialization. I've never seen that before. I actually have a class where I serialize/deserialize stringified images, but I don't know how to help you with the way you want to do it.
hanslanda5019
hanslanda5019OP2y ago
What are stringified images?
Google
Google2y ago
iVBORw0KGgoAAAANSUhEUgAAAE4AAACZCAYAAACBkQyKAAAABGdBTUEA...
hanslanda5019
hanslanda5019OP2y ago
oh so just a string I could try that
Google
Google2y ago
hanslanda5019
hanslanda5019OP2y ago
I am just stuck on saving an image to json
Google
Google2y ago
The image I just provided is in a JSON file I store then I compress the entire file for the user so they can transfer something smaller 9256 kb => 552kb compressed
hanslanda5019
hanslanda5019OP2y ago
And how did you convert the Image to a string? And you can serialize /desirialize BitMapImage just fine?
Google
Google2y ago
Yeah. I just hacked a lot of stuff together. I haven't looked at these methods since.
hanslanda5019
hanslanda5019OP2y ago
aight ty man
Accord
Accord2y ago
Looks like nothing has happened here. 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