runtime icon indicating copy to clipboard operation
runtime copied to clipboard

JSON custom DateTime format \/Date(1605712704841)\/

Open martindrlik opened this issue 5 years ago • 13 comments

Hello guys,

for compatibility I'm trying to serialize DateTime to custom date time format: \/Date(1605712704841)\/. I am not sure what is the origin of escaping forward slash and the name of this format.

First I thought that this would be possible to do by implementing own JsonConverter<>:

public class JsonConverterBackslashDateTime : JsonConverter<DateTime>
{
    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        var unixTimeMilliseconds = ((DateTimeOffset)value).ToUnixTimeMilliseconds();
        writer.WriteStringValue(@$"\/Date({unixTimeMilliseconds})\/");
    }
}

This will produce string as following "\\/Date(1619690852000)\\/". Note that escaped back slash. It seems that Utf8JsonWriter.WriteStringValue recognize \/ as back slash that needs to be escaped and forward slash.

I am wondering if there are some options how to serialize to \/Date(milliseconds)\/ date time format and still be able to use System.Text.Json.JsonSerializer. Thank you.

martindrlik avatar May 03 '21 12:05 martindrlik

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar May 03 '21 12:05 ghost

Tagging subscribers to this area: @eiriktsarpalis, @layomia See info in area-owners.md if you want to be subscribed.

Issue Details

Hello guys,

for compatibility I'm trying to serialize DateTime to custom date time format: \/Date(1605712704841)\/. I am not sure what is the origin of escaping forward slash and the name of this format.

First I thought that this would be possible to do by implementing own JsonConverter<>:

public class JsonConverterBackslashDateTime : JsonConverter<DateTime>
{
    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        var unixTimeMilliseconds = ((DateTimeOffset)value).ToUnixTimeMilliseconds();
        writer.WriteStringValue(@$"\/Date({unixTimeMilliseconds})\/");
    }
}

This will produce string as following "\\/Date(1619690852000)\\/". Note that escaped back slash. It seems that Utf8JsonWriter.WriteStringValue recognize \/ as back slash that needs to be escaped and forward slash.

I am wondering if there are some options how to serialize to \/Date(milliseconds)\/ date time format and still be able to use System.Text.Json.JsonSerializer. Thank you.

Author: martindrlik
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

ghost avatar May 03 '21 18:05 ghost

I don't think that forward slashes can be escaped, which I suppose is why you had add the literal @ prefix to your string. This results in the backslash being treated as a separate character and is therefore (correctly) escaped in the serialization. I'm curious though what character you would expect an escaped forward slash to map.

eiriktsarpalis avatar May 04 '21 11:05 eiriktsarpalis

@eiriktsarpalis thank you for your answer. I do not think that forward slash needs to be escaped. It is only required for compatibility with following date time JSON format described here. image

Also note this question on stackoverflow where there is some explanation of why backslash is used.

martindrlik avatar May 04 '21 12:05 martindrlik

Writing unescaped backslash characters does not seem to be possible under any configuration of the JavaScriptEncoder. Given that this is an old Microsoft format that used to be the default in JSON.net, it stands to reason that certain users might need to support this format. Tagging @GrabYourPitchforks and @layomia who might know of any workarounds that could be used here.

eiriktsarpalis avatar May 04 '21 14:05 eiriktsarpalis

Seems like the easiest mechanism would be to add APIs which would allow you to add pre-escaped text:

public readonly struct JsonEncodedText
{
    public static JsonEncodedText Raw(string escapedValue);
}

// ...

writer.WriteStringValue(JsonEscapedText.Raw($"/Date({unixTimeMilliseconds})/"));

GrabYourPitchforks avatar May 05 '21 00:05 GrabYourPitchforks

@GrabYourPitchforks thank you for your answer. If I get it correctly there could be new WriteStringValue method overload that takes JsonEncodedText and simply writes what is in there without escaping?

writer.WriteStringValue(JsonEscapedText.Raw(@$"\/Date({unixTimeMilliseconds})\/"));

would result to "\/Date(1619690852000)\/" (note that double quoted as it is still JSON string value). That would be nice.

It might be also possible to add WriteRaw method to Utf8JsonWriter. Perhaps as an extension method, as it might not be needed in most cases.

public static void WriteRaw(this Utf8JsonWriter writer, ...)
{
    // writes without escaping
}

And usage would be as following

// import that extension
// ...
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
    var unixTimeMilliseconds = ((DateTimeOffset)value).ToUnixTimeMilliseconds();
    writer.WriteRaw(@$"""\/Date({unixTimeMilliseconds})\/""");
}

Note that WriteRaw adds no double quotes so to write JSON string value quotes should be added.

martindrlik avatar May 05 '21 12:05 martindrlik

@martindrlik out of curiosity, did you try to implement the Read part of the converter for this format?

eiriktsarpalis avatar May 05 '21 12:05 eiriktsarpalis

@eiriktsarpalis not yet. Currently there is no need for us as it is only used to serialize response. Consumer of this response has already adapted JSON deserializer to accept \/Date(millis)\/ format.

martindrlik avatar May 05 '21 12:05 martindrlik

@eiriktsarpalis not yet. Currently there is no need for us as it is only used to serialize response. Consumer of this response has already adapted JSON deserializer to accept \/Date(millis)\/ format.

Uh, this implies that there's active development happening on both sides here. At that point the question becomes "Can you agree on changing the date format?". Possibly gated by an API version.

Clockwork-Muse avatar May 05 '21 15:05 Clockwork-Muse

@eiriktsarpalis not yet. Currently there is no need for us as it is only used to serialize response. Consumer of this response has already adapted JSON deserializer to accept \/Date(millis)\/ format.

Uh, this implies that there's active development happening on both sides here. At that point the question becomes "Can you agree on changing the date format?". Possibly gated by an API version.

It is deserialized by our customers. We can use new date format for new API and when announced as breaking change it might be used for existing API, but for convenience it would be nice to be able to use \/Date(millis)\/ with this JsonSerializer. But I agree that need for \/ should be dropped someday and if we don't find a reasonable solution we will change to better date time format.

martindrlik avatar May 05 '21 16:05 martindrlik

Any news on this? Is it possible that "\/Date(millis)\/" format will be supported somehow?

martindrlik avatar Jun 12 '21 20:06 martindrlik

There is a proposed API for adding support for writing raw json in https://github.com/dotnet/runtime/issues/1784. We are planning on releasing this in time for .NET 6.

I'm going to close this issue, feel free to contribute to the conversation in the linked issue.

eiriktsarpalis avatar Jun 14 '21 10:06 eiriktsarpalis