mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using MongoDB.Bson.IO;
|
|
using MongoDB.Bson.Serialization;
|
|
using MongoDB.Bson.Serialization.Attributes;
|
|
|
|
namespace Squidex.MongoDb.TestHelpers;
|
|
|
|
public static class MongoTestUtils
|
|
{
|
|
public sealed class ObjectHolder<T>
|
|
{
|
|
[BsonRequired]
|
|
public T Value1 { get; set; }
|
|
|
|
[BsonRequired]
|
|
public T Value2 { get; set; }
|
|
}
|
|
|
|
public static T SerializeAndDeserializeBson<T>(this T value)
|
|
{
|
|
return value.SerializeAndDeserializeBson<T, T>();
|
|
}
|
|
|
|
public static TOut SerializeAndDeserializeBson<TOut, TIn>(this TIn value)
|
|
{
|
|
using var stream = new MemoryStream();
|
|
|
|
using (var writer = new BsonBinaryWriter(stream))
|
|
{
|
|
BsonSerializer.Serialize(writer, new ObjectHolder<TIn> { Value1 = value, Value2 = value });
|
|
}
|
|
|
|
stream.Position = 0;
|
|
|
|
using (var reader = new BsonBinaryReader(stream))
|
|
{
|
|
return BsonSerializer.Deserialize<ObjectHolder<TOut>>(reader).Value1;
|
|
}
|
|
}
|
|
}
|
|
|