4

I'm trying to deserilize an object the following code, and I'm wondering what the proper regex would be to replace json dates. When I run the following code, the regex never gets triggered. I'm using the standard JSON date format inside the json string.

{
    "UniqueId": "1000000003",     
    "Id": 3, 
    "ModifyTimestamp": "/Date(1338857699743)/"         
}

string json = // see above
string p = @"\\/Date\((\d+)\+\d+\)\\/";
MatchEvaluator matchEvaluator = new MatchEvaluator(convertJsonDateToDateString);
Regex reg = new Regex(p);
json = reg.Replace(json, matchEvaluator);
JavaScriptSerializer serializer = new JavaScriptSerializer();            
Student student = serializer.Deserialize<Student>(json) as Student; 



public static string convertJsonDateToDateString(Match m) {
        string result = string.Empty;
        DateTime dt = new DateTime(1970, 1, 1);
        dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
        dt = dt.ToLocalTime();
        result = dt.ToString("yyyy-MM-dd HH:mm:ss");
        return result;
    }

2 Answers 2

5

Here is a fully working solution:

using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace ResolveOverlapIssue
{
    public partial class Form1 : Form
    {
        private static void DoSomethingElse<T>(ref T Id)
        {
            int i = 00;
        }

        public Form1()
        {
            InitializeComponent();

            string json = "{" +
                          "UniqueId: 1000000003," +
                          "Id: 3," +
                          "ModifyTimestamp: /Date(1338857699743)/" +
                          "}";

            MatchEvaluator matchEvaluator = ConvertJsonDateToDateString;
            var reg = new Regex(@".Date\(\d+\)\/.");
            json = reg.Replace(json, matchEvaluator);

        }

        public static string ConvertJsonDateToDateString(Match m)
        {
            var dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            var result = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return result;
        }
    }
}
Sign up to request clarification or add additional context in comments.

18 Comments

Thanks for the answer... I actually have about 5 or 6 dates in the object, so I won't know what the actual date is. What could I put inside the <ticks> to have it pick up the actual number as a Regex value as well?
Once you decide that everything is working OK, feel free to upvote the hell out of me, I need the points ;)
Can you paste a working (sample) on pastebin or somewhere else so that I can see exactly what you are doing and try to debug it? It's 04:04 here in the UK so I'm going to sleep soon... But I can hang around and help you out first...
@AdamLevitt hahahaha, StackOverflow appears to be changing what I write... I checked pastebin and it's right on there: pastebin.com/jtTaJmSq Don't forget to upvote me... It's worth it, like pantenne...
@AdamLevitt right. Sorry to mess you about, or to have messed you about... Here is a fully working solution. I can see it firing the correct method when it should and not firing when it shouldn't. pastebin.com/fhjSx6gD
|
0

@"\\/Date portion look wrong. It probably should be either @"\/Date..." or "\\/Data...". (same for trailing "...\\/")

2 Comments

I'm not sure I follow... isn't that what I already have? -- string p = @"\\/Date((\d+)\+\d+)\\/";
No.Vijay have it correct in his answer - no double escaping of ` as you get in case of using @` in front of string: @"\\" produces 2 back slashes, "\\" produces just one.

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.