2

I need to convert a json to a native .net object using mongodb. The application is written in javascript/mvc.

One of the field is a datetime object and the toJson function in the mongodb driver formats this as: "Modified":{"$date":1319630804846}

I want to parse this json from the client using the same format, but can't find a function that does this.

In Newtonsoft.Json I used this code, but this fails because of the date field:

var jobject = JObject.parse(jsonAsString)
var myObject = jobject.ToObject<myObject>();

But with the mongoDb driver, all I can do is converting the string to a BsonDocument

var buffer = new JsonBuffer(json);
using (BsonReader reader = new JsonReader(buffer))
{
    var doc = BsonDocument.ReadFrom(reader);    

    .... 
}
1
  • Solved: I found out that in a newer driver it's possible to do BsonSerializer.Deserialize<myObject>(json) Commented Oct 26, 2011 at 20:24

3 Answers 3

1

The BSON serialization format for DateTime is an Int64 containing the number of milliseconds since Unix Epoch. So if you were to create a DateTime of kind Utc set to jan 1 1970 and then create a TimeSpan with TotalMilliseconds set to the Int64, and add the two together you'd have the date in Utc. The same algorithm could be used in reverse as needed.

Sign up to request clarification or add additional context in comments.

Comments

1

If you're using the official .NET driver, you can work with objects without going through the JSON serialization.

Check the following example of how easy this is:

class Child
{
    public ObjectId id;
    public string name;
    public DateTime birthday;
}

class Program
{
    static void Main(string[] args)
    {
        Child m = new Child();
        m.name = "Micaiah";
        m.birthday = DateTime.Parse("January 1, 2011");

        Children.Insert<Child>(m);

        foreach (Child kiddo in Children.FindAllAs<Child>())
        {
            Console.WriteLine("Kiddo: {0} {1}", kiddo.name, kiddo.birthday);
        }

        Console.ReadLine();
    }

    static MongoCollection Children
    {
        get
        {
            MongoServer s = MongoServer.Create("mongodb://localhost");
            return s["demos"]["children"];
        }
    }
}

Here's the record as stored in MongoDB:

> db.children.findOne()
{
    "_id" : ObjectId("4ea821b2dd316c1e70e34d08"),
    "name" : "Micaiah",
    "birthday" : ISODate("2011-01-01T06:00:00Z")
}
>

1 Comment

Thank you, but the format has to be Json, since the application is a mvc application written in C# and javascript
0

Use JSON.Net to de-serialize your Json into a JObject, and send that to MongoDB... if you have more concrete types in C#, you'll want to serialize/deserialize to/from that to JSON... then persist from your concrete object, or JObject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.