ServiceStack.Text JsConfig.IncludePublicFields = true doesn't work with DataContracts - c#

I set ServiceStack.Text.JsConfig.IncludePublicFields = true; in AppHost.Configure but public fields are still not deserializing in JSON format.
Here is a simplified example:
[DataContract(Name = "RspItems")]
public class RspItems<T1>
{
[DataMember]
public int ItemCount { get { return Items == null ? 0 : Items.Count; } set { } }
[DataMember]
public IList<T1> Items;
public void SetItems(T1 item)
{
if (item != null)
{
Items = new List<T1>(1);
Items.Add(item);
}
}
public void SetItems(IList<T1> items)
{
Items = items;
}
}
[DataContract(Name="UserInfo")]
public class UserInfo
{
[DataMember]
public int UserID;
[DataMember]
public string LoginName;
[DataMember]
public string FirstName;
[DataMember]
public string LastName;
[DataMember]
public string Comment;
}
public class UserInfoReq<T> : IReturn<RspItems<T>>
{
public int? UserID { get; set; }
}
[Route("/app/user/list", "GET")]
public class UserInfoGetReq : UserInfoReq<UserInfo> { }
public class UserList : Service
{
public RspItems<UserInfo> Get(UserInfoGetReq req)
{
RspItems<UserInfo> rsp = new RspItems<UserInfo>();
UserInfo u = new UserInfo();
u.UserID = 3418;
u.LoginName = "jsmith";
u.FirstName = "John";
u.LastName = "Smith";
u.Comment = req.UserID.HasValue ? req.UserID.ToString() : "NULL";
rsp.SetItems(u);
return rsp;
}
}
The above example does not deserialize the response object in JSON format, although it works in XML format.
But inexplicably, if I remove the DataContract attributes on the classes, JSON format works.
Is this a bug?
Also, in the request DTO above, if I make UserID a simple public field (instead of being a property), then it is not deserialized from the querystring. Why?
Why does ServiceStack not include public fields by default anyway? Aren't DTOs supposed to be just "flat" structures used as a container for serializing/deserializing inputs and outputs (ie: the simpler, the better)? Public fields are also smaller and faster (no generated hidden private fields and getter/setter functions).
Note: Please don't suggest to just turn all public fields into properties because there are already tons of serialization structures that exist in the project. With many coming from different teams.

Related

How to use abstract class and Interface in hiding base class properties?

I have created a base class as "Common" there are many properties such as pageno,pagesize,search,etc which will use in all classes for entire project(must require).
There is other class as "Area" which extends "Common" class.
All properties are automatic get and set.
Here the problem is,
I have created web api.It returned object of Area class.
So here client received all properties of area and common.But I need specific properties to response.
Means I just need two properties of Area i.t AreaId,AreaName
This requirement for retuned data in different format like JSON and XML.I did with linq it gives specific properties Which I need exactly. But It is anonymous type data. Not strongly object.
following sample of my code
public class Common
{
public int CaseNo { get; set; }
public int? RET_ID { get; set; }
public string MSGSTATUS { get; set; }
public string MSG { get; set; }
public int? LoginId { get; set; }
}
public class Area : Common
{
public int AreaId { get; set; }
public string AreaName { get; set; }
public string PinCode{ get; set; }
}
/Web api code/
public IHttpActionResult GetAreaById(int AreaId, int LoginId)
{
try
{
AreaDAL objDal = new AreaDAL();
Area objBo = new Area();
objBo = objDal.EditArea(AreaId, LoginId);
if (objBo != null)
{
/*Not working for xml returned data(work for json).anonymous type data*/
return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new Area { AreaId = objBo.AreaId, AreaName = objBo.AreaName }));
/*working for json and xml */
/*But it retuned all properties of Area and common*/
/*Needed as AreaId and AreaName*/
return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, objBo));
}
}
Introduce an intermediate class (AreaInfo) and pull AreaId and AreaName members up:
public class AreaInfo : Common
{
public int AreaId { get; set; }
public string AreaName { get; set; }
}
public class Area : AreaInfo
{
public string PinCode{ get; set; }
}
//...
public IHttpActionResult GetAreaById(int AreaId, int LoginId)
{
try
{
AreaDAL objDal = new AreaDAL();
Area objBo = new Area();
objBo = objDal.EditArea(AreaId, LoginId);
if (objBo != null)
{
return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new AreaInfo { AreaId = objBo.AreaId, AreaName = objBo.AreaName }));
return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, objBo));
}
}
edit: for hiding fields in the Common base class, you can:
1) Change their access modifier (e.g. to protected)
2) Mark them with attributes to be skipped on serialization like:
[XmlIgnore] for xml serialization
[JsonIgnore] for json serialization
3) Separate the class hierarchies (Common from AreaInfo<-Area) and use composition for when you need
extra fields in Common class.
e.g.
public class Common<T>
where T: class
{
//... common fields here
public T Data {get;}
public Common(T data) => Data = data;
}
...
var area = new Common(new Area(){...});
//area.LoginId;
//area.Data.AreaId;

JSON.NET Serializes Empty JSON

I am using MetadataType to define Json.NET attributes for the following type, then serializing it using Json.NET inside its ToString() method:
namespace ConsoleApp1
{
public interface ICell
{
int Id { get; }
}
public interface IEukaryote
{
System.Collections.Generic.IEnumerable<ICell> Cells { get; }
string GenericName { get; }
}
public sealed partial class PlantCell
: ICell
{
public int Id => 12324;
}
public sealed partial class Plant
: IEukaryote
{
private readonly System.Collections.Generic.IDictionary<string, object> _valuesDict;
public Plant()
{
_valuesDict = new System.Collections.Generic.Dictionary<string, object>();
var cells = new System.Collections.Generic.List<PlantCell>();
cells.Add(new PlantCell());
_valuesDict["Cells"] = cells;
_valuesDict["GenericName"] = "HousePlant";
}
public System.Collections.Generic.IEnumerable<ICell> Cells => _valuesDict["Cells"] as System.Collections.Generic.IEnumerable<ICell>;
public string GenericName => _valuesDict["GenericName"] as string;
public int SomethingIDoNotWantSerialized => 99999;
public override string ToString()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this,
new Newtonsoft.Json.JsonSerializerSettings()
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
}
);
}
}
[System.ComponentModel.DataAnnotations.MetadataType(typeof(PlantMetadata))]
public sealed partial class Plant
{
[Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
internal sealed class PlantMetadata
{
[Newtonsoft.Json.JsonProperty]
public System.Collections.Generic.IEnumerable<ICell> Cells;
[Newtonsoft.Json.JsonProperty]
public string GenericName;
//...
}
}
class Program
{
static void Main(string[] args)
{
var plant = new Plant();
System.Console.WriteLine(System.String.Format("Output is {0}", plant.ToString()));
System.Console.ReadKey();
}
}
}
My problem is that Plant.ToString() will return '{}'. Why is that? It was working before. The only change I made was in PlantMetadata where I altered the MemberSerialization to OptIn instead of OptOut, as I had less properties I wanted included than left out.
As stated by Newtonsoft in this issue, MetadataTypeAttribute attributes are in fact supported by Json.NET. However, it appears that Json.NET requires that the MetadataClassType members must be properties when the corresponding "real" members are properties, and fields when the corresponding "real" members are fields. Thus, if I define your Plant type as follows, with two properties and one field to be serialized:
public sealed partial class Plant : IEukaryote
{
public System.Collections.Generic.IEnumerable<ICell> Cells { get { return (_valuesDict["Cells"] as System.Collections.IEnumerable).Cast<ICell>(); } }
public string GenericName { get { return _valuesDict["GenericName"] as string; } }
public string FieldIWantSerialized;
public int SomethingIDoNotWantSerialized { get { return 99999; } }
// Remainder as before.
Then the PlantMetadata must also have two properties and one field for them to be serialized successfully:
//Metadata.cs
[System.ComponentModel.DataAnnotations.MetadataType(typeof(PlantMetadata))]
public sealed partial class Plant
{
[JsonObject(MemberSerialization.OptIn)]
internal sealed class PlantMetadata
{
[JsonProperty]
public IEnumerable<ICell> Cells { get; set; }
[JsonProperty]
public string GenericName { get; set; }
[JsonProperty]
public string FieldIWantSerialized;
}
}
If I make Cells or GenericName be fields, or FieldIWantSerialized be a property, then they do not get opted into serialization.
Sample working .Net Fiddle.
Note that, in addition, I have found that the MetadataClassType properties apparently must have the same return type as the real properties. If I change your PlantMetadata as follows:
[JsonObject(MemberSerialization.OptIn)]
internal sealed class PlantMetadata
{
[JsonProperty]
public object Cells { get; set; }
[JsonProperty]
public object GenericName { get; set; }
[JsonProperty]
public object FieldIWantSerialized;
}
Then only FieldIWantSerialized is serialized, not the properties. .Net Fiddle #2 showing this behavior. This may be a Newtonsoft issue; as stated in the Microsoft documentation Defining Attributes in Metadata Classes:
The actual type of these properties is not important, and is ignored
by the compiler. The accepted approach is to declare them all as of
type Object.
If it matters, you could report an issue about the return type restriction to Newtonsoft - or report an issue asking that details of their support for MetadataTypeAttribute be more fully documented.

what's an elegant method to transfer/cast a poco of 1 type to a poco of another type - different namespace but same name and fields?

Let's say you have a Domain layer which returns a User object:
public class User
{
public string FirstName{get;set;}
public string LastName{get;set;}
}
Let's say you have an identical class defined in your service layer. What's an elegant method to easily transfer/cast the Domain User object into a service User object?
"Elegant" is subjective. I might just write an extension that converts one to the other.
public static class MappingExtensions
{
public ThisNameSpace.User ToThisUser(this OtherNameSpace.User source)
{
return new ThisNameSpace.User
{
FirstName = source.FirstName,
LastName = source.LastName,
UserId = source.UserId
}
}
}
To me that's the simplest.
You could also use Automapper (add from Nuget.)
Do a one-time configuration:
AutoMapper.Mapper.Initialize(c=>c.CreateMap<User,Other.User>());
and then you can call it to map an instance of User to a new instance of Other.User.
var other = AutoMapper.Mapper.Map<Other.User>(user);
It works without specifying the mapping for individual properties if the property names and types are identical.
You can use reflection:
using System.Windows.Forms;
using System.Reflection;
namespace First
{
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserID { get; set; }
public User()
{
}
}
}
namespace Second
{
public class User
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserID { get; set; }
public User()
{
}
}
}
namespace YetAnotherNamespace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
First.User user = new First.User()
{
FirstName = "John",
LastName = "Public",
UserID = "jpublic#mydomain.com"
};
Second.User newUser = ConvertUser(user);
}
public Second.User ConvertUser(First.User oldUser)
{
Second.User newUser = new Second.User();
foreach (PropertyInfo prop in oldUser.GetType().GetProperties())
{
string propertyName = prop.Name;
object propertyValue = prop.GetValue(oldUser);
newUser.GetType().GetProperty(propertyName).SetValue(newUser, propertyValue);
}
return newUser;
}
}
}
In this code sample, when I instantiate the form, I create a First.User and convert it to a Second.User.
This is tested and working (as a strongly-typed method). I think you can make it more generic and accept and return an "object", and it will just throw an exception if the properties don't match up. Also keep in mind that reflection tends to be slow - this may not be the most scalable solution.
Another approach would be serializing to json and then deserializing to the other type.

Set JsonProperty PropertyName dynamically

I' m developing some WebAPI and have to return HttpResponseMessage with 2 standart service fields and one field which contains an array. Something like this:
{
field1:"test",
field2:123,
array:[{},{}]
}
But actually i need several responses which should differ only in name of array property:
{
field1:"test",
field2:123,
someArray:[{},{}]
}
and
{
field1:"test",
field2:123,
someOtherArray:[{},{}]
}
Type of elemnts in array is the same in each response. So I want to create one class for such type of responses and just change its PropertyName. Like this one:
public class Response
{
public int field2 { get; set; }
public string field1 { get; set; }
[JsonProperty(PropertyName = ???)]
public IEnumerable<SomeType> Array { get; set; }
}
How can I implement it?
You could also consider using a dictionary.
[HttpGet, Route("api/yourroute")]
public IHttpActionResult GetSomeData()
{
try
{
var data = new Dictionary<string, dynamic>();
data.Add("field1", "test");
data.Add("field2", 123);
var fieldName = someCondition == true? "someArray" : "someOtherArray";
data.Add(fieldName, yourArray);
return Ok(data);
}
catch
{
return InternalServerError();
}
}

Using Reflection and GetValue problems

I have an abstract class that looks like so:
public abstract class PageObjectsBase
{
public abstract string FriendlyName { get; }
public abstract string PageObjectKeyPrefix { get; }
public abstract string CollectionProperty { get; }
}
And a class that derives from PageObjectsBase:
public class PageRatingList : PageObjectsBase
{
public IList<PageRating> PageRatings { get; set; }
public PageRatingList()
{
this.PageRatings = new List<PageRating>();
}
public override string CollectionProperty
{
get
{
var collectionProperty = typeof(PageRatingList).GetProperties().FirstOrDefault(p => p.Name == "PageRatings");
return (collectionProperty != null) ? collectionProperty.Name : string.Empty;
}
}
public override string FriendlyName
{
get
{
return "Page feedback/rating";
}
}
public override string PageObjectKeyPrefix
{
get
{
return "pagerating-";
}
}
}
And a PageRating class which PageRatingList.PageRatings is holding a collection of:
public class PageRating : PageObjectBase
{
public int Score { get; set; }
public string Comment { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
The PageRatingList is being stored in a database (EPiServer's Dynamic Data Store, more specifically using the Page Object Manager). I need to create some reporting functionality and am essentially loading all reports that derive from PageObjectBase. When it comes to returning the data, the code will never know at compile time what type of data it is to load, so I am using Reflection. In my reporting class I have:
//this gives me the right type
var type = Type.GetType("MyNameSpace.PageRatingList", true);
var startPageData = this._contentRepository.Get<PageData>(startPage);
PageObjectManager pageObjectManager = new PageObjectManager(startPageData);
//this loads the instances from the DB
var props = pageObjectManager.LoadAllMetaObjects()
.FirstOrDefault(o => o.StoreName == "Sigma.CitizensAdvice.Web.Business.CustomEntity.PageRatingList");
//this gives me 4 PropertyInfo objects (IList: PageRatings, string : CollectionProperty, string :FriendlyName, string : PageObjectKeyPrefix)
var properties = props.Value.GetType().GetProperties();
I can then iterate through the PropertyInfo objects using:
foreach (var property in properties)
{
//extract property value here
}
The issue I am having is that I cannot figure out how to get the value of each of the propertyinfo objects. In addition, one of those properties is type List and again we wont know the type of T until runtime. So I also need some logic that checks if one of the PropertyInfo objects is of type List and then provides access to each of the properties in the List - the List being of type PageRating.
Can anyone help here? I've not really used reflection in the past so I am winging my way through it, rightly or wrongly!
Many thanks
Al
I may be missunderstanding the problem, but i think you may use something like this:
var props = new PageRatingList(); /*actual instanse of the object, in your case, i think "props.Value" */
var properties = typeof(PageRatingList).GetProperties();
foreach (var property in properties)
{
if (property.PropertyType == typeof(IList<PageRating>))
{
IList<PageRating> list = (IList<PageRating>)property.GetValue(props);
/* do */
}
else
{
object val = property.GetValue(props);
}
}
Hope this helps to find your solution.

Categories