2016-09-26 80 views
0

我们有一个自定义页面类型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类的实际/传统的序列化?

回答

0

你需要提供更多的细节理想。 ProductProvider.GetProducts().FirstObject返回什么类型? A TreeNode?你想从序列化中得到什么数据?

我怀疑你的答案在于strongly-typed page type models in Kentico。使用这种方法,您可以完全控制页面模型的序列化方式,因为您可以使DocumentHelperTreeNodeProvider返回一个强类型模型,您可以使用Netonsoft的序列化属性来修饰它们。 DocumentHelper.GetDocuments<MyCustomProductModel>().First()

+1

在Kentico查询中使用LINQ是一种惊喜。先调用将实际加载内存中的所有对象,然后返回第一个结果。它不会限制从db返回的行数。 – Enn

+0

知道的非常有用。感谢您指出了这一点。 – getsetcode

2

您没有收到结果,因为ProductProvider.GetProducts()返回的Base对象没有任何自定义属性。 Newtonsoft适用于一类公共财产。你可以生成你自己的类,然后你可以用它来序列化以下this documentation

这里的最佳做法是创建你自己的模型,以便你不必污染JSON的所有属性,而是只使用你想要和需要的。

+0

事实上,返回的类型是'Product'(生成的类),我可以在代码正常的时候访问自定义属性,只是在序列化时它们不显示。我已更新我的问题以反映这一点。 不幸的是,几乎所有的属性都是必需的,所以一个新的模型会带来一些维护上的麻烦。 – jaypeagi

1

如果你试图从UI获取json对象,我建议使用REST服务来获取它,这将完成你所需要的一切。了解如何使用它here

+0

在将它作为JSON返回之前,我们实际上对获取的数据做了很多工作。 'Product'是返回对象的一小部分。 – jaypeagi

+1

在这种情况下,来自Enn的答案是要走的路。另外我想提一下,你可以实现Web API控制器,所以它会自动将对象序列化为JSON,并且它很好(如果不是最好的)在客户端上使用它。 –

1

在CMS.DataEngine命名空间中有一种扩展方法,称为ToJSON。如果您需要以与Kentico REST相同的方式序列化对象,请使用该方法。

如果您只需要选择对象的某些部分和/或对数据进行一些转换,最简单的方法是创建一个新的匿名对象并手动初始化其属性。