WebApi odata: Serialize long strings

I am migrating a WCF data service form to Web API odata v4. WCF Data Service has done serialization of quoted long values:

{
   "value":[{
     "ID":"4527895973896126465"
   },{
     "ID":"4527895973896126466"
   }]
}

      

Web API odata doesn't:

{
   "value":[{
     "ID":4527895973896126465
   },{
     "ID":4527895973896126466
   }]
}

      

This means that I lost the 64 bit number precision during JSON.parse in JavaScript, since JavaScript numbers are only 53 bits.

Does WebApi have a build mechanism for handling long values ​​as string values? I am thinking about the IEEE754Compatible header element. But this does not affect the generated response. Am I missing something?

An alternative solution would be to deserilaize 64 bit numbers as string values ​​during JSON.parse on the client side. Is it possible?

+3


source to share


2 answers


Finally I got this to work. OdataLib does support this with the IEEE754Compatible parameter. It checks the response of the Content-Type header to see if this parameter is present.

The thing is that the header value is not automatically propagated to the response header in the web framework api. You have to do it yourself. I built a derived class ODataController that fixes the IEEE754Compatible parameter in the response Content-Type header like so:

public abstract class ODataControllerIEEE754Compatible : ODataController 
{
    private void PatchResponse(HttpResponseMessage responseMessage)
    {
        if (responseMessage != null && responseMessage.Content != null)
        {
           if (this.Request.Content.Headers.GetValues("Content-Type").Any(
               h => h.Contains("IEEE754Compatible=true")))
           {
               responseMessage.Content.Headers.TryAddWithoutValidation(
                  "Content-Type", "IEEE754Compatible=true");
           }
       }
    }

    public override Task<HttpResponseMessage> ExecuteAsync(
       HttpControllerContext controllerContext, CancellationToken cancellationToken)
    {
            var response = base.ExecuteAsync(
               controllerContext, cancellationToken);
            response.Wait(cancellationToken);

            PatchResponse(response.Result);

            return response;
    }
}

      



Now, by sending the IEEE754Compatible = true parameter to the Content-Type header, I get all long values ​​serialized as JSON strings:

GET http://localhost/some/url HTTP/1.1
OData-Version: 4.0;
Content-Type: application/json;odata.metadata=minimal;IEEE754Compatible=true;charset=utf-8
Cache-Control: no-cache

HTTP/1.1 200 OK
Content-Type: application/json;odata.metadata=minimal;IEEE754Compatible=true
Server: Microsoft-HTTPAPI/2.0
OData-Version: 4.0

{
  "@odata.context":"http://localhost/some/url","value":[
{
  "ID":"4527895973896126465", ...

      

+1


source


Although I don't know much about ASP.net, I can give you a rexeg that can be used to add quotes around large numbers in JSON. Here I am setting it to any number of 16 digits or more.

http://jsfiddle.net/yryk70qz/1/
value.replace(/:\s*(\d{16,})(\s*[,\}])/g, ':"$1"$2');



You can do this with all numbers regardless of their length: value.replace(/:\s*(\d+)(\s*[,\}])/g, ':"$1"$2');

(related to this question: Converts all integer value to string in JSON )

+1


source







All Articles