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

  • Home
  • SEARCH
  • 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 3314056
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:11:14+00:00 2026-05-17T22:11:14+00:00

In my music/rhythm game, I using serialization in order to save user-created simfiles (think

  • 0

In my music/rhythm game, I using serialization in order to save user-created simfiles (think music tabs or notecharts). Nothing too earth-shattering there. But, I’m using a DataContract in order to perform the serialization, because:

1) I need private and protected fields serialized as well. I don’t care if they’re visible, mainly due to…
2) I’d like for the user to be able to edit the serialized file in his/her favorite text editor and be able to load these into the game (these files will be used to represent musical notes in the game, think StepMania simfiles).

One of my custom datatypes I’d like to serialize is a Fraction class I’ve created:

using System.Runtime.Serialization;

namespace Fractions {
    [DataContract(Namespace="")] // Don't need the namespaces.
    public sealed class Fraction {
        // NOTE THAT THESE ARE "READONLY"!
        [DataMember(Name="Num", Order=1)] private readonly long numer;
        [DataMember(Name="Den", Order=2)] private readonly long denom;

        // ...LOTS OF STUFF...

        public static Fraction FromString(string str) {
            // Try and parse string and create a Fraction from it, and return it.
            // This is static because I'm returning a new created Fraction.
        }

        public override ToString() {
            return numer.ToString() + "/" + denom.ToString();
        }
    }
}

Testing this, it works decently, serializing into an XML fragment of the form:

<Fraction>
   <Num>(INT64 VALUE AS STRING)</Num>
   <Den>(INT64 VALUE AS STRING)</Den>
</Fraction>

Now, I could just be happy with this and go on my merry coding way. But I’m picky.

My end users will probably not be super familiar with XML, and there’s a LOT of more complex datatypes in my game that will include a lot of Fractions, so I’d much rather prefer to be able to represent a Fraction in the XML simfile as such (much more concisely):

<Fraction>(NUMERATOR)/(DENOMINATOR)</Fraction>

However, I’m at a loss as to how to do this without breaking automatic (de)serialization. I looked into the IXmlSerializable interface, but I was shut down by the fact that my datatype needed to be mutable in order for it to work (ReadXml() doesn’t return a new Fraction object, but instead seems to flash-instantiate one, and you have to fill in the values manually, which doesn’t work due to the readonly). Using the OnSerializing and OnDeserialized attributes didn’t work either for the same reason. I’d REALLY prefer to keep my Fraction class immutable.

I’m guessing there’s a standard procedure by which primitives are converted to/from strings when serializing to XML. Any numeric primitives, for instance, would have to be converted to/from strings upon serializing/deserializing. Is there any way for me to be able to add this sort of automatic string from/to conversion to my Fraction type? If it were possible, I’d imagine the serializing procedure would look something like this:

1) Serialize this complex datatype which contains Fraction fields.
2) Start serializing the [DataMember] fields.
3) Hey, this field is a Fraction object. Fractions are able to represent themselves fully as a string. Write that string out to the XML directly, instead of diving into the Fraction object and writing out all its fields.
…

Deserialization would work the opposite way:

1) Deserialize this data, we’re expecting so-and-so data type.
2) Start deserializing fields.
3) Oh look, a Fraction object, I can just go ahead and read the content string, convert that string into a new Fraction object, and return the newly-created Fraction.
…

Is there anything I can do to accomplish this? Thanks!

EDIT: Data Contract Surrogates seem like the way to go, but for the life of me I can’t seem to understand them or have them work in my game. Or rather, they add some nasty automatic namespace and ID fields to my serialized elements.

  • 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-05-17T22:11:14+00:00Added an answer on May 17, 2026 at 10:11 pm

    I guess that you can probably use Data Contract Surrogates.

    But even simpler way would be to have a private string member fractionString within your type that will represent the string representation of your Type. You have to initialize it only during object construction (as your type is immutable). Now you can skip num and den from serialization and mark your fractionString field as DataMember. Downside to the approach is additional space consumption.

    public sealed class Fraction {
    
        private readonly long numer;
        private readonly long denom;
    
        [DataMember(Name="Fraction")
        private string fractionString;
    

    EDIT: Never mind, just re-read what you want and realized above won’t work.

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

Sidebar

Related Questions

So in my new music app game I'm creating, I'm allowing the user to
I have created a music application in which I have 5 songs added to
I personally loathe background music on a website. My client has opposite feelings on
I'm going to build a Classic Music website and the client already has several
I have a table of music albums. I have a second table of album
I need to serve audio content (not music though, more like podcasts; human speech),
I was making a website for a music band, and i was wondering the
To create a playlist for all of the music in a folder, I am
I am working on an application for college music majors. A feature i am
I've been thinking a lot lately about a music-oriented project I'd like to work

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.