Valid JSON but 'Cocoa Error 3840' from AFNetworking / NSJSONSerialization

I tried to crack this problem for hours, but it didn’t help - it seems my only option is to post here to find out if anyone can shed light on this problem. It could be a problem with AFNetworking, or (more likely) it could be a problem with my code.

The code I use works fine for 99% of the operations in my application. I am developing an application that uses Elastic Search, creating JSON requests, sending them and receiving a response from the server.

Here is an example of the returned JSON:

{ "took": 4, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": null, "hits": [ { "_index": "asx", "_type": "61a88d3848b00655d9aa59db70847318", "_id": "b91f9257744fedb4ef1c127e275c127c", "_score": null, "_source": { "value": "22/06/1998" }, "sort": [ 4.439049394553e-312 ] } ] } } 

Now, by connecting this to jsonlint.com (and learning a bit about JSON formatting), it's easy to see that it is really JSON.

I use a subclass of AFHTTPClient to POST my request and receive data. Here is the code I'm using for POST:

 [super postPath:path parameters:parameters success:^(AFHTTPRequestOperation *operation, NSDictionary *response) { NSData *responseData = [(AFJSONRequestOperation *)operation responseData]; NSDictionary *responseDictionary; if (responseData != nil) { responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; } if (success) { success(operation, responseDictionary); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSData *responseData = [(AFJSONRequestOperation *)operation responseData]; NSDictionary *responseDictionary; if (responseData != nil) { responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:NULL]; } if (failure) { failure(operation, error, responseDictionary); } }]; 

Nothing special here, I'm just canceling the response in some kind of JSON.

However, the problem is that for this, in a particular request, AFNetworking considers the response as a failure, so the code in the fault block is the only code that can be executed. I get the following error:

 (lldb) po error $1 = 0x0adbc710 Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Number wound up as NaN around character 279.) UserInfo=0xad968d0 {NSDebugDescription=Number wound up as NaN around character 279.} 

JSON contains an exponential number (4.439049394553e-312), however this is a real number and should be analyzed. I get the same NSError when I try to parse the response data using NSJSONSerialization. Just to clarify - AFNetworking gives me the same error message as NSJSONSerialization.

I cannot find anyone else who has the same problem as me and cannot understand why my JSON cannot be parsed. This leaves my application with a very big error that I cannot fix.

If someone can shed light on this question, that would be great. If this is not a problem with AFNetworking, if you could point me to a useful resource that would also be awesome. Of course, if you need more information, please ask

Thanks.

+6
source share
1 answer

NSJSONSerialization uses NSDecimalNumber to represent numbers, and

 [NSDecimalNumber decimalNumberWithString:@"4.439049394553e-312"] 

already returns NaN , because NSDecimalNumber can only represent numbers

 mantissa x 10^exponent where `-128 <= exponent <= 127`. 

So this seems like a “limitation” (or mistake) of NSJSONSerialization that it only works with numbers in a certain range.

I did a quick test with "SBJsonParser" and it had the same problem.

+6
source

Source: https://habr.com/ru/post/953289/


All Articles