使用Iserialazable接口以json格式序列化对象

时间:2018-12-19 07:18:52

标签: json serialization

我需要将证书对象序列化为JSON格式,以下代码用于二进制转换。

static void Main()
{
    string fileName = "Certificate.json";
    IFormatter formatter = new BinaryFormatter();

    Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
    Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
    Console.WriteLine("Done");

}
public static void SerializeItem(string fileName, IFormatter formatter)
{
    string Certificate = (@"\Certificate.cer");
    // Create an instance of the type and serialize it.
    X509Certificate2 x509 = new X509Certificate2(Certificate);
    byte[] rawData = x509.RawData;
    x509.Import(rawData);
    FileStream s = new FileStream(fileName, FileMode.Create);
    formatter.Serialize(s, x509);
    s.Close();
}

0 个答案:

没有答案