mirror of https://github.com/Squidex/squidex.git
4 changed files with 97 additions and 2 deletions
@ -0,0 +1,39 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading; |
|||
using MongoDB.Bson.Serialization; |
|||
using MongoDB.Bson.Serialization.Serializers; |
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.MongoDb.Contents |
|||
{ |
|||
public sealed class StatusSerializer : SerializerBase<Status2> |
|||
{ |
|||
private static volatile int isRegistered; |
|||
|
|||
public static void Register() |
|||
{ |
|||
if (Interlocked.Increment(ref isRegistered) == 1) |
|||
{ |
|||
BsonSerializer.RegisterSerializer(new StatusSerializer()); |
|||
} |
|||
} |
|||
|
|||
public override Status2 Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
|||
{ |
|||
var value = context.Reader.ReadString(); |
|||
|
|||
return new Status2(value); |
|||
} |
|||
|
|||
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, Status2 value) |
|||
{ |
|||
context.Writer.WriteString(value.Name); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using MongoDB.Bson; |
|||
using MongoDB.Bson.IO; |
|||
using MongoDB.Bson.Serialization; |
|||
using Squidex.Domain.Apps.Core.Contents; |
|||
using Squidex.Domain.Apps.Entities.MongoDb.Contents; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Domain.Apps.Entities.Contents.MongoDb |
|||
{ |
|||
public sealed class StatusSerializerTests |
|||
{ |
|||
private sealed class TestObject |
|||
{ |
|||
public Status2 Status { get; set; } |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_serialize_and_deserialize_status() |
|||
{ |
|||
StatusSerializer.Register(); |
|||
|
|||
var source = new TestObject |
|||
{ |
|||
Status = new Status2("Published") |
|||
}; |
|||
|
|||
var document = new BsonDocument(); |
|||
|
|||
using (var writer = new BsonDocumentWriter(document)) |
|||
{ |
|||
BsonSerializer.Serialize(writer, source); |
|||
|
|||
writer.Flush(); |
|||
} |
|||
|
|||
using (var reader = new BsonDocumentReader(document)) |
|||
{ |
|||
var result = BsonSerializer.Deserialize<TestObject>(reader); |
|||
|
|||
Assert.Equal(source.Status, result.Status); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue