using System.IO; using System.Runtime.Serialization.Formatters.Binary; BinaryFormatter formatter = new BinaryFormatter(); MyObject obj = new MyObject(); obj.Name = "John"; obj.Age = 30; obj.Email = "[email protected]"; using (FileStream stream = new FileStream("object.bin", FileMode.Create)) { GenericWriter writer = new GenericWriter(stream); writer.Write(formatter.Serialize(stream, obj)); }
using System.Net; using System.Text; string jsonData = "{ \"name\": \"John\", \"age\": 30, \"email\": \"[email protected]\" }"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/api/user"); request.Method = "POST"; request.ContentType = "application/json"; using (Stream stream = request.GetRequestStream()) { byte[] data = Encoding.UTF8.GetBytes(jsonData); GenericWriter writer = new GenericWriter(stream); writer.Write(data); }In this example, we create a JSON string and then use GenericWriter to write the data to the request stream for a web API call. Package/library: System.IO.Abstractions (version 2.1.0)