protected byte[] WriteJsonUsingNativeBsonWriter(Newtonsoft.Json.JsonConverter converter, object value, bool mustBeNested = false) { using (var stream = new MemoryStream()) using (var writer = new Newtonsoft.Json.Bson.BsonWriter(stream)) { WriteJson(converter, value, writer, mustBeNested); return(stream.ToArray()); } }
public static byte[] Serialise(object obj) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); Newtonsoft.Json.Bson.BsonWriter bw = new Newtonsoft.Json.Bson.BsonWriter(ms); ser.Serialize(bw, obj); //IStateObj return(ms.GetBuffer()); }
private static byte[] SerializeWithJsonNetBson <T>(T obj, JsonNetJsonSerializer serializer) { using (var memStream = new MemoryStream()) { var writer = new JsonNetBsonWriter(memStream); serializer.Serialize(writer, obj); return(memStream.ToArray()); } }
static byte[] SerializeWithJsonNetBson <T>(T obj) { using (var memStream = new MemoryStream()) using (var writer = new Newtonsoft.Json.Bson.BsonWriter(memStream)) { var serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(writer, obj); writer.Flush(); return(memStream.ToArray()); } }
public static byte[] SerializeJsonNetBson <T>(T obj) { using (var ms = new MemoryStream()) { var serializer = new Newtonsoft.Json.JsonSerializer(); var writer = new Newtonsoft.Json.Bson.BsonWriter(ms); serializer.Serialize(writer, obj); var bytes = ms.ToArray(); return(bytes); } }
/// <summary> /// Converts an instance of an object to a string format /// </summary> /// <param name="format">Specifies if it should convert to XML, BSON or JSON</param> /// <returns>The object, converted to a string representation</returns> public string ToString(SerializationFormats format) { Encryption64 encryptor = new Encryption64(); List <DataLayer.FooEnterprises.Product.SerializableProduct> zs = new List <DataLayer.FooEnterprises.Product.SerializableProduct>(); foreach (Product z in this) { DataLayer.FooEnterprises.Product.SerializableProduct serializableProduct = new DataLayer.FooEnterprises.Product.SerializableProduct(); serializableProduct.ProductId = z.IsNull(Product.Fields.ProductId) ? (int?)null : z.ProductId; serializableProduct.ProductName = z.IsNull(Product.Fields.ProductName) ? null : z.ProductName; serializableProduct.WholesaleCost = z.IsNull(Product.Fields.WholesaleCost) ? (decimal?)null : z.WholesaleCost; serializableProduct.SaleCost = z.IsNull(Product.Fields.SaleCost) ? (decimal?)null : z.SaleCost; serializableProduct.SerializationIsUpdate = z.LayerGenIsUpdate(); serializableProduct.SerializationConnectionString = encryptor.Encrypt(z.LayerGenConnectionString(), DataLayer.FooEnterprises.Universal.LayerGenEncryptionKey); zs.Add(serializableProduct); } if (format == SerializationFormats.Json) { return(Newtonsoft.Json.JsonConvert.SerializeObject(zs)); } if (format == SerializationFormats.Xml) { System.Xml.Serialization.XmlSerializer xType = new System.Xml.Serialization.XmlSerializer(zs.GetType()); using (System.IO.StringWriter sw = new System.IO.StringWriter()) { xType.Serialize(sw, zs); return(sw.ToString()); } } if (format == SerializationFormats.BsonBase64) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(ms)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(writer, zs); } return(Convert.ToBase64String(ms.ToArray())); } } return(""); }
public static byte[] Pack(Newtonsoft.Json.Linq.JToken json) { using (var ms = new System.IO.MemoryStream()) { using (var bswrite = new Newtonsoft.Json.Bson.BsonWriter(ms)) { json.WriteTo(bswrite); var bytes = ms.ToArray(); return(bytes); } } }
private byte[] GetCompiledProgramV1() { using (var outputStream = new System.IO.MemoryStream()) { using (var bsonWriter = new Newtonsoft.Json.Bson.BsonWriter(outputStream)) { var s = new Newtonsoft.Json.JsonSerializer(); s.Serialize(bsonWriter, this.program); } return(outputStream.ToArray()); } }
public static byte[] SerializeBson <T>(T obj) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(ms)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(writer, obj); } return(ms.ToArray()); } }
/// <summary> /// Converts an instance of an object to a string format /// </summary> /// <param name="format">Specifies if it should convert to XML, BSON or JSON</param> /// <returns>The object, converted to a string representation</returns> public string ToString(SerializationFormats format) { Encryption64 encryptor = new Encryption64(); List <DataLayer.Customer.SerializableCustomer> zs = new List <DataLayer.Customer.SerializableCustomer>(); foreach (Customer z in this) { DataLayer.Customer.SerializableCustomer serializableCustomer = new DataLayer.Customer.SerializableCustomer(); serializableCustomer.CustomerId = z.IsNull(Customer.Fields.CustomerId) ? (int?)null : z.CustomerId; serializableCustomer.FirstName = z.IsNull(Customer.Fields.FirstName) ? null : z.FirstName; serializableCustomer.LastName = z.IsNull(Customer.Fields.LastName) ? null : z.LastName; serializableCustomer.SerializationIsUpdate = z.LayerGenIsUpdate(); serializableCustomer.SerializationConnectionString = encryptor.Encrypt(z.LayerGenConnectionString(), DataLayer.Universal.LayerGenEncryptionKey); zs.Add(serializableCustomer); } if (format == SerializationFormats.Json) { return(Newtonsoft.Json.JsonConvert.SerializeObject(zs)); } if (format == SerializationFormats.Xml) { System.Xml.Serialization.XmlSerializer xType = new System.Xml.Serialization.XmlSerializer(zs.GetType()); using (System.IO.StringWriter sw = new System.IO.StringWriter()) { xType.Serialize(sw, zs); return(sw.ToString()); } } if (format == SerializationFormats.BsonBase64) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(ms)) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); serializer.Serialize(writer, zs); } return(Convert.ToBase64String(ms.ToArray())); } } return(""); }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="graph"></param> public static byte[] SerializeBson(object graph) { try { using (var file = new System.IO.MemoryStream()) { Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer(); Newtonsoft.Json.Bson.BsonWriter writer = new Newtonsoft.Json.Bson.BsonWriter(file); serializer.Serialize(writer, graph); return(file.ToArray()); } } catch (Exception) { throw; } }
private void button1_Click(object sender, System.EventArgs e) { // X:\jsc.svn\examples\javascript\test\TestRoslynYieldReturn\TestRoslynYieldReturn\Application.cs // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201405/20150520 // https://github.com/scriptcs/scriptcs/pull/525 // how do we use bson writer? // https://sites.google.com/a/jsc-solutions.net/backlog/knowledge-base/2014/201405/20140519-1 // X:\opensource\github\Newtonsoft.Json\Src\Newtonsoft.Json\Bson var m = new MemoryStream(); var w = new BinaryWriter(m); var z = new Newtonsoft.Json.Bson.BsonWriter(w); z.WriteStartObject(); z.WritePropertyName("hello"); z.WriteValue("world"); z.Close(); // http://blogs.msdn.com/b/visualstudioalm/archive/2014/02/06/json-debugger-visualizer-in-visual-studio-2013.aspx //error at CopyType: //*Illegal one - byte branch at position: 43.Requested branch was: 130. //* Newtonsoft.Json.Converters.RegexConverter 020000c9 //* IllegalBranchAt 0000002b //* RequestedBranch 130 Debugger.Break(); }
protected byte[] SerializeUsingNewtonsoftWriter <T>(T value, bool mustBeNested = false) { using (var memoryStream = new MemoryStream()) using (var newtonsoftWriter = new Newtonsoft.Json.Bson.BsonWriter(memoryStream)) { if (mustBeNested) { newtonsoftWriter.WriteStartObject(); newtonsoftWriter.WritePropertyName("x"); } var newtonsoftSerializer = new Newtonsoft.Json.JsonSerializer(); newtonsoftSerializer.Serialize(newtonsoftWriter, value); if (mustBeNested) { newtonsoftWriter.WriteEndObject(); } return(memoryStream.ToArray()); } }
internal static byte[] ToBson <TValue>(TValue value) { byte[] buffer; using (MemoryStream ms = new MemoryStream()) { using (var writer = new Newtonsoft.Json.Bson.BsonWriter(ms)) { var serializer = new Newtonsoft.Json.JsonSerializer { Formatting = Newtonsoft.Json.Formatting.None, Culture = CultureInfo.InvariantCulture, TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple, }; serializer.Serialize(writer, value); } buffer = ms.ToArray(); } return(buffer); }