1

Update: When you access a page that produces JSON as output with the chrome browser. Incorrect results are shown.

Consider this django code that produces json. When you json.dumps a long, the two digits are different from the expected value. Here is the code:

from django.http import JsonResponse, HttpResponse
import json

def dumps1(request):
    return JsonResponse({'pk': 456011173705795438 })


def dumps2(request):
    return HttpResponse(json.dumps({'pk': 455928532169112023 }))

Naturally, you would expect dumps1 to return and HttpResponse with the body as {'pk': 456011173705795438 } while dumps2 should return {'pk': 455928532169112023 } but actual results are

{
    pk: 456011173705795460
}

and

{
    pk: 455928532169112000
}

Notice that in both cases, the last two digits in the number have changed. If you open the django shell and type in json.dumps({'pk': 455928532169112023 }) the correct output is produced.

Django 1.8.5 and python 2.7.6 ( ipython 4.0.b1)

Is this a bug or a feature?

middlestump
  • 1,035
  • 8
  • 22
  • I just opened up python shell and did this: >>>from django.http import JsonResponse, HttpResponse >>>import json >>>j = JsonResponse({'pk': 455928532169112023 }) >>>h = HttpResponse(json.dumps({'pk': 455928532169112023 })) >>>for i in j: print i >>>for i in h: print i ------------ both printed b'{"pk": 455928532169112023}' which is the correct value. When you do the commands I did in your shell, does it give the same output? – SilentDev Oct 22 '15 at 01:10
  • yes @user2719875 this is getting curioser and curioser, your method returns the correct value – middlestump Oct 22 '15 at 01:52
  • @user2719875 I have found the answer, but it was thanks to your comment, which helped me to find it. Will you post it as an answer? I will give upvote. – middlestump Oct 22 '15 at 01:55

2 Answers2

3

As mentioned in the comments, I opened up python shell and did this:

>>>from django.http import JsonResponse, HttpResponse 
>>>import json 
>>>j = JsonResponse({'pk': 455928532169112023 }) 
>>>h = HttpResponse(json.dumps({'pk': 455928532169112023 })) 
>>>for i in j: 
    print i 
>>>for i in h: 
    print i

both printed b'{"pk": 455928532169112023}' which is the correct value. I am using Django 1.8 as well, so the problem may be somewhere else.

SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • While this did not directly solve the problem it did help me to find it. The problem was in the chrome Json extension that was being used. It was showing incorrect result!! – middlestump Oct 22 '15 at 02:10
1

After @user2719875 's answer. I looked for possible other problems. I was using chrome before. Checked in firefox and then wget and found that the result was correct. Then I disabled some extensions in chrome. The problem was in Chrome JsonView extension. (I will not link to it because it's buggy). The extension was displaying the values incorrectly!! That was a bug that wasted many hours of my time.

Update: After that I tried out several json viewers in the chrome store. The only one that seemed to produce the right result was 'JSON Viewer' by 'Tulios'

Community
  • 1
  • 1
middlestump
  • 1,035
  • 8
  • 22