0

I'm trying to convert a yaml file to a json file with a C# application. My application crashed on the following row "created: !ruby/object:ActiveSupport::TimeWithZone"

How can I convert the date, because I need this part: "2016-10-14 04:56:51.057147000 Z"

The yaml:

- id: 1
  created: !ruby/object:ActiveSupport::TimeWithZone
    utc: &4 2016-10-14 04:56:51.057147000 Z
    zone: *2
    time: *4

Code:

using System.IO;
using System;
using YamlDotNet.Serialization;     

public class Program
{
    public static void Main()
    {
        var r = new StringReader(@"
- id: 1
  created: !ruby/object:ActiveSupport::TimeWithZone
    utc: &4 2016-10-14 04:56:51.057147000 Z
    zone: *2
    time: *4
"); 
        var deserializer = new Deserializer();
        var yamlObject = deserializer.Deserialize(r);

        var serializer = new Serializer(SerializationOptions.JsonCompatible);
        serializer.Serialize(Console.Out, yamlObject);
    }
}
Barry The Wizard
  • 1,258
  • 2
  • 12
  • 25
  • To serialize a Date, you need a *date* to begin with. The contents of the `utc` property aren't a date. This is a string that kind-of-looks-like-a-date but has a weird `&4 ` prefix, and a suffix ` Z` that could be a timezone with an extra whitespace, could be something else. This can only be treated as a string – Panagiotis Kanavos Feb 21 '17 at 10:29
  • You have to write a representer for this ruby tag: `!ruby/object:ActiveSupport::TimeWithZone` so you get an object that extracts the the date from the value for `utc`. For that you don't need the time part. Alternatively bind the ruby intepreter into your C# program and have ruby load the yaml, then pick up the date value. – Anthon Feb 21 '17 at 20:27

0 Answers0