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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:23:20+00:00 2026-05-23T08:23:20+00:00

I have a class which I am using to serialize and deserialize business objects

  • 0

I have a class which I am using to serialize and deserialize business objects in an ASP.NET application.

The class contains this static variable:

private static DataContractJsonSerializer m_serializer = new DataContractJsonSerializer(typeof(MyBusinessObject));

… and two static methods in the class:

public static string SerializeJson(MyBusinessObject bo);

public static MyBusinessObject DeserializeJson(string json);

In these methods, I use the static DataContractJsonSerializer object (m_serializer) to perform serialization and deserialization. This way, I do not need to instantiate a DataContractJsonSerializer instance for each call.

Is it correct design to use a static variable in this way?

Could I run into any problems if I get lots of hits on the serialize/deserialize code?

It will be called from website which gets 100s of concurrent hits every second.

  • 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-23T08:23:21+00:00Added an answer on May 23, 2026 at 8:23 am

    As Alex mentioned, you can run into problems – the documentation of the class states that it’s not thread safe (at least not the methods used to read / write objects). By doing a quick look on reflector it seems like the serialization can actually work (assuming you’re not serializing the same object at the same time in different threads, that’s a whole new can of worms), but since it’s an internal implementation detail, it’s possible that in a new version of the framework the class has some change (i.e., optimization) and that won’t be true anymore.

    An option which you can consider is to have a pool of serializers which can be reused. Your code would take one serializer from the pool, use it to serialize / deserialize, and then return it to the pool. This way you get the benefit of reusing the instances, but without the risk of using a thread-unsafe class in multiple threads.

    Finally, have you verified that the creation of the serializers is indeed a problem (i.e., by profiling)? It’s possible that the time it takes is negligible in your scenario, and using some sort of reuse would just add unnecessary complication to your code.

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

Sidebar

Related Questions

I have a parent class which contains a child object. I am using set
I have a DAO class which I am using to do database operations. I
I have a DAO class which I'm using to try select/update/insert with hibernate and
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,
I have a class which I want to add a property with using formula
I have a python class which reads a config file using ConfigParser: Config file:
I am using C#. I have created a class which can be included in
I am using the apache library. I have created a class which sends a
I have gotten everything working for my class in which I am using Tomcat
I have a Linq-To-Sql based repository class which I have been successfully using. I

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.