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 887547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:14:49+00:00 2026-05-15T13:14:49+00:00

consider the following class and struct public class Entity { public IdType Id {get;set;}

  • 0

consider the following class and struct

public class Entity {
    public IdType Id {get;set;}
    public string Data {get;set;}
}

[TypeConverter(IdTypeConverter))]
public struct IdType {
    ... any data ...
}

The IdTypeConverter can convert the IdType struct from and to string.

Now, what I want is this class to be serializable for AJAX, WCF and ViewState. The senario could be a WCF web service which provides as array of Entity[] to a DataSource. And a custom control which binds to the datasource, saves this class to it’s ViewState and sends the data to client side code.

This could be achieved easily by simply adding a [Serializable] attribute to all serialized types. But I don’t want the IdType to be serialized, but to be converted to a string. Thus the JSON representation should be

{ 'Id'=>'StringRepresentationOfId', 'Data'=>'foo' }

This would analogously be the optimal serialization for WCF and ViewState.

Another solution would be to write another class

public class JsonEntity {
    public JsonEntity(Entity from) {
        Id = from.Id;
        Data = from.Data;
    }
    public string Id {get;set;}
    public string Data {get;set;}
}

and use this for JsonSerialization. But I don’t like this, because this would imply that the control which is sending the data to the client knows about the Entity type.

The actual question is: Is it possible to customize JsonSerialization with attributes without breaking WCF and ViewState serialization?

EDIT:
An answer like “That’ impossible” would satify me, since I’d stop trying.

  • 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-15T13:14:49+00:00Added an answer on May 15, 2026 at 1:14 pm

    From what I’ve read, the LosFormatter class used for ViewState serialization checks if there is a TypeConverter for a given object and uses it, so you should be covered there.

    This article describes how to create your own JavaScriptConverter to perform custom serialization. The Serialize method that you have to implement returns IDictionary<string, object>, so you might have to create a JavaScriptConverter class for your Entity class rather than for your IdType struct. Below is an example, though I haven’t had a chance to test it out:

    Note: the examples in my post just look up the associated TypeConverter for IdType in order to convert it to a string, but unless there’s a specific reason to do it that way you might as well just call a specific function (e.g. override ToString) that returns a the string representation directly instead of looking up the TypeConverter as my example code is doing.

    public class EntityJsonConverter : System.Web.Script.Serialization.JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }
    
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            if (obj == null)
                throw new ArgumentNullException("obj");
    
            Entity entity = obj as Entity;
            if (entity != null)
            {
                var values = new Dictionary<string, object>();
                TypeConverter idTypeConverter = TypeDescriptor.GetConverter(entity.Id);
                if (idTypeConverter != null)
                {
                    if (idTypeConverter.CanConvertTo(typeof(string)))
                        values.Add("Id", idTypeConverter.ConvertTo(entity.Id, typeof(string)));
                    else
                        throw new SerializationException(string.Format("The TypeConverter for type \"{0}\" cannot convert to string.", this.GetType().FullName));
                }
                else
                    throw new SerializationException(string.Format("Unable to find a TypeConverter for type \"{0}\".", this.GetType().FullName));
    
                values.Add("Data", serializer.Serialize(entity.Data));
    
                return values;
            }
            else
                throw new ArgumentException(string.Format("Expected argument of type \"{0}\", but received \"{1}\".", typeof(Entity).FullName, obj.GetType().FullName), "obj");
        }
    
        public override IEnumerable<Type> SupportedTypes
        {
            get { yield return typeof(Entity); }
        }
    }
    

    If you also want the behavior that you described for XML serialization, you could have the IdType struct implement the IXmlSerializable interface. Below is an example of a WriteXml method implementation that uses the TypeConverter defined for the IdType struct:

        void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(this);
            if (converter != null)
            {
                if (converter.CanConvertTo(typeof(string)))
                    writer.WriteString(converter.ConvertTo(this, typeof(string)) as string);
                else
                    throw new SerializationException(string.Format("The TypeConverter for type \"{0}\" cannot convert to string.", this.GetType().FullName));
            }
            else
                throw new SerializationException(string.Format("Unable to find a TypeConverter for type \"{0}\".", this.GetType().FullName));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 439k
  • Answers 439k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer in your view folders you can place your BackOffice and… May 15, 2026 at 4:54 pm
  • Editorial Team
    Editorial Team added an answer String literals need to be in single-quotes: ...WHERE email =… May 15, 2026 at 4:54 pm
  • Editorial Team
    Editorial Team added an answer I think... $('#OIPHPFrame')[0].find('#email_box').val(); $('#OIPHPFrame')[0].find('#butImport').click(); ...should work. May 15, 2026 at 4:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.