[CSHARP-354] JsonReader: No valid JSON format for reading Int64 Created: 09/Nov/11  Updated: 11/Mar/19  Resolved: 10/Nov/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Derek Ludwig Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: json
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File 0001-Enabled-JsonReader-to-read-an-Int64-when-the-input-v.patch    
Backwards Compatibility: Fully Compatible

 Description   

There is no way for JsonReader to read a strict JSON-conforming Int64 in the range int.MinValue to int.MaxValue. This is an issue if reading documents generated by a strict JSON implementation.

Possible solutions:

  • permitting CurrentBsonType to be Int32 or Int64 in ReadInt64
  • an additional extended format, i.e. "
    Unknown macro: { "bigValue"}

    "; forces bigValue to be an Int64 regardless of the actual value.



 Comments   
Comment by Robert Stam [ 10/Nov/11 ]

OK. Thanks for letting me know.

Comment by Derek Ludwig [ 10/Nov/11 ]

Yes, there is custom serialization. After taking a look at the class map serializer and other default primitive serializers, I see how those ones are handling this particular issue, and it seems like a reasonable solution. I will integrate that solution into the custom serialization.

You can close this issue.

Thanks.

Comment by Robert Stam [ 09/Nov/11 ]

I can't reproduce this. Here's my test code:

    [TestFixture]
    public class CSharp354Tests {
        private class Data {
            public long Value { get; set; }
        }
 
        [Test]
        public void TestDeserialize123() {
            var json = "{ Value : 123 }";
            var data = BsonSerializer.Deserialize<Data>(json);
        }
 
        [Test]
        public void TestDeserializeNumberLong32() {
            var json = "{ Value : NumberLong(123) }";
            var data = BsonSerializer.Deserialize<Data>(json);
        }
    }

Are you trying to do your own deserialization?

Comment by Derek Ludwig [ 09/Nov/11 ]

A patch that contains a possible solution.

Comment by Derek Ludwig [ 09/Nov/11 ]

Sorry, it looks like my examples were garbled somehow.

C# Class:

public class Data {
   public long Value { get; set; }
}

Possible input strings:

{ "Value": 123 }
 
{ "Value": { "$long": 123 }}

The use case I'm dealing with is parsing JSON produced by a browser or json2.js (JSON.stringify). Currently, the first string throws an InvalidOperationException saying that ReadInt64 cannot be called when CurrentBsonType is Int32; the simplest solution would be to permit ReadInt64 in that case, and return bsonValue.ToInt64().

Comment by Robert Stam [ 09/Nov/11 ]

I don't understand what you are asking for. Can you explain it a different way? Perhaps show a sample input string and describe what you want the result to be.

Generated at Wed Feb 07 21:36:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.