using MongoDB.Bson;
namespace adas_core.Domain.Utils;
///
/// Provides static utility methods for working with BSON (Binary JSON) data.
///
public static class BsonUtils
{
///
/// Converts a to a corresponding common language runtime (CLR) scalar object, supporting , , , (normalized to UTC), and values.
///
/// The instance to convert to a CLR object.
/// The underlying CLR value when is an Int32, Int64, Double, valid DateTime (in UTC), or String; otherwise .
public static object? ToObject(this BsonValue val)
{
if (val.IsInt32) return val.AsInt32;
if (val.IsInt64) return val.AsInt64;
if (val.IsDouble) return val.AsDouble;
if (val.IsValidDateTime) return val.ToUniversalTime();
if (val.IsString) return val.AsString;
return null;
}
///
/// Gets the value associated with the specified key from the BSON document, returning null if the key is not found.
///
/// The BSON document to search for the key.
/// The key of the value to retrieve.
/// The associated with , or null if the key does not exist.
public static BsonValue? Get(this BsonDocument doc, string key)
{
return doc.TryGetValue(key, out var value) ? value : null;
}
}