Skip to content

Commit

Permalink
Fix BSON serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
filipbekic01 committed Sep 22, 2024
1 parent da0aa17 commit 693e885
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/ResQueue/ResQueue/Endpoints/QueueEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void MapQueueEndpoints(this IEndpointRouteBuilder routes)
return Results.Ok(queues.Select(x => new QueueDto()
{
Id = x.Id.ToString(),
RawData = x.RawData.ToString(),
RawData = JsonHelper.ConvertBsonToJson(x.RawData),
TotalMessages = x.TotalMessages,
IsFavorite = x.IsFavorite,
CreatedAt = x.CreatedAt
Expand Down Expand Up @@ -137,7 +137,7 @@ public static void MapQueueEndpoints(this IEndpointRouteBuilder routes)
Items = queues.Select(x => new QueueDto()
{
Id = x.Id.ToString(),
RawData = x.RawData.ToString(),
RawData = JsonHelper.ConvertBsonToJson(x.RawData),
TotalMessages = x.TotalMessages,
IsFavorite = x.IsFavorite,
CreatedAt = x.CreatedAt
Expand Down
20 changes: 20 additions & 0 deletions backend/ResQueue/ResQueue/JsonHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using MongoDB.Bson;
using MongoDB.Bson.IO;

namespace ResQueue;

public static class JsonHelper
{
public static string ConvertBsonToJson(BsonDocument bsonDocument)
{
// Relaxed converts Int64 to a standard number representation in JSON
// instead of MongoDB-specific types like NumberLong(), ensuring
// compatibility with JSON parsers that expect standard number types.

// This mode also normalizes other BSON types, like dates and binary data,
// into formats that work seamlessly in typical JSON contexts.

var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.RelaxedExtendedJson };
return bsonDocument.ToJson(jsonSettings);
}
}

0 comments on commit 693e885

Please sign in to comment.