我们有一个自定义页面类型Product
,当它序列化为JSON(使用Newtonsoft)时,将不同的字段输出到Product
上的属性。Kentico - JSON序列化自定义页面类型返回不同的数据
Product product = ProductProvider.GetProducts().FirstObject;
var json = JsonConvert.SerializeObject(product);
生成以下JSON:
{
"LogSynchronization": 3,
"ObjectType": "cms.document.apollo.product",
"UpdateTimeStamp": true,
"LastUpdated": "2016-09-26T16:23:58.9228865Z",
"Locked": false,
"DataClass": null,
"UpdateVersionGUID": true,
"Components": [
{
"LogSynchronization": 3,
"ObjectType": "cms.node",
"UpdateTimeStamp": true,
"LastUpdated": "2016-09-26T16:23:58.9238891Z",
"Locked": false,
"DataClass": {
"Locked": false,
"IDWasChanged": false,
"Data": [
285,
...
false
],
"OriginalData": null,
"ClassName": "CMS.Tree"
},
"UpdateVersionGUID": true
},
{ ... }
]
}
自动生成Product.cs
:
//--------------------------------------------------------------------------------------------------
// <auto-generated>
//
// This code was generated by code generator tool.
//
// To customize the code use your own partial class. For more info about how to use and customize
// the generated code see the documentation at http://docs.kentico.com.
//
// </auto-generated>
//--------------------------------------------------------------------------------------------------
// ...
public partial class Product : SKUTreeNode
{
#region "Constants and variables"
/// <summary>
/// The name of the data class.
/// </summary>
public const string CLASS_NAME = "MyProject.Product";
/// <summary>
/// The instance of the class that provides extended API for working with Product fields.
/// </summary>
private readonly ProductFields mFields;
#endregion
#region "Properties"
/// <summary>
/// ProductID.
/// </summary>
[DatabaseIDField]
public int ProductID
{
get
{
return ValidationHelper.GetInteger(GetValue("ProductID"), 0);
}
set
{
SetValue("ProductID", value);
}
}
/// <summary>
/// My Custom Property.
/// </summary>
[DatabaseField]
public string MyCustomProperty
{
get
{
return ValidationHelper.GetString(GetValue("MyCustomProperty"), 0);
}
set
{
SetValue("MyCustomProperty", value);
}
}
// Truncated. Lots more properties here...
#endregion
#region "Constructors"
/// <summary>
/// Initializes a new instance of the <see cref="Product" /> class.
/// </summary>
public Product() : base(CLASS_NAME)
{
mFields = new ProductFields(this);
}
#endregion
}
我怎样才能让这个返回Product
类的实际/传统的序列化?
在Kentico查询中使用LINQ是一种惊喜。先调用将实际加载内存中的所有对象,然后返回第一个结果。它不会限制从db返回的行数。 – Enn
知道的非常有用。感谢您指出了这一点。 – getsetcode