Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BsonDataReader does not handle deserializing arrays, but BsonDataWriter does, so does Newtonsoft.json serializer. #43

Open
htmlsplash opened this issue Jun 19, 2024 · 0 comments

Comments

@htmlsplash
Copy link

The BsonDataReader throws the following exception when deserializing byte[] that contains an array of integers:

Newtonsoft.Json.JsonSerializationException
HResult=0x80131500
Message=Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Int32[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '0'.
Source=Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at PrlSystems.Serialization.ObjectSerializerUsingBsonFormatter.DeserializeObjectFromBytes(Byte[] bytes, SerializationSettings settings) in C:\PRL\DEV\Libs\CurrentLibraries\PrlSystemsLibrary\Serialization\ObjectSerializerUsingBsonFormatter.cs:line 54
at PrlSystems.Serialization.Base64SerializerUsingBsonFormatter.DeserializeObject(String str, SerializationSettings settings) in C:\PRL\DEV\Libs\CurrentLibraries\PrlSystemsLibrary\Serialization\Base64SerializerUsingBsonFormatter.cs:line 20
at PrlSystems.Serialization.SerializerUtil.DeserializeObjectFrom64Str(String str, IBase64Serializer serializer, SerializationSettings settings) in C:\PRL\DEV\Libs\CurrentLibraries\PrlSystemsLibrary\Serialization\SerializerUtil.cs:line 23
at Pimarcnet.WebAppCode.Shared.Web.QueryStringHelper.GetObjectFromQueryString(String objKey, Type type) in C:\PRL\DEV\Libs\CurrentLibraries\Pimarcnet.WebAppCode\Shared\Web\QueryStringHelper.cs:line 78
at Pimarcnet.WebAppCode.Shared.Web.QueryStringHelper.GetObjectFromQueryString[T](String objKey) in C:\PRL\DEV\Libs\CurrentLibraries\Pimarcnet.WebAppCode\Shared\Web\QueryStringHelper.cs:line 46
at Pimarcnet.Pages.Printout.PrintProjectData._GetCriteria() in C:\PRL\DEV\Pimarcnet\Pimarcnet\Pages\Printout\PrintProjectData.aspx.cs:line 29
at Pimarcnet.Pages.Printout.PrintProjectData._Init() in C:\PRL\DEV\Pimarcnet\Pimarcnet\Pages\Printout\PrintProjectData.aspx.cs:line 20
at Pimarcnet.Pages.Printout.PrintProjectData.Page_Load(Object sender, EventArgs e) in C:\PRL\DEV\Pimarcnet\Pimarcnet\Pages\Printout\PrintProjectData.aspx.cs:line 15

Here's the code (I included both serialization/deserialization code... Serialization code works, deserialization throws):

`var typeBeingSerialized = typeof(int[]);

JsonSerializer serializer = new JsonSerializer();

byte[] bytes;
int[] arr1 = { 1 };

using (var ms = new MemoryStream())
{
using (var writer = new BsonDataWriter(ms))
{
serializer.Serialize(writer, arr1, typeBeingSerialized);
bytes = ms.ToArray();
}
}

object result;

using (var ms = new MemoryStream(bytes))
{
using (var reader = new BsonDataReader(ms))
{
result = serializer.Deserialize(reader, typeBeingSerialized); // throws here!
}
}`

NOTE:

The Newtonsoft.json library handles of serializing/deserializing arrays. I have code for that if you need to see it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant