Json 使用反序列化fn在服务堆栈中自定义序列化
在servicestack中,我试图处理一个webhook,它将以下JSON主体发送到服务堆栈端点:Json 使用反序列化fn在服务堆栈中自定义序列化,json,
servicestack,Json,
servicestack,在servicestack中,我试图处理一个webhook,它将以下JSON主体发送到服务堆栈端点: { "action": "actionType1", "api_version": "1.00", "data": { "id": "a8d316b8-10a7-4440-a836-9bd354f656db", //VARIABLE other properties / structure } } 我正在尝试将其映射到以下请求对象: [Route("/pub
{
"action": "actionType1",
"api_version": "1.00",
"data": {
"id": "a8d316b8-10a7-4440-a836-9bd354f656db",
//VARIABLE other properties / structure
}
}
我正在尝试将其映射到以下请求对象:
[Route("/public/Webhookhandler", HttpVerbs.Post)]
public class MyCustomRequst
{
public string action { get; set; }
public string api_version { get; set; }
public string data { get; set; } //Will be the remaining JSON
}
但是,当服务堆栈框架处理这个问题时,“data”中的值是JSON正文的正确部分,但是删除了所有引号,因此它不再有效
我已尝试使用以下内容覆盖整个请求对象的序列化:
JsConfig<MyCustomRequst>.DeSerializeFn = DeserializeMyRequestDto;
public MyCustomRequst DeserializeMyRequestDto(string rawBody)
{
var result = rawBody.FromJson<MyCustomRequst>();
return result
}
我做错什么了吗?我不确定我是否正在尝试让服务堆栈做一些它不打算做的事情,或者我是否遗漏了一些可以让它工作的东西
任何帮助都将不胜感激:-)您的DTO应该与JSON的形状紧密匹配,如果它始终是平面对象,您可以使用字符串字典,例如:
[Route("/public/Webhookhandler", HttpVerbs.Post)]
public class MyCustomRequst
{
public string action { get; set; }
public string api_version { get; set; }
public Dictionary<string,string> data { get; set; }
}
[路由(“/public/Webhookhandler”,HttpVerbs.Post)]
公共类MyCustomRequst
{
公共字符串操作{get;set;}
公共字符串api_版本{get;set;}
公共字典数据{get;set;}
}
如果是更嵌套的对象结构,您可以使用
JsonObject
来实现更灵活的API动态解析。这个问题对于问题是什么、导致问题的原因以及您期望的答案都非常不清楚。我会考虑完全改写它来显示导致问题的源代码、最终结果以及你尝试过的内容。请明确说明如何获得无效的JSON,因为这是您尝试解决的第一件事,因为JSON序列化程序需要有效的JSON。在提问时,关注源代码,目标是再现性,而不是多余的描述,这只会影响识别问题和首选解决方案。我感谢您的评论和时间,我已经尽力让它更清楚。感谢Mythz,这正是我们所需要的,并解决了问题。我不知道JSONObject,所以一定是找错地方了!
[Route("/public/Webhookhandler", HttpVerbs.Post)]
public class MyCustomRequst
{
public string action { get; set; }
public string api_version { get; set; }
public Dictionary<string,string> data { get; set; }
}