第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

自定義序列化程序設(shè)置

自定義序列化程序設(shè)置

C#
翻閱古今 2022-07-23 17:13:57
我正在創(chuàng)建一個(gè) AWS Lambda 函數(shù),它將接受 JSON 有效負(fù)載并對(duì)其進(jìn)行處理。通過(guò) C# SDK,他們提供了一個(gè)基于 Newtonsoft.Json 的序列化程序。[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]我需要為日期指定自定義格式,以便可以將事物正確反序列化為 .NET 類(lèi)等。在 Newtonsoft.Json 中,我可以像這樣定義自定義設(shè)置:new JsonSerializerSettings(){    DateFormatString = "yyyyMMdd",    Formatting = Formatting.Indented,    NullValueHandling = NullValueHandling.Ignore};我在文檔中找不到任何地方,也找不到如何通過(guò) Amazon 實(shí)現(xiàn)來(lái)完成。有沒(méi)有人定制過(guò) LambdaSerializer?
查看完整描述

1 回答

?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是一個(gè)簡(jiǎn)單的例子:


using System;

using System.Collections.Generic;

using System.IO;


using Amazon.Lambda.Core;


namespace MySerializer

{

    public class LambdaSerializer : ILambdaSerializer

    {


        public LambdaSerializer()

        {

        }



        public LambdaSerializer(IEnumerable<Newtonsoft.Json.JsonConverter> converters) : this()

        {

            throw new NotSupportedException("Custom serializer with converters not supported.");

        }



        string GetString(Stream s)

        {

            byte[] ba = new byte[s.Length];


            for (int iPos = 0; iPos < ba.Length; )

            {

                iPos += s.Read(ba, iPos, ba.Length - iPos);

            }


            string result = System.Text.ASCIIEncoding.ASCII.GetString(ba);

            return result;

        }



        public T Deserialize<T>(Stream requestStream)

        {

            string json = GetString(requestStream);

            // Note: you could just pass the stream into the deserializer if it will accept it and dispense with GetString()

            T obj = // Your deserialization here

            return obj;

        }



        public void Serialize<T>(T response, Stream responseStream)

        {

            string json = "Your JSON here";

            StreamWriter writer = new StreamWriter(responseStream);

            writer.Write(json);

            writer.Flush();

        }


    } // public class LambdaSerializer


}

在您的 lambda 函數(shù)中,您將擁有以下內(nèi)容:


[assembly: LambdaSerializer(typeof(MySerializer.LambdaSerializer))]


namespace MyNamespace

{


   public MyReturnObject FunctionHandler(MyInObject p, ILambdaContext context)

   {


   }

請(qǐng)注意,顯式實(shí)現(xiàn)接口不起作用:


void ILambdaContext.Serialize<T>(T response, Stream responseStream)

{

   // won't work

不要問(wèn)我為什么。我的猜測(cè)是 AWS 創(chuàng)建對(duì)象并且不將其轉(zhuǎn)換為接口但期望公共方法。


您實(shí)際上可以在那里找到序列化程序源代碼,但我目前找不到。如果我遇到它,我會(huì)編輯這篇文章。


根據(jù)我的經(jīng)驗(yàn),只使用了默認(rèn) ctor,但為了安全起見(jiàn),您可能應(yīng)該將其默認(rèn)轉(zhuǎn)換器添加到您的序列化程序中。我現(xiàn)在不打擾,不過(guò)還好。


希望這可以幫助。


查看完整回答
反對(duì) 回復(fù) 2022-07-23
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)