csharp Custome deserializer

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Custome deserializer相关的知识,希望对你有一定的参考价值。

  public class PoseController : IPoseController
    {
        public async Task<Dictionary<string, IPose>> GetSubjectImagesExternal(string subjectID) {

            string serialized = "0";
            var url = "https://api.edgeimaging.ca:940/index.php/api/get_subject_image_info/";
            Logger.Log.Debug(url);

            var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("GUID", subjectID)
            });

            try
            {
                
                HttpResponseMessage response = await new HttpClient().PostAsync(url, formContent);
                if (response.IsSuccessStatusCode)
                {
                    serialized = await response.Content.ReadAsStringAsync();
                }
                else
                {
                    Logger.Log.Error(response);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.Error(ex);
            }
            
            var deserializeResponse = JsonConvert.DeserializeObject<Dictionary<string, Pose>>(serialized, new PoseDeserializer());
            var result = deserializeResponse.Select(d => new KeyValuePair<String, IPose>(d.Key, d.Value)).ToDictionary(k => k.Key, k => k.Value);

            result["Message"] = null;

            return result;
        }
    }
    
    -----------------------------------EDge.Model------------------------------------------------
    
     public class PoseDeserializer : JsonConverter
    {

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException("Not implemented yet");
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return new Pose();
            }
            else if (reader.TokenType == JsonToken.String)
            {
                if ((string)reader.Value != "success")
                {
                    //throw new Exception();
                }

                return new Pose();
            }
            else
            {
                try
                {
                    JObject obj = JObject.Load(reader);
                    var newPose = new Pose();

                    newPose.SiteID = obj["SiteID"].ToString().Trim();
                    newPose.AssocID = obj["AssocID"].ToString().Trim();
                    newPose.JobID = obj["JobID"].ToString().Trim();
                    newPose.EventID = obj["EventID"].ToString().Trim();
                    newPose.Title = obj["Title"].ToString().Trim();
                    newPose.onlineVersion = obj["onlineVersion"].ToString().Trim();
                    newPose.idBackgroundFamily = obj["idBackgroundFamily"].ToString().Trim();
                    newPose.Priceplan = obj["Priceplan"].ToString().Trim();
                    newPose.SubjectID = obj["SubjectID"].ToString().Trim();
                    newPose.FirstName = obj["FirstName"].ToString().Trim();
                    newPose.LastName = obj["LastName"].ToString().Trim();
                    newPose.Grade = obj["Grade"].ToString().Trim();
                    newPose.Address1 = obj["Address1"].ToString().Trim();
                    newPose.Address2 = obj["Address2"].ToString().Trim();
                    newPose.City = obj["City"].ToString().Trim();
                    newPose.Province = obj["Province"].ToString().Trim();
                    newPose.PostalCode = obj["PostalCode"].ToString().Trim();
                    newPose.HomePhone = obj["HomePhone"].ToString().Trim();
                    newPose.Email = obj["Email"].ToString().Trim();
                    newPose.ImageID = obj["ImageID"].ToString().Trim();
                    newPose.ImageNum = obj["ImageNum"].ToString().Trim();
                    newPose.RotateDegrees = obj["RotateDegrees"].ToString().Trim();
                    newPose.fileExtension = obj["fileExtension"].ToString().Trim();
                    newPose.Med = obj["Med"].ToString().Trim();

                    return newPose;
                }
                catch
                {
                    return string.Empty;
                }
   
            }
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return false;
        }
    }
}
-------------------------------------------------na Model klasi dekorator-----------------------------------------------

[JsonConverter(typeof(PoseDeserializer))]
    public class Pose : IPose
    {
        public string SiteID { get; set; }
        public string AssocID { get; set; }
        public string JobID { get; set; }
        public string EventID { get; set; }
        public string Title { get; set; }
        public string onlineVersion { get; set; }
        public string idBackgroundFamily { get; set; }
        public string Priceplan { get; set; }
        public string SubjectID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Grade { get; set; }
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public string Province { get; set; }
        public string PostalCode { get; set; }
        public string HomePhone { get; set; }
        public string Email { get; set; }
        public string ImageID { get; set; }
        public string ImageNum { get; set; }
        public string RotateDegrees { get; set; }
        public string fileExtension { get; set; }
        public string Med { get; set; }

        //public string Low { get; set; }
    }

以上是关于csharp Custome deserializer的主要内容,如果未能解决你的问题,请参考以下文章

当动态生成javascript对象的密钥时,gulp会中断。

JsonConvert 无法反序列化整数

magento更改实体类

mysql 内连接外连接自连接

sql 查询多表插入

Vue事件总线实例(全局事件)