Control JSON Serialization format of a custom type in .NET to produce a primitive string

Ok, EE .NET gurus, here is a Stackoverflow challenge.

I have a PhoneNumber class that stores a normalized string, and I've defined implicit operators for converting string to/from Phone to simplify treatment of the PhoneNumber as a string. I've also overridden the ToString() method to always return the cleaned version of the number (no hyphens or parentheses or spaces). In any views where I display the number, I explicitly call phone.Format(). The point is, I need to treat PhoneNumber as a string everwhere, but give me the ability for some validation and formatting as needed.

The problem here is serializing a PhoneNumber to JSON; JavaScriptSerializer serializes it as [object Object].

I want to serialize it as a primitive string (for example +1-555-555-5555 format), but the formatting is not the problem, it's getting the JavaScriptSerializer to do what I want.


NOTE: Writing a custom JavaScriptConverter, at first, seems like a solution, but JavaScriptConverter.Serialize() returns a dictionary of name-value pairs, so it is only useful for serializing an aggregate. So I'd have to write a serializer for all the aggregate classes. That seems ridiculous. Phonenumber is a primitive in my system. I do not want PhoneNumber to be treated as an object with fields, I want to simply serialize it as a string.

I would have thought there was an Object Oriented, simple  C# solution to this. In C++, I'd inherit from the string type. But in C# I cannot derive from a sealed type (string) so I tried overriding ToString() and defining a string cast operator but it has not solved it.

-mjc
LVL 40
mrjoltcolaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_Katka_Commented:
Hi, did you try to use and implement ISerializable ?

regards,
Kate
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrjoltcolaAuthor Commented:
I have not. I had overlooked that, thinking it was for the other .NET standard serialization methods, not JSON. But now reading again, it looks promising. I'll go back through the docs on ISerializable and try it. Give me a couple of days to try it, I'm out of town this weekend.

Thanks for the suggestion.
0
_Katka_Commented:
Here is the example how to do it for a dictionary:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/8bef40bc-8466-4c6f-a717-15f3d6e61e3c

regards,
Kate
0
leakim971PluritechnicianCommented:
@mrjoltcola did you resolve your problem?
0
mrjoltcolaAuthor Commented:
Sorry about that, I forgot to get back to this question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.