本文介绍了如何使ServiceStack v3符合jsonapi.org标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有ServiceStack v3后端的Ember.js前端。我遇到的问题是,Ember Data正在按照jsonapi.org标准期待JSON,如下所示:

  [{ 客户端:
[
{clientID:80,名称:测试客户端6,首字母缩略词:TCL6,网站:http://www.tcl6 .com},
{clientID:81,name:Test Client 7,acronym:TCL7,website:http://www.tcl7.com}
]
}]

但是ServiceStack将数据序​​列化为以下内容: / p>

  [{ClientID:80,Name:Test Client 6,Acronym:TCL6网站:http://www.tcl6.com},
{ClientID:81,名称:测试客户端7,首字母缩略词:TCL7,网站 http://www.tcl7.com}]

迫使我在前台实现自定义逻辑 - 将数据按摩到适当的格式。我想避免在客户端执行此转换,并将其直接实现到后端的响应中。



任何帮助或方向将不胜感激。 >

谢谢!

解决方案

您可以告诉ServiceStack的JSON序列化器发出 camelCase 属性名称:

  JsConfig.EmitCamelCaseNames = true; 

您还将使用与您要返回的JSON的形状相匹配的类型,例如:

  public class JsonApiClients 
{
public List< Client>客户{get;组; }
}


I am attempting to utilize an Ember.js front-end with a ServiceStack v3 backend. The issue I'm running into is that Ember Data is expecting JSON as per the jsonapi.org standards like so:

[{"clients":
    [
       {"clientID":80,"name":"Test Client 6","acronym":"TCL6","website":"http://www.tcl6.com"},  
       {"clientID":81,"name":"Test Client 7","acronym":"TCL7","website":"http://www.tcl7.com"}
    ]
}] 

But ServiceStack serializes the data into the following:

[{"ClientID":80,"Name":"Test Client 6","Acronym":"TCL6","Website":"http://www.tcl6.com"},
 {"ClientID":81,"Name":"Test Client 7","Acronym":"TCL7","Website":"http://www.tcl7.com"}]

Forcing me to implement custom logic on the front-end to "massage" the data into the appropriate format. I would like to avoid performing this conversion on the client side and implement it directly into the back-end's responses.

Any assistance or direction would be greatly appreciated.

Thanks!

解决方案

You can tell ServiceStack's JSON Serializer to emit camelCase property names with:

JsConfig.EmitCamelCaseNames = true;

You will also want to use a type that matches the shape of the JSON you want to return, e.g:

public class JsonApiClients
{
    public List<Client> Clients { get; set; }
}

这篇关于如何使ServiceStack v3符合jsonapi.org标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 06:14