|
|
|
@ -5,6 +5,7 @@ |
|
|
|
// All rights reserved. Licensed under the MIT license.
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
using MongoDB.Bson; |
|
|
|
using MongoDB.Bson.Serialization; |
|
|
|
using MongoDB.Bson.Serialization.Serializers; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
@ -15,18 +16,38 @@ namespace Squidex.Infrastructure.MongoDb |
|
|
|
{ |
|
|
|
public static readonly JTokenSerializer<T> Instance = new JTokenSerializer<T>(); |
|
|
|
|
|
|
|
protected override T DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args) |
|
|
|
public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) |
|
|
|
{ |
|
|
|
var jsonReader = new BsonJsonReader(context.Reader); |
|
|
|
var bsonReader = context.Reader; |
|
|
|
|
|
|
|
return (T)JToken.ReadFrom(jsonReader); |
|
|
|
if (bsonReader.GetCurrentBsonType() == BsonType.Null) |
|
|
|
{ |
|
|
|
bsonReader.ReadNull(); |
|
|
|
|
|
|
|
return null; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var jsonReader = new BsonJsonReader(bsonReader); |
|
|
|
|
|
|
|
return (T)JToken.ReadFrom(jsonReader); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, T value) |
|
|
|
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, T value) |
|
|
|
{ |
|
|
|
var jsonWriter = new BsonJsonWriter(context.Writer); |
|
|
|
var bsonWriter = context.Writer; |
|
|
|
|
|
|
|
if (value == null) |
|
|
|
{ |
|
|
|
bsonWriter.WriteNull(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
var jsonWriter = new BsonJsonWriter(bsonWriter); |
|
|
|
|
|
|
|
value.WriteTo(jsonWriter); |
|
|
|
value.WriteTo(jsonWriter); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|