views:

421

answers:

2

Hi guys,

I have a C# application that serializes its DTOs to JSON and sends them accros the wire to be processed by Ruby. Now the format of the serialized date is like so:

/Date(1250170550493+0100)/

When this hits the Ruby app I need to cast this string representation back to a date/datetime/time (whatever it is in Ruby). Any ideas how I would go about this?

Cheers, Chris.

A: 

You could parse out the seconds since the epoch, something like:

def parse_date(datestring)
  seconds_since_epoch = datestring.scan(/[0-9]+/)[0].to_i
  return Time.at(seconds_since_epoch)
end

parse_date('/Date(1250170550493+0100)/')

You'd still need to handle the timezone info (the +0100 part), so this is a starting point.

orip
+2  A: 

You could use Json.NET to serialize your DTOs instead of the built in .NET JSON serializer. It gives you flexibility over how to serializing dates (i.e. as a constructor, ISO format, etc).

James Newton-King
+1, great option if your requirements allow you to change the date serialization format to something Ruby likes better
orip
BTW - we use Json.NET all the time, great library. Thanks James!
orip