c# - 无法将 Json 反序列化为类型,无法找到构造函数
问题描述
我在反序列化发送到 azure 函数的 JSON 时遇到了一些问题。首先,我打算将一组带有 post 的 Ciphertext 类型发送到 azure,反序列化 JSON 以恢复我的数据,然后对该数据进行操作。我的类如下所示被调用,它有一个typesampleClass
属性:ciphertext
Ciphertext
[DataContract]
public class sampleClass
{
[DataMember]
public Ciphertext ciphertext { get; set; }
[JsonConstructor]
public sampleClass() { }
}
这是我试图序列化/反序列化的类。
要发布我使用 HttpClient 的数据,我将其发布为 JSON,如下所示:
HttpResponseMessage response = await client.PostAsJsonAsync("api/Function1", cipher);
在我的天蓝色函数中,我试图将 Json 作为流读取并将其反序列化为 sampleClass [],但是这给了我一个错误。
//Receive data from The Http PostRequest.
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
//De serialises to an object.
sampleClass[] array = JsonConvert.DeserializeObject<sampleClass[]>(requestBody);
抛出的错误如下所示:
执行“Function1”(失败,Id=1be7633e-6b6a-4626-98b7-8fec98eac633)[11/02/2020 15:50:48] System.Private.CoreLib:执行函数时出现异常:Function1。Newtonsoft.Json:找不到用于 Microsoft.Research.SEAL.Ciphertext 类型的构造函数。一个类应该有一个默认构造函数、一个带参数的构造函数或一个标有 JsonConstructor 属性的构造函数。路径“[0].ciphertext.CoeffModCount”,第 1 行,位置 32。
当我尝试反序列化我的 JSON 时抛出此错误,我该如何解决这个问题?
解决方案
你在这里有几个问题。让我们按顺序排列它们。
首先,该类型既没有无参数构造函数,也没有单参数构造函数,从参考源Microsoft.Research.SEAL.Ciphertext
可以看出:
public class Ciphertext : NativeObject
{
public Ciphertext(MemoryPoolHandle pool = null)
{
// Contents omitted
}
public Ciphertext(SEALContext context, MemoryPoolHandle pool = null)
{
// Contents omitted
}
// Additional constructors, methods and members omitted.
第一个构造函数的参数是可选的,但这并不意味着它是无参数的,它只是意味着编译器在代码中不存在该值时提供该值。但是当通过反射调用构造函数时(这是 Json.NET 所做的),仍然需要提供一个值;有关详细信息,请参阅反射 - 使用参数调用构造函数。这种类型缺少真正的无参数构造函数是导致Newtonsoft.Json:无法找到用于 Microsoft.Research.SEAL.Ciphertext 类型的构造函数的原因。要抛出的异常。
(在评论中指出您的问题是sampleClass
缺少默认构造函数,但该评论是错误的。)
由于您无法修改Ciphertext
提供自己的构造方法的标准方法,因此请CustomCreationConverter<T>
像这样使用:
public class CiphertextConverter : CustomCreationConverter<Ciphertext>
{
public override Ciphertext Create(Type objectType)
{
return new Ciphertext(); // Use the default value for the optional parameter
}
}
然后做:
var settings = new JsonSerializerSettings
{
Converters = { new CiphertextConverter() },
};
var array = JsonConvert.DeserializeObject<sampleClass []>(requestBody, settings);
但是,这不起作用,这是您的下一个问题。由于 的大多数公共属性Ciphertext
都是只读的,因此无法从它们中反序列化类型。
在这里失败的演示小提琴 #1 。
那么该怎么办?事实证明,Ciphertext
有两种方法
public long Save(Stream stream, ComprModeType? comprMode = null)
public long Load(SEALContext context, Stream stream)
这些似乎允许我们将 a 序列化为 a Ciphertext
,MemoryStream
然后使用如下转换器将内容作为 Base64 字符串插入 JSON:
public class CiphertextConverter : JsonConverter<Ciphertext>
{
readonly SEALContext context;
public CiphertextConverter(SEALContext context) => this.context = context ?? throw new ArgumentNullException(nameof(context));
public override Ciphertext ReadJson(JsonReader reader, Type objectType, Ciphertext existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var data = serializer.Deserialize<byte []>(reader);
if (data == null)
return null;
var cipherText = new Ciphertext();
using (var stream = new MemoryStream(data))
cipherText.Load(context, stream);
return cipherText;
}
public override void WriteJson(JsonWriter writer, Ciphertext value, JsonSerializer serializer)
{
using (var stream = new MemoryStream())
{
value.Save(stream, ComprModeType.Deflate); // TODO: test to see whether Deflate gives better size vs speed performance in practice.
writer.WriteValue(stream.ToArray());
}
}
}
然后在序列化和反序列化时使用转换器如下:
var settings = new JsonSerializerSettings
{
Converters = { new CiphertextConverter(GlobalContext.Context) },
};
var array = JsonConvert.DeserializeObject<sampleClass []>(requestBody, settings);
但是等等——这个对象是GlobalContext.Context
什么?这给您带来了第三个问题,即您需要SEALContext
客户端和服务器端的兼容对象Ciphertext
通过序列化在两者之间传递。现在,查看Cloud Functions Demo演示应用程序,这似乎是一个正确的假设,因为该应用程序在客户端和服务器端确实具有兼容的上下文:
- 客户端:https ://github.com/microsoft/SEAL-Demo/blob/master/CloudFunctionsDemo/ClientBasedFunctions/ClientBasedFunctions/GlobalProperties.cs
- 服务器端:https ://github.com/microsoft/SEAL-Demo/blob/master/CloudFunctionsDemo/CloudBasedFunctions/CloudBasedFunctions/GlobalProperties.cs
所以我假设你也这样做。既然你这样做了,上面的转换器应该用于序列化和反序列化。
出于测试目的,我调整了来自https://github.com/microsoft/SEAL/tree/master/dotnet/testsCiphertextTests.SaveLoadTest()
的测试方法和类,以创建以下测试工具:GlobalContext
public class TestClass
{
[TestMethod]
public void JsonNetSaveLoadTest()
{
Debug.WriteLine("Testing Json.NET");
Func<Ciphertext, SEALContext, Ciphertext> roundtrip = (cipher, context) =>
{
var clientArray = new [] { new sampleClass { ciphertext = cipher } };
var settings = new JsonSerializerSettings
{
Converters = { new CiphertextConverter(GlobalContext.Context) },
};
var requestBody = JsonConvert.SerializeObject(clientArray, settings);
Debug.Write(" ");
Debug.WriteLine(requestBody);
Debug.WriteLine(" requestBody.Length={0}", requestBody.Length);
var array = JsonConvert.DeserializeObject<sampleClass []>(requestBody, settings);
Assert.IsTrue(array.Length == clientArray.Length);
var reserializedJson = JsonConvert.SerializeObject(array, settings);
Debug.Write(" ");
Debug.WriteLine(reserializedJson);
Assert.IsTrue(requestBody == reserializedJson);
return array[0].ciphertext;
};
SaveLoadTest(roundtrip);
Console.WriteLine(" passed.");
}
// Adapted from https://github.com/microsoft/SEAL/blob/master/dotnet/tests/CiphertextTests.cs#L113
[TestMethod]
public void DirectSaveLoadTest()
{
Debug.WriteLine("Testing direct save and load:");
Func<Ciphertext, SEALContext, Ciphertext> roundtrip = (cipher, context) =>
{
Ciphertext loaded = new Ciphertext();
Assert.AreEqual(0ul, loaded.Size);
Assert.AreEqual(0ul, loaded.PolyModulusDegree);
Assert.AreEqual(0ul, loaded.CoeffModCount);
using (MemoryStream mem = new MemoryStream())
{
cipher.Save(mem);
mem.Seek(offset: 0, loc: SeekOrigin.Begin);
loaded.Load(context, mem);
}
return loaded;
};
SaveLoadTest(roundtrip);
Debug.WriteLine(" passed.");
}
// Adapted from https://github.com/microsoft/SEAL/blob/master/dotnet/tests/CiphertextTests.cs#L113
static void SaveLoadTest(Func<Ciphertext, SEALContext, Ciphertext> roundtrip)
{
SEALContext context = GlobalContext.Context;
KeyGenerator keygen = new KeyGenerator(context);
Encryptor encryptor = new Encryptor(context, keygen.PublicKey);
Plaintext plain = new Plaintext("2x^3 + 4x^2 + 5x^1 + 6");
Ciphertext cipher = new Ciphertext();
encryptor.Encrypt(plain, cipher);
Assert.AreEqual(2ul, cipher.Size);
Assert.AreEqual(8192ul, cipher.PolyModulusDegree);
Assert.AreEqual(4ul, cipher.CoeffModCount);
var loaded = roundtrip(cipher, context);
Assert.AreEqual(2ul, loaded.Size);
Assert.AreEqual(8192ul, loaded.PolyModulusDegree);
Assert.AreEqual(4ul, loaded.CoeffModCount);
Assert.IsTrue(ValCheck.IsValidFor(loaded, context));
ulong ulongCount = cipher.Size * cipher.PolyModulusDegree * cipher.CoeffModCount;
for (ulong i = 0; i < ulongCount; i++)
{
Assert.AreEqual(cipher[i], loaded[i]);
}
}
}
static class GlobalContext
{
// Copied from https://github.com/microsoft/SEAL/blob/master/dotnet/tests/GlobalContext.cs
static GlobalContext()
{
EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV)
{
PolyModulusDegree = 8192,
CoeffModulus = CoeffModulus.BFVDefault(polyModulusDegree: 8192)
};
encParams.SetPlainModulus(65537ul);
BFVContext = new SEALContext(encParams);
encParams = new EncryptionParameters(SchemeType.CKKS)
{
PolyModulusDegree = 8192,
CoeffModulus = CoeffModulus.BFVDefault(polyModulusDegree: 8192)
};
CKKSContext = new SEALContext(encParams);
}
public static SEALContext BFVContext { get; private set; } = null;
public static SEALContext CKKSContext { get; private set; } = null;
public static SEALContext Context => BFVContext;
}
工作演示小提琴#2 here。
笔记:
只要是公共的,就不需要
sampleClass
用 with标记无参构造函数[JsonConstructor]
。通过测试,生成的 Base64 字符串
Ciphertext
似乎很长,每个 .5 MB 大约为 .5 MBCiphertext
。因为 Json.NET 在解析过程中完全实现了每个字符串,所以在处理如此巨大的字符串时效率并不高。如果超过最大有效字符串长度或遇到大对象堆碎片,您将需要重新评估您的架构。
免责声明
我不是安全专业人士。我无法告诉您通过网络发送序列化文件是否Ciphertext
会泄漏信息。我也不能建议您如何SEALContext
为您的应用程序选择合适的 - 甚至在客户端和服务器端具有兼容的上下文是否可能会泄漏信息。这个答案只解释了如何通过 Json.NET 序列化特定的 SEAL 对象。
推荐阅读
- imagemagick - 如何使用 Node GraphicsMagick/ImageMagick 获取 gif 帧
- linux - 如何将儒略日期转换为公历日期并使用 Shell 脚本从中减去几天
- python - 如何在 Python 中直接在 s3 中的文件中写入 JSON?
- python - 在使用客户端 Boto3 SDK 时获取不带父文件夹前缀的确切文件名
- python - 如何修复 RuntimeWarning:协程
- ibm-midrange - DSPUSRPRF UPCHGD 字段
- python - 关于根据Flask_restx的API对象声明位置路由函数的问题
- ethereum - 我们有两种方式来启动一个 web3 实例,我们选择哪一种更好呢?
- python - 如何在 Python 3.8 的 dbf 文件中写入/附加列?
- algorithm - 在锦标赛排序算法中排序八个数字的最少比较时间是多少?(考虑坏情况)