0

I have a C# and java code that serializes an object into a JSON string to send between clients and the server. In C# I use Jil to serialize and deserialize objects. In java I use gson. Everything works fine until I work with C# DateTime object and Java Timestamp object. In C# DateTime serialization return Unix format like this:

{"timesent":"\/Date(1645856979963)\/"}

minimal reproducible example code:

        class MyObject
        {
            public DateTime date;
            public MyObject(DateTime date)
            {
                this.date = date;
            }
        }
        static void Main(string[] args)
        {
            DateTime now = DateTime.Now;
            MyObject myObject = new MyObject(now);
            String json = Jil.JSON.Serialize<MyObject>(myObject);
            Console.WriteLine(json);
            //{"date":"\/Date(1648191236905)\/"}
        }

meanwhile in Java Timestamp return format like this:

{"timesent":"Feb 20, 2022, 1:43:23 AM"}

PS: They are 2 different timestamps, I just want to say that the formats are different and cannot be deserialized by the other language

I tried converting Java Timestamp to Unix format with .getTime() and making it return a string like

"\\/Date(" + sql.getTimestamp("timesent").getTime() + ")\\/"

but it doesn't work because Jil throws an error:

Jil.DeserializationException: Expected character: '/'

but I can't just give it one \ because it is an escape character, so I have to give 2, but it doesn't take 2 and requires a / after a \

So how can I make Java gson serialization serializes Timestamp in Unix format so that the C# Jil can read it?

Edited: added reproducible code, I keep using Jil because it currently works for the app in C# and now I am just rewriting the app in Java but the client side is still in C#, I will try the suggestions!

Mancitiss
  • 15
  • 2
  • 6
  • _"In C# DateTime serialization return Unix format like this"_ - I haven't seen that as the default for any JSON serializer I've used. Which one are you using? – ProgrammingLlama Mar 25 '22 at 06:36
  • 2
    "In C# DateTime serialization return Unix format like this" - I think you mean that's the default of Jil. It's not the default of any of the serialization libraries I've used. Please provide a [mcve] so that it's easier to help you. (You might also want to consider whether you *have* to stick with Jil. The fact that the last stable release was in three years ago isn't encouraging.) – Jon Skeet Mar 25 '22 at 06:36
  • 2
    Mandatory https://xkcd.com/1179/. You really should use standard date time format (ISO 8601), especially if you are looking for interoperability between languages/platforms. Even if you have to convert DateTime to string yourself you save yourself a lot of headache. (Clearly this note is not an answer and not even suitable for comment as it is not asking for clarification. Looks like it is unkind trolling and should be flagged as such) – Alexei Levenkov Mar 25 '22 at 06:44
  • Yup, I'd suggest changing both the Java *and* the C# code to ISO-8601 - your first timestamp would be "2022-03-25T06:45:23Z" for example. – Jon Skeet Mar 25 '22 at 06:46
  • looking at its source code, Jil seems can be told to use iso-8601 using `Jil.JSON.Serialize(myObject, Jil.Options.ISO8601)`. also, though technically json doesnt specify the date time format - back before .net 4.5 even NewtonsoftJSON library also outputs unix time. nowadays, we all agree to use that particular iso standard. – Bagus Tesa Mar 25 '22 at 07:23

1 Answers1

1

Jil do support another DateTime format, namely the ISO-8601. Unfortunately, the developer specifically said they wont add more date formats. It can be used roughly the following way:

Jil.JSON.Serialize<MyObject>(myObject, Jil.Options.ISO8601)

However, if you are wiling to switch to Newtonsoft.Json, you can keep your current date format (Feb 20, 2022, 1:43:23 AM) using JsonConverter attribute to tell the serializer to spew the date time in any format you want.

First step to do that after installing Newtonsoft.Json, you should implement your own converter:

public class MyDateTimeConverter : IsoDateTimeConverter
{
   public MyDateTimeConverter()
   {
      base.DateTimeFormat = "MMM dd, yyyy, H:m:s tt";
   }
}

The next step is to apply the JsonConvert attribute to the your class:

class MyObject
{
  [JsonConvert(typeof(MyDateTimeConverter))]
  public DateTime date;
  public MyObject(DateTime date)
  {
    this.date = date;
  }
}
Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42