- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个向用户公开的 Web API。
每个用户都有一个管理员,他也是一个用户。
在我的 EDMX 中,我在“用户”实体上有一个 self 1..* 导航属性,从经理到用户的导航是“合作者”,从用户到他的导航是“经理”经理。
我的 JSON API 使用 NewtonJSON 序列化实体。
为了自定义 API 调用的 JSON 结果,我在查询字符串中实现了关键字“fields”。使用“字段”可以获取用户的部分 JSON 表示形式。
调用如下所示:/api/users?fields=id,name,department,picture
并且您从完整的 C# 用户对象中获得仅包含 id、名称、部门和图片属性的 JSON。
我已经使用 IContractResolver 的自定义实现实现了这一点。
问题是 NewtonJSON 契约(Contract)解析器是“按类型”而不是“按对象”工作的,这意味着您可以告诉序列化程序序列化声明类型的此类成员而不是另一个,但您不能告诉它(据我所知,这就是我在这里问的原因)序列化该对象的此类成员,而不是同一类型的另一个对象上的同一成员。
也就是说,我的问题是当我要求:/api/users?fields=id,name,manager
我在每个用户对象上收到了对经理成员进行递归序列化的响应,如下所示:
[{
id: 123,
name: "Foo",
manager:
{
id: 124,
name: "Foo",
manager:
{
id: 125,
name: "Foo",
manager:
{
...
}
}
}
},
{
id: 124,
name: "Foo",
manager:
{
id: 125,
name: "Foo",
manager:
{
...
}
}
},
{
id: 125,
name: "Foo",
manager:
{
...
}
}]
我还实现了请求部分子实体响应的能力,如下所示:
/api/users?fields=id,name,manager.id
但由于主对象(这里是用户)和子对象(经理)都是同一类型,所以它不起作用。
有没有人已经使用 NewtonJSON 来实现部分响应 Web API?您如何设法使用嵌入式 self Type 实体?
谢谢你的建议。
最佳答案
您可以编写一个 Action 过滤器来理解字段参数并转换响应。我有一个将响应对象转换为字典的示例实现。键是客户端请求的字段,值是响应中返回的对象的字段值。实现是一个示例,还远未完成:)
public class FieldFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var fieldsParameter = actionExecutedContext.Request.GetQueryNameValuePairs().Where(kvp => kvp.Key == "fields");
if (fieldsParameter.Count() == 1)
{
object response;
object newResponse;
if (actionExecutedContext.Response.TryGetContentValue(out response))
{
string[] fields = fieldsParameter.First().Value.Split(',');
IEnumerable<object> collection = response as IEnumerable<object>;
if (collection != null)
{
newResponse = collection.Select(o => SelectFields(fields, o));
}
else
{
newResponse = SelectFields(fields, response);
}
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.OK, newResponse);
}
}
}
private static Dictionary<string, object> SelectFields(string[] fields, object value)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (string field in fields)
{
result.Add(field, GetValue(field, value));
}
return result;
}
private static object GetValue(string indexer, object value)
{
string[] indexers = indexer.Split('.');
foreach (string property in indexers)
{
PropertyInfo propertyInfo = value.GetType().GetProperty(property);
if (propertyInfo == null)
{
throw new Exception(String.Format("property '{0}' not found on type '{1}'", property, value.GetType()));
}
value = propertyInfo.GetValue(value);
if (value == null)
{
return null;
}
}
return value;
}
}
关于c# - 自引用导航属性和 json 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777084/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!