0

SerializeDateTime method returns datetime in milliseconds. I want him to turn to microseconds. SerializeDateTime method as follows:

private static void SerializeDateTime(DateTime datetime, StringBuilder sb, JavaScriptSerializer.SerializationFormat serializationFormat)
{
  if (serializationFormat == JavaScriptSerializer.SerializationFormat.JSON)
  {
    sb.Append("\"\\/Date(");
    sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
    sb.Append(")\\/\"");
  }
  else
  {
    sb.Append("new Date(");
    sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks) / 10000L);
    sb.Append(")");
  }
}

I want to override this method as follows.

private static void SerializeDateTime(DateTime datetime, StringBuilder sb, JavaScriptSerializer.SerializationFormat serializationFormat)
{
  if (serializationFormat == JavaScriptSerializer.SerializationFormat.JSON)
  {
    sb.Append("\"\\/Date(");
    sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks));
    sb.Append(")\\/\"");
  }
  else
  {
    sb.Append("new Date(");
    sb.Append((datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks));
    sb.Append(")");
  }
}

I do not know how to do it. How else can we come up with a solution? Newtonsoft serializer cannot serialize my model when I want to try. If it worked, maybe I could set it up with dateformat.

2 Answers 2

0

The method in question is not marked as virtual. It cannot be overridden.

Why not just create your own method in a static class?

public static CustomSerializer
{
    public static void SerializeDateTime(...) {
         //your implementation
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You could create a custom JavaScriptConverter and use that. The only downside is that the date will be wrapped.

public class JavaScriptDateTimeMicrosecondConverter : JavaScriptConverter
{
    private const string FieldName = "DateTimeInMicroSeconds";

    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        if (dictionary == null) throw new ArgumentNullException(nameof(dictionary));

        if (serializer == null) throw new ArgumentNullException(nameof(serializer));

        if (dictionary.TryGetValue(FieldName, out var value))
        {
            return DateTime.Parse(value.ToString());
        }

        return null;
    }

    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        DateTime datetime = (DateTime)obj;
        var result = new Dictionary<string, object>
        {
            { FieldName, (datetime.ToUniversalTime().Ticks - JavaScriptSerializer.DatetimeMinTimeTicks).ToString() }
        };

        return result;
    }

    public override IEnumerable<Type> SupportedTypes => new List<Type> { typeof(DateTime) };
}

// ...
// Registration
var jsSerializer = new JavascriptSerializer();
jsSerializer.RegisterConverters(new JavaScriptConverter[] { new JavaScriptDateTimeMicrosecondConverter() });

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.