JamesNK/Newtonsoft.Json · error · ArgumentOutOfRangeException
Unexpected BsonType value: {0}
Error message
Unexpected BsonType value: {0} What it means
Thrown by BsonReader.ReadType's default case when it reads a type byte from the BSON stream that does not match any known BsonType (it handles Number, String, Object, Array, Binary, Undefined, Oid, Boolean, Date, Null, Reference, Code, CodeWScope, Integer, TimeStamp, Long). It is an ArgumentOutOfRangeException reporting the raw BsonType value encountered.
Source
Thrown at Src/Newtonsoft.Json/Bson/BsonReader.cs:568
SetToken(JsonToken.StartObject);
_bsonReaderState = BsonReaderState.ReferenceStart;
break;
case BsonType.Code:
SetToken(JsonToken.String, ReadLengthString());
break;
case BsonType.CodeWScope:
SetToken(JsonToken.StartObject);
_bsonReaderState = BsonReaderState.CodeWScopeStart;
break;
case BsonType.Integer:
SetToken(JsonToken.Integer, (long)ReadInt32());
break;
case BsonType.TimeStamp:
case BsonType.Long:
SetToken(JsonToken.Integer, ReadInt64());
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + type);
}
}
private byte[] ReadBinary(out BsonBinaryType binaryType)
{
int dataLength = ReadInt32();
binaryType = (BsonBinaryType)ReadByte();
#pragma warning disable 612,618
// the old binary type has the data length repeated in the data for some reason
if (binaryType == BsonBinaryType.BinaryOld && !_jsonNet35BinaryCompatibility)
{
dataLength = ReadInt32();
}
#pragma warning restore 612,618
return ReadBytes(dataLength);View on GitHub (pinned to 4f73e74372)
Solutions
- Verify the input stream is genuinely BSON and not another binary format.
- Check the byte value reported in the message against the BSON spec; if it is a newer type (e.g. Decimal128 0x13), upgrade to the dedicated Newtonsoft.Json.Bson package or MongoDB driver.
- Validate stream integrity (length prefix, CRC) before deserialization.
- Ensure the writer and reader use compatible BSON type vocabularies.
Example fix
// before: feeding arbitrary bytes
using var reader = new BsonReader(ms);
var obj = serializer.Deserialize(reader, typeof(Foo));
// after: confirm the stream is BSON before reading
if (!LooksLikeBson(ms)) throw new InvalidDataException("not BSON");
ms.Position = 0;
using var reader = new BsonReader(ms); Defensive patterns
Strategy: validation
Validate before calling
byte[] data = ReadFully(stream);
int docLen = BitConverter.ToInt32(data, 0);
if (data.Length < docLen) throw new InvalidDataException("truncated BSON");
// optionally validate type bytes are within known BsonType range (0x01..0x12 typical) Type guard
static bool IsKnownBsonType(byte t) =>
t == 0x01 || t == 0x02 || t == 0x03 || t == 0x04 || t == 0x05 || t == 0x06
|| t == 0x07 || t == 0x08 || t == 0x09 || t == 0x0A || t == 0x0B || t == 0x0C
|| t == 0x0D || t == 0x0E || t == 0x0F || t == 0x10 || t == 0x11 || t == 0x12; Try / catch
try { var v = serializer.Deserialize(bsonReader, type); }
catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("Unexpected BsonType"))
{
throw new InvalidDataException("BSON stream contains an unsupported/unknown type byte", ex);
} Prevention
- Validate the stream is BSON before deserialization (check length prefix and type bytes).
- Ensure writer and reader share the same BSON type vocabulary.
- Upgrade to the dedicated BSON package if you need newer BSON types (Decimal128, etc.).
When it happens
Trigger: Deserializing a BSON document where an element's type byte is an unknown/invalid value. Typically the result of a corrupt BSON stream, a version mismatch (the writer used a BSON type this reader build does not recognize), or feeding non-BSON binary data into BsonReader.
Common situations: Feeding arbitrary binary or a different serialization format into BsonReader by mistake. Reading BSON produced by a newer/older MongoDB driver using a type code Newtonsoft's BsonReader does not support. Network corruption or truncated data causing the reader to misalign and read a data byte as a type byte.
Related errors
- Unable to read beyond the end of the stream.
- Unexpected token when writing BSON: {0}
- Expected Bytes but got {0}.
- An ObjectId must be 12 bytes
- No object created.
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/80f90e862b33a59e.
Report an issue: GitHub.