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

  • SEARCH
  • Home
  • 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 6888843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:04:45+00:00 2026-05-27T06:04:45+00:00

How do I format object ids and dates to show up correctly in the

  • 0

How do I format object ids and dates to show up correctly in the json serialized output using .net?

return Json(result, JsonRequestBehavior.AllowGet);

and here is the output that I get

{
  "_id": {
      o "Timestamp": 1321487136,
      o "Machine": 5156,
      o "Pid": -4604,
      o "Increment": 78,
      o "CreationTime": "/Date(1321487136000)/"
  },
"start": "/Date(1321487094000)/",
"end": "/Date(1638039600000)/",

}

I’d like the json to look like this

{
    "_id":"4e483da1e517801b09000004",
    "end":"2012-12-30T05:00:00.000Z",
    "start":"2011-08-14T17:26:57.000Z"
}

Reading through the advice below, got it working with the following

public class MongoSimpleIdConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(MongoDB.Bson.ObjectId);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return new MongoDB.Bson.ObjectId((string)existingValue);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(((MongoDB.Bson.ObjectId)value).ToString());
        }
    }


 [HttpGet]
        public ContentResult Index()
        {

            var result = JsonConvert.SerializeObject(svc.GetTasks(), new MongoSimpleIdConverter(), new IsoDateTimeConverter());

            return new ContentResult { Content = result, ContentType = "application/json" };
        }
  • 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-27T06:04:45+00:00Added an answer on May 27, 2026 at 6:04 am

    Huh, interesting, I was sure it was going to be a breeze, but it doesn’t seem like there’s a simple answer like “Write a converter and plug it with an attribute”.

    I would recommend using a 3rd party like Json.net (which supports custom converters) and staying away from the built-in method.

    I once wrote this code to handle the same problem with id serialization in the unofficial mongo driver –

    public class OidConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(MongoDB.Oid);
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return new MongoDB.Oid((string)existingValue);
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteValue(((MongoDB.Oid)value).ToString());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started to use Json.NET to convert a string in JSON format to object
What is the JSON (JavaScript Object Notation) format?
Is it possible to format how an object is returned as JSON from a
Basicaly I want to format a Date object using a specific pattern and the
What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If
I have a traceback object that I want to show in the nice format
I have been using json.NET successfully in projects for some time now without any
I'm using JSON.js provided in JSON.org <% JSONReturn = GetDistance(Sample) // return a string
I want to format a date object so that I can display strings such
I have a String Object in format yyyyMMdd .Is there a simple way to

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.