猿问

自定义序列化程序设置

我正在创建一个 AWS Lambda 函数,它将接受 JSON 有效负载并对其进行处理。通过 C# SDK,他们提供了一个基于 Newtonsoft.Json 的序列化程序。


[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

我需要为日期指定自定义格式,以便可以将事物正确反序列化为 .NET 类等。


在 Newtonsoft.Json 中,我可以像这样定义自定义设置:


new JsonSerializerSettings()

{

    DateFormatString = "yyyyMMdd",

    Formatting = Formatting.Indented,

    NullValueHandling = NullValueHandling.Ignore

};

我在文档中找不到任何地方,也找不到如何通过 Amazon 实现来完成。有没有人定制过 LambdaSerializer?


翻阅古今
浏览 116回答 1
1回答

杨魅力

这是一个简单的例子:using System;using System.Collections.Generic;using System.IO;using Amazon.Lambda.Core;namespace MySerializer{&nbsp; &nbsp; public class LambdaSerializer : ILambdaSerializer&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public LambdaSerializer()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public LambdaSerializer(IEnumerable<Newtonsoft.Json.JsonConverter> converters) : this()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotSupportedException("Custom serializer with converters not supported.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; string GetString(Stream s)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] ba = new byte[s.Length];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int iPos = 0; iPos < ba.Length; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iPos += s.Read(ba, iPos, ba.Length - iPos);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string result = System.Text.ASCIIEncoding.ASCII.GetString(ba);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public T Deserialize<T>(Stream requestStream)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string json = GetString(requestStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Note: you could just pass the stream into the deserializer if it will accept it and dispense with GetString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; T obj = // Your deserialization here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return obj;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Serialize<T>(T response, Stream responseStream)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string json = "Your JSON here";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamWriter writer = new StreamWriter(responseStream);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.Write(json);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.Flush();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } // public class LambdaSerializer}在您的 lambda 函数中,您将拥有以下内容:[assembly: LambdaSerializer(typeof(MySerializer.LambdaSerializer))]namespace MyNamespace{&nbsp; &nbsp;public MyReturnObject FunctionHandler(MyInObject p, ILambdaContext context)&nbsp; &nbsp;{&nbsp; &nbsp;}请注意,显式实现接口不起作用:void ILambdaContext.Serialize<T>(T response, Stream responseStream){&nbsp; &nbsp;// won't work不要问我为什么。我的猜测是 AWS 创建对象并且不将其转换为接口但期望公共方法。您实际上可以在那里找到序列化程序源代码,但我目前找不到。如果我遇到它,我会编辑这篇文章。根据我的经验,只使用了默认 ctor,但为了安全起见,您可能应该将其默认转换器添加到您的序列化程序中。我现在不打扰,不过还好。希望这可以帮助。
随时随地看视频慕课网APP
我要回答