0

So I have been building an app for a school assignment, its a betting app flavoured to my school's fundraising day. It allows you to bet on the outcomes of certain events on the day, such as which team will win the bubble soccer, or who will win the teacher vs student debate, and so on. I needed somewhere to store the many events and their outcomes, events and outcomes are separate objects. I was told the best way to store these was as binary. To do this I was told to create another app, that would allow me to create, edit and delete these objects, then serialize them to binary to be deserialized by the app later. The serializing to binary works. The deserializing on the apps side, not so much.

Heres the Serializer:

namespace MazBrokes_Editor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        {
        private List<Event> mEvents;
        private List<string> mEventNames = new List<string>();

        private Dictionary<Event, Outcome> mOutcomesToRemove = null;

        private List<string> mOutcomeNames;
        private static string file;
        private Event SelectedEvent = null;
        private Outcome SelectedOutcome = null;

        public MainWindow()
        {
            InitializeComponent();
        }


        private void btnEventAdd_Click(object sender, RoutedEventArgs e)
        {
            mEvents.Add(new Event(
                    txtEventName.Text,
                    txtEventType.Text,
                    txtEventDesc.Text,
                    new List<Outcome>()
                ));

        }

        private void btnAddOutcome_Click(object sender, RoutedEventArgs e)
        {
            if (txtEventOrigin.Text == "")
            {

              SelectedEvent.Outcomes.Add(new Outcome(txtName.Text, txtProbability.Text, txtPrizeDesc.Text, txtBetMultiplier.Text));

            }

            foreach (var item in mEvents)
            {

                if (item.eventName != txtEventOrigin.Text)
                {
                    continue;
                }

                else
                {
                    item.Outcomes.Add(new Outcome(txtName.Text, txtProbability.Text, txtPrizeDesc.Text, txtBetMultiplier.Text));
                }
            }
            Clear();
        }

        private void btnSerialize_Click(object sender, RoutedEventArgs e)
        {


            using (Stream stream = File.Open(file, FileMode.Create))
            {
                var binaryformatter = new BinaryFormatter();

                binaryformatter.Serialize(stream, mEvents);
            }

        }


            private void btnDeserialize_Click(object sender, RoutedEventArgs e)
            {
                FileStream fs = File.Open(file, FileMode.Open);

                BinaryFormatter formatter = new BinaryFormatter();


                mEvents = formatter.Deserialize(fs) as List<Event>;

                foreach(var item in mEvents)
                {
                    mEventNames.Add(item.eventName);
                }

                fs.Flush();
                fs.Close();
                fs.Dispose();

                lstDesirializedObjects.ItemsSource = mEventNames;

            }

            private void lstDesirializedObjects_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                    mOutcomeNames = new List<string>();

                   foreach (var item in mEvents)
                   {
                        if (lstDesirializedObjects.SelectedItem.ToString() != item.eventName)
                        {
                            continue;
                        }
                        else
                        {
                            txtEventName.Text = item.eventName;
                            txtEventDesc.Text = item.Description;
                            txtEventType.Text = item.eventType;
                            SelectedEvent = item;
                                foreach(var outcome in SelectedEvent.Outcomes)
                                {
                                    mOutcomeNames.Add(outcome.Name);

                                }
                                lstDesirializedOutcomes.ItemsSource = mOutcomeNames;

                        }
                   }

            }

        private void BtnEventEdit_Click(object sender, RoutedEventArgs e)
        {
            SelectedEvent.eventName = txtEventName.Text;
            SelectedEvent.Description = txtEventDesc.Text;
            SelectedEvent.eventType = txtEventType.Text;


        }

        private void lstDesirializedOutcomes_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            foreach (var item in SelectedEvent.Outcomes)
            {
                if (lstDesirializedOutcomes.SelectedItem.ToString() != item.Name)
                {
                    continue;
                }
                else
                {

                    txtName.Text = item.Name;
                    txtProbability.Text = item.oddsArray[0] + "/" + item.oddsArray[1];
                    txtPrizeDesc.Text = item.Prize;
                    txtBetMultiplier.Text = Convert.ToString(item.mPayout);
                    SelectedOutcome = item;

                }
            }
        }

        private void btnEditOutcome_Click(object sender, RoutedEventArgs e)
        {
            SelectedOutcome.Name = txtName.Text;
            SelectedOutcome.oddsArray = txtProbability.Text.Split('/');
            SelectedOutcome.Prize = txtPrizeDesc.Text;
            SelectedOutcome.mPayout = Convert.ToDouble(txtBetMultiplier.Text);

            Clear();

        }

        private void btnDeleteOutcome_Click(object sender, RoutedEventArgs e)
        {
            mOutcomesToRemove = new Dictionary<Event, Outcome>();
            foreach (var Event in mEvents)
            {
                foreach (var outcome in Event.Outcomes)
                {
                    if (SelectedOutcome != outcome)
                    {
                        continue;
                    }
                    else
                    {
                        //Event.Outcomes.Remove(outcome);
                        mOutcomesToRemove.Add(Event, outcome);
                    }
                }
            }
            foreach (var item in mOutcomesToRemove)
            {
                item.Key.Outcomes.Remove(item.Value);
            }
        }

        private void DeleteOutcome(Event evvent, Outcome outcome)
        {
            evvent.Outcomes.Remove(outcome);
        }

        private void btnDeleteevent_Click(object sender, RoutedEventArgs e)
        {
            mEvents.Remove(SelectedEvent);
        }

        public void Clear()
        {
            txtEventOrigin.Text = "";
            txtName.Text = "";
            txtProbability.Text = "";
            txtPrizeDesc.Text = "";
            txtBetMultiplier.Text = "";
        }
    }
}`

The Deserializer:

namespace MazBrokes
{
    class configParser
    {

        public static void SerializeParse()
        {

            BinaryFormatter formatter = new BinaryFormatter();
            SlidingTabFragment.UpcomingEvents.mEvents = formatter.Deserialize(MainActivity.assets.Open("MazBrokesData.bin")) as List<Events>;

        }


    }

}

I was thrown the exception detailed here: BinaryFormatter deserialize gives SerializationException.

I tried the recommended fix, but that only caused more errors.

Any help you can offer is super appreciated, and if I can provide any more info, Just say.

1 Answers1

0

Turns out, the answer was to scrap the use of binary altogether. The commenter Jason told me about JSON, I kept the editor programme but instead of serializing to binary it serialized to JSON, which the main app was able to interpret without errors.