| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.IO;
- using MongoDB.Bson;
- using MongoDB.Bson.IO;
- using MongoDB.Bson.Serialization;
- namespace Common.Helper
- {
- public static class MongoHelper
- {
- public static string ToJson(object obj)
- {
- return obj.ToJson();
- }
- public static string ToJson(object obj, JsonWriterSettings settings)
- {
- return obj.ToJson(settings);
- }
- public static T FromJson<T>(string str)
- {
- return BsonSerializer.Deserialize<T>(str);
- }
- public static byte[] ToBson(object obj)
- {
- return obj.ToBson();
- }
- public static object FromBson(Type type, byte[] bytes)
- {
- return BsonSerializer.Deserialize(bytes, type);
- }
- public static object FromBson(Type type, byte[] bytes, int index, int count)
- {
- using (MemoryStream memoryStream = new MemoryStream(bytes))
- {
- memoryStream.Seek(index, SeekOrigin.Begin);
- memoryStream.Seek(index + count, SeekOrigin.End);
- return BsonSerializer.Deserialize(memoryStream, type);
- }
- }
- public static T FromBson<T>(byte[] bytes, int index = 0)
- {
- using (MemoryStream memoryStream = new MemoryStream(bytes))
- {
- memoryStream.Seek(index, SeekOrigin.Begin);
- return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
- }
- }
- public static T FromBson<T>(byte[] bytes, int index, int count)
- {
- using (MemoryStream memoryStream = new MemoryStream(bytes))
- {
- memoryStream.Seek(index, SeekOrigin.Begin);
- memoryStream.Seek(index + count, SeekOrigin.End);
- return (T) BsonSerializer.Deserialize(memoryStream, typeof (T));
- }
- }
- }
- }
|