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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:20:45+00:00 2026-05-28T20:20:45+00:00

I have an object model that looks like this: public MyObjectInJson { public long

  • 0

I have an object model that looks like this:

public MyObjectInJson
{
   public long ObjectID {get;set;}
   public string ObjectInJson {get;set;}
}

The property ObjectInJson is an already serialized version an object that contains nested lists. For the moment, I’m serializing the list of MyObjectInJson manually like this:

StringBuilder TheListBuilder = new StringBuilder();

TheListBuilder.Append("[");
int TheCounter = 0;

foreach (MyObjectInJson TheObject in TheList)
{
  TheCounter++;
  TheListBuilder.Append(TheObject.ObjectInJson);

  if (TheCounter != TheList.Count())
  {
    TheListBuilder.Append(",");
  }
}
TheListBuilder.Append("]");

return TheListBuilder.ToString();

I wonder if I can replace this sort of dangerous code with JavascriptSerializer and get the same results.
How would I do this?

  • 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-28T20:20:46+00:00Added an answer on May 28, 2026 at 8:20 pm

    If using .Net 6.0 or later;

    Default to using the built in System.Text.Json parser implementation with Source Generation. Its a little more typing and compiling but, is more efficient at runtime.

    The "easier to code" option below, still works but, less efficiently because it uses Reflection at runtime. The new method has some intentional, by design limitations. If you can’t change your model to avoid those, the Reflection based method remains available.

    e.g.

    using System.Text.Json;
    using System.Text.Json.Serialization;
    
    var aList = new List<MyObjectInJson>
    {
        new(1, "1"),
        new(2, "2")
    };
    
    var json = JsonSerializer.Serialize(aList, Context.Default.ListMyObjectInJson);
    Console.WriteLine(json);
    
    return;
    
    public record MyObjectInJson
    (
        long ObjectId,
        string ObjectInJson
    );
    
    [JsonSerializable(typeof(List<MyObjectInJson>))]
    internal partial class Context : JsonSerializerContext
    {
    }
    

    If using .Net Core 3.0 to .Net 5.0, it is time to upgrade;

    Default to using the built in System.Text.Json parser implementation.

    e.g.

    using System.Text.Json;
    
    var json = JsonSerializer.Serialize(aList);
    

    If stuck using .Net Core 2.2 or earlier;

    Default to using Newtonsoft JSON.Net as your first choice JSON Parser.

    e.g.

    using Newtonsoft.Json;
        
    var json = JsonConvert.SerializeObject(aList);
    

    you may need to install the package first.

    PM> Install-Package Newtonsoft.Json
    

    For more details see and upvote the answer that is the source of this information.

    For reference only, this was the original answer, many years ago;

    // you need to reference System.Web.Extensions
    
    using System.Web.Script.Serialization;
    
    var jsonSerialiser = new JavaScriptSerializer();
    var json = jsonSerialiser.Serialize(aList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that looks like this: class Model { public string Category
I have an object model that looks like this: public class MyObjectModel { public
I have a controller method that looks like this: [AcceptGet] public ActionResult Index(SecurityMatrixIndexViewModel model)
I have an action that looks like this: [Post] [PopulateModelFromId] public ActionResult ChangeName( string
I have an object model that looks like this (pseudo code): class Product {
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have a model that looks something like this: public class SampleModel { public
I have an object that looks like this: [Bindable] public class MyRecord implements ValueObject
I have a simple question. I have a model that looks like this: public
I have a constructor signature that looks like this. public LocateEditorViewModel( ILocateRepository locateRepository, int

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.