[Webinar] Streamline your web hosting managementRegister Today


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

Posted on 2010-04-08
Medium Priority
Last Modified: 2013-11-08
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.

Question by:mrjoltcola
  • 2
  • 2
LVL 10

Accepted Solution

_Katka_ earned 2000 total points
ID: 30191677
Hi, did you try to use and implement ISerializable ?

LVL 40

Author Comment

ID: 30203816
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.
LVL 10

Assisted Solution

_Katka_ earned 2000 total points
ID: 30205001
Here is the example how to do it for a dictionary:


LVL 83

Expert Comment

ID: 33150297
@mrjoltcola did you resolve your problem?
LVL 40

Author Closing Comment

ID: 35917910
Sorry about that, I forgot to get back to this question.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question