private void LoadCompiledProgramV1(byte[] bytes) { using (var stream = new System.IO.MemoryStream(bytes)) { using (var reader = new Newtonsoft.Json.Bson.BsonReader(stream)) { var serializer = new Newtonsoft.Json.JsonSerializer(); try { // Load the stored program var newProgram = serializer.Deserialize <Program>(reader); // Merge it with our existing one, if present if (program != null) { program.Include(newProgram); } else { program = newProgram; } } catch (Newtonsoft.Json.JsonReaderException e) { LogErrorMessage(string.Format("Cannot load compiled program: {0}", e.Message)); } } } }
/// <summary> /// Creates an instance of Products from a base64 encoded BSON string /// </summary> /// <param name="bson">The base64 encoded BSON string</param> /// <returns>A Products object instance</returns> public static Products FromBson(string bson) { List <DataLayer.FooEnterprises.Product.SerializableProduct> zc; byte[] data = Convert.FromBase64String(bson); Products tmp = new Products(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data)) { using (Newtonsoft.Json.Bson.BsonReader reader = new Newtonsoft.Json.Bson.BsonReader(ms)) { reader.ReadRootValueAsArray = true; Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); zc = serializer.Deserialize <List <DataLayer.FooEnterprises.Product.SerializableProduct> >(reader); } } foreach (DataLayer.FooEnterprises.Product.SerializableProduct z in zc) { tmp.Add(Product.FromJson(Newtonsoft.Json.JsonConvert.SerializeObject(z))); } if (zc.Count > 0) { Encryption64 decryptor = new Encryption64(); tmp._connectionString = decryptor.Decrypt(zc[0].SerializationConnectionString, DataLayer.FooEnterprises.Universal.LayerGenEncryptionKey); } return(tmp); }
protected T DeserializeUsingNewtonsoftReader <T>(byte[] bson, bool mustBeNested = false) { using (var memoryStream = new MemoryStream(bson)) using (var newtonsoftReader = new Newtonsoft.Json.Bson.BsonReader(memoryStream)) { newtonsoftReader.DateTimeKindHandling = System.DateTimeKind.Utc; newtonsoftReader.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; if (mustBeNested) { newtonsoftReader.Read(); // StartObject newtonsoftReader.Read(); // PropertyName } newtonsoftReader.Read(); // Json.NET serializers expect caller to have already called Read var newtonsoftSerializer = new Newtonsoft.Json.JsonSerializer(); var value = newtonsoftSerializer.Deserialize <T>(newtonsoftReader); if (mustBeNested) { newtonsoftReader.Read(); // EndObject } return(value); } }
private static T DeserializeWithJsonNetBson <T>(byte[] byteArray, JsonNetJsonSerializer serializer) { using (var memStream = new MemoryStream(byteArray)) { var reader = new JsonNetBsonReader(memStream); return(serializer.Deserialize <T>(reader)); } }
protected T ReadJsonUsingNativeBsonReader <T>(Newtonsoft.Json.JsonConverter converter, byte[] bson, bool mustBeNested = false) { using (var stream = new MemoryStream(bson)) using (var reader = new Newtonsoft.Json.Bson.BsonReader(stream)) { return(ReadJson <T>(converter, reader, mustBeNested)); } }
public static T DeSerialise <T>(byte[] rawData) { System.IO.MemoryStream ms = new System.IO.MemoryStream(rawData); Newtonsoft.Json.Bson.BsonReader br = new Newtonsoft.Json.Bson.BsonReader(ms); T result = ser.Deserialize <T>(br); return(result); }
static T DeserializeWithJsonNetBson <T>(byte[] bytes) { using (var memStream = new MemoryStream(bytes)) using (var reader = new Newtonsoft.Json.Bson.BsonReader(memStream)) { var serializer = new Newtonsoft.Json.JsonSerializer(); return(serializer.Deserialize <T>(reader)); } }
//http://james.newtonking.com/archive/2009/12/26/json-net-3-5-release-6-binary-json-bson-support.aspx public static T DeserializeJsonNetBson <T>(byte[] bson) { using (var ms = new MemoryStream(bson)) { var serializer = new Newtonsoft.Json.JsonSerializer(); var reader = new Newtonsoft.Json.Bson.BsonReader(ms); return(serializer.Deserialize <T>(reader)); } }
public static Newtonsoft.Json.Linq.JToken UnPack(byte[] bytes) { using (var ms = new System.IO.MemoryStream(bytes)) { using (var bsreader = new Newtonsoft.Json.Bson.BsonReader(ms)) { return(Newtonsoft.Json.Linq.JToken.ReadFrom(bsreader)); } } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="data"></param> /// <returns></returns> public static T DeserializeBson <T>(byte[] data) { try { using (var file = new System.IO.MemoryStream(data)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); Newtonsoft.Json.Bson.BsonReader reader = new Newtonsoft.Json.Bson.BsonReader(file); return(serializer.Deserialize <T>(reader)); } } catch (Exception) { throw; } }
public async Task <JObject> PostRequestAPI(string url, object data = null) { try { return(await Task.Run <JObject>(async() => { var content = await PostRequestAPIStream(url, data); Newtonsoft.Json.Bson.BsonReader reader = new Newtonsoft.Json.Bson.BsonReader(content); return JObject.Load(reader); })); } catch (Exception ex) { Insight.Track(ex.Message, ChadderError.CONNECTION_ERROR); var r = new BasicResponse(); r.Error = ChadderError.CONNECTION_ERROR; r.InnerException = ex; return(JObject.FromObject(r)); } }
internal static TValue FromBson <TValue>(byte[] buffer) { TValue value; using (MemoryStream ms = new MemoryStream(buffer)) { using (var reader = new Newtonsoft.Json.Bson.BsonReader(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer { Formatting = Newtonsoft.Json.Formatting.None, Culture = CultureInfo.InvariantCulture, TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple, }; value = serializer.Deserialize <TValue>(reader); } } return(value); }
public async Task <T> PostRequestAPI <T>(string url, object data = null) where T : BasicResponse, new() { try { return(await Task.Run <T>(async() => { var content = await PostRequestAPIStream(url, data); Newtonsoft.Json.Bson.BsonReader reader = new Newtonsoft.Json.Bson.BsonReader(content); var serializer = new JsonSerializer(); return serializer.Deserialize <T>(reader); })); } catch (Exception ex) { Insight.Track(ex.Message, ChadderError.CONNECTION_ERROR); var r = new T(); r.Error = ChadderError.CONNECTION_ERROR; r.InnerException = ex; return(r); } }
public string ToJson(BsonDocument bson) { using (var stream = new MemoryStream()) { using (var writer = new BsonBinaryWriter(stream)) { BsonSerializer.Serialize(writer, typeof(BsonDocument), bson); } stream.Seek(0, SeekOrigin.Begin); using (var reader = new Newtonsoft.Json.Bson.BsonReader(stream)) { var sb = new StringBuilder(); var sw = new StringWriter(sb); using (var jWriter = new JsonTextWriter(sw)) { jWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc; jWriter.WriteToken(reader); } return(sb.ToString()); } } }