Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:54:02+00:00 2026-06-12T06:54:02+00:00

I have an extension method for System.Object to serialize and deserialize objects using Json.Net

  • 0

I have an extension method for System.Object to serialize and deserialize objects using Json.Net. this is my Extension methods:

public static void SaveToFile(this object data, string FileName)
{
    using (StreamWriter writer = new StreamWriter(FileName))
    {
        string encode = WpfApplication.Helper.Encrypt(JsonConvert.SerializeObject(data));
        writer.Write(encode);
        writer.Close();
    }
}

public static void LoadFromFile<t>(this object data, string FileName)
{
    using (StreamReader reader = new StreamReader(FileName))
    {

        data = JsonConvert.DeserializeObject<t>(WpfApplication.Helper.Decrypt(reader.ReadToEnd()));
        reader.Close();
    }
}

and It’s the class that I want to deserialize:

public class CardPack
{
    #region Properties
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private List<FlashCard> cards;

    public List<FlashCard> Cards
    {
        get { return cards; }
        set { cards = value; }
    }

    private bool registered;
    public bool Registered
    {
        get { return registered; }
        set { registered = value; }
    }

    private int currentCardIndex;

    public int CurrentCardIndex
    {
        get { return currentCardIndex; }
        set { currentCardIndex = value; }
    }

    public string RegisterKey { get; set; }

    public string ViewName { get; set; }

    public List<FlashCard> TodayCards { get; set; }
    #endregion
    ~CardPack()
    {
        foreach (FlashCard card in cards)
        {
            card.Check();
        }
        currentCardIndex = 0;
        TodayCards = null;
        this.SaveToFile(string.Format(System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(@"file:\\")[1] + @"\Packs\{0}.json", name));
    }

but whenever I deserialize the class cards is empty and I don’t know how to resolve the problem. Can anybody help me?

Update
I find the error when I had this code:

public CardPack(string Name)
    {
        this.name = Name;
        this.LoadFromFile<CardPack>(string.Format(System.IO.Path.GetDirectoryName(
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split(@"file:\\")[1] + @"\Packs\{0}.json", name));
        foreach (var item in cards)
        {
            if (item.NextTime == null)
            {
                int a = 0;
            }
        }
        TodayCards = cards.Where(c => c.NextTime.Date == DateTime.Today).ToList();
    }

because the application closed when it tries to run foreach (var item in cards)!
I asked here and found out that cards is empty!

update2 I serialized the CardPack object with a little different structure. in previous structure Cards property was read-only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:54:03+00:00Added an answer on June 12, 2026 at 6:54 am

    I found that data = JsonConvert.DeserializeObject<t>(WpfApplication.Helper.Decrypt(reader.ReadToEnd())); in extension method doesn’t change to ‘data’ class then Cards in CardPack is always null. I’ll ask a question to find out why I cant set the class from it’s extension method later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having an issue using BinaryFormatter.Serialize. I have this generic extension method to
I have an extension : public static T GetPrivatePropertyValue<T>(this object obj, string propName) {
I have an extension method here like this: public static class Extensions { public
I have a method which i want to convert to Extension Method public static
I am making an extension. public static MvcHtmlString Image(this HtmlHelper helper, string src, object
This extension method does not work on two separate development machines: public static string
I have this little bit of code: using System; using System.Web.Mvc; public class SecureFilter
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
I have an extension method below, but when I run this, the foreach gives
I have an extension method to dynamically filter Linq to Entities results using string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.