2

I am wokring a asp.net core project with mongoDB.

I want to get the list of booked hotels from HotelBookingDocument.

public class HotelBookingDocument
{
    public string Id { get; set; }
    public HotelDocument Hotel { get; set; }
// removed rest 
}

public class HotelDocument
{
    public List<RoomDocument> Rooms { get; set; }
// removed rest 
}

public class RoomDocument
{
      public object SupplierReference { get; set; }
// removed rest 
}
// removed rest 

Configuration

BsonClassMap.RegisterClassMap<HotelBookingDocument>(cm =>
{
  cm.AutoMap();
  cm.SetIgnoreExtraElements(true);
  cm.MapIdMember(c => c.Id)
    .SetSerializer(new StringSerializer(BsonType.ObjectId))
    .SetIgnoreIfDefault(true);
});

BsonClassMap.RegisterClassMap<HotelBookingDocument.RoomDocument>(cm =>
{
  cm.AutoMap();
});
            BsonClassMap.RegisterClassMap<HotelBookingDocument.HotelDocument>(cm =>
{
  cm.AutoMap();
});

Query

public async Task<List<HotelBookingDocument>> GetHotelsList(string transactionId)
{
  var hotels = await HotelBookingCollection.Find(x => x.TransactionId == transactionId).ToListAsync();   // this does not work
  return hotels;
}

Screenshot of SupplierReference Property in Database

Screenshot of SupplierReference Property in Database

Why this "_t": "JsonElement" did add automatically?

"SupplierReference": {
                "_t": "JsonElement"
            },

Error:

System.FormatException: An error occurred while deserializing the Hotel property of class HotelBookingDocument: An error occurred while deserializing the Rooms property of class HotelBookingDocument+HotelDocument: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> System.FormatException: An error occurred while deserializing the Rooms property of class HotelBookingDocument+HotelDocument: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> System.FormatException: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> MongoDB.Bson.BsonSerializationException: Unknown discriminator value 'JsonElement'.
 at MongoDB.Bson.Serialization.BsonSerializer.LookupActualType(Type nominalType, BsonValue discriminator)
 at MongoDB.Bson.Serialization.Conventions.ObjectDiscriminatorConvention.GetActualType(IBsonReader bsonReader, Type nominalType)
 at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
 at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)...............

when I try to get list of hotelBookings from HotelBookingCollection by transactionId, I got above error. But I find a hotelBooking from HotelBookingCollection i did not get any error. I can get first hotelBooking.

Why this error occur?

Please help me to find the issue.

Thank you

hanushi
  • 1,169
  • 2
  • 12
  • 27
  • `JsonElement` is [`System.Text.Json.JsonElement`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonelement?view=net-6.0). When you use the [`System.Text.Json`](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to) serializer to deserialize JSON to something declared as type `object`, the serializer actually deserializes to this type. – dbc Jul 02 '22 at 13:51
  • I'm not familiar with mongodb, but you might try applying `[System.Text.Json.Serialization.JsonConverter(typeof(ObjectAsPrimitiveConverter))]` to `public object SupplierReference { get; set; }` where `ObjectAsPrimitiveConverter` comes from [this answer](https://stackoverflow.com/a/65974452/3744182) to [C# - Deserializing nested json to nested Dictionary](https://stackoverflow.com/a/65974452/3744182). – dbc Jul 02 '22 at 13:55
  • `ObjectAsPrimitiveConverter` will deserialize JSON objects to `ExpandoObject` which, according to [this answer](https://stackoverflow.com/a/8584187/3744182) by [Ryan](https://stackoverflow.com/users/291955/ryan) to [Persisting an ExpandoObject to MongoDB](https://stackoverflow.com/q/5073209/3744182), are already supported by MongoDB. If that answer is not correct and MongoDB does not support ExpandoObject, change the default constructor of `ObjectAsPrimitiveConverter` to be `this(FloatFormat.Double, UnknownNumberFormat.Error, ObjectFormat.Dictionary)`. – dbc Jul 02 '22 at 13:57
  • And if you are deserializing financial information you might also want to set `FloatFormat.Decimal` to ensure there are no roundoff errors when deserializing floating point numbers from JSON. – dbc Jul 02 '22 at 14:01

0 Answers0