1

Is there a function (perhaps in ODataLib 5.6.1) to serialize a .NET primitive type to OData URI format (see)

for instance:

new DateTime(2000,12,12).ToODataString() //would return: datetime’2000-12-12T12:00′
Nestor
  • 13,706
  • 11
  • 78
  • 119
  • There are a [set of built in classes](http://msdn.microsoft.com/en-us/library/system.web.http.odata.formatter.serialization.odataserializer%28v=vs.111%29.aspx), but it isn't going to be pretty. – Haney Apr 28 '14 at 16:48
  • Those classes seem to be for the payload, not for uri. I am wrong? – Nestor Apr 28 '14 at 16:58
  • probably not wrong. I more meant to show you a path to some of the documentation around the OData namespace... I don't believe that the serializer is A) simple and can be applied to any object, or 2) yet made public. Sorry. – Haney Apr 28 '14 at 17:22
  • [This may also help](http://msdn.microsoft.com/en-us/library/hh525392%28v=vs.103%29.aspx) – Haney Apr 28 '14 at 17:22

2 Answers2

2

With static class Microsoft.Data.OData.Query.ODataUriUtils, you can call ConvertToUriLiteral to serialize value to uri literal.

public static string ConvertToUriLiteral(object value, ODataVersion version)
public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model)
public static string ConvertToUriLiteral(object value, ODataVersion version, IEdmModel model, ODataFormat format)

And you can call ConvertFromUriLiteral to do deserialization.

public static object ConvertFromUriLiteral(string value, ODataVersion version)
public static object ConvertFromUriLiteral(string value, ODataVersion version, IEdmModel model, IEdmTypeReference typeReference)
Feng Zhao
  • 2,977
  • 1
  • 14
  • 20
1

Here is my temporary implementation until someone knows the function in ODataLib.

static public string ToODataUriString(object value)
{
    if (value == null) return "null";
    switch (value.GetType().FullName)
    {
        case "System.Guid":
            return string.Format("guid'{0}'", (Guid)value);
        case "System.Decimal":
            return string.Format("{0}m", (decimal)value);
        case "System.Single":
            return string.Format("{0}f", ((Single)value).ToString("R"));
        case "System.Double":
            return string.Format("{0}d", ((double)value).ToString("R"));
        case "System.Boolean":
            return string.Format("{0}", (Boolean)value).ToLower();
        case "System.DateTime":
            {
                DateTime dvalue = (DateTime)value;
                string format;
                if (dvalue.Second==0 && dvalue.Millisecond==0)
                    format = "yyyy-MM-ddTHH:mm"; //datetime’yyyy-mm-ddThh:mm[:ss[.fffffff]]’
                else if (dvalue.Millisecond == 0)
                    format = "yyyy-MM-ddTHH:mm:ss"; //datetime’yyyy-mm-ddThh:mm[:ss[.fffffff]]’
                else
                    format = "yyyy-MM-ddTHH:mm:ss.fffffff"; //datetime’yyyy-mm-ddThh:mm[:ss.fffffff]]’
                return string.Format("datetime'{0}'", dvalue.ToString(format));
            }
        case "System.DateTimeOffset":
            return string.Format("datetimeoffset'{0}Z'", ((DateTimeOffset)value).ToString("s"));
        case "System.Int16":
        case "System.Int32":
        case "System.Byte":
            return value.ToString();
        case "System.Int64":
            return string.Format("{0}L", value);
        case "System.TimeSpan":
            return string.Format("time'{0}'", (TimeSpan)value);
        case "System.String":
            // TODO Need better quote handling for escaping, this is not correct
            return string.Format("'{0}'", value.ToString().Replace("'", "''"));
        default:
            // TODO Need better quote handling for escaping, this is not correct
            return string.Format("'{0}'", value.ToString().Replace("'", "''"));
    }
}
Nestor
  • 13,706
  • 11
  • 78
  • 119