ASP.NET ScriptService prevent return of null properties in JSON - asp.net

Basically I want to make my script service only serialise properties that are not null on an array of object I am returning... So this..
{"k":"9wjH38dKw823","s":10,"f":null,"l":null,"j":null,"p":null,"z":null,"i":null,"c":null,"m":0,"t":-1,"u":2}
would be
{"k":"9wjH38dKw823","s":10,"m":0,"t":-1,"u":2}
Does anyone know if this is possible?
Basically the reason for this is because null values are for unchanged properties. A local copy is kept in the javascript that is just updated to reduce traffic to the server. Change values are then merged.

You can create a custom JavaScriptConverter class for the JSON serialization process to use to handle your object, and then put the necessary logic in the Serialize method of that class to exclude the properties that are null.
This article has a clear step-by-step discussion of the process involved in creating it.
You probably would not need to actually implement the Deserialize method (can throw a NotImplementedException) if you are not passing that type of object in as an input parameter to your web services.

Related

How to pass array of custom objects to ASMX webservice?

I am trying to pass an array of custom objects from my asp.net code behind to a webservice method. The webservice is ASMX and I can't change that for now. I can verify up to the point of the code behind calling the webservice, that the custom object is in the array and has the proper values. When I put a break point in the webservice, I notice that the array is null.
I have checked my Service Reference and the collection type is System.array.
I read that I might need to mark my custom class with [DataContract] and the properties as [Datamember] for this to work, but these don't even show up in the intellisense.
I tried all kinds of things and I couldn't get this to work. I tried casting the custom objects as objects and passing them that way. I also tried to cast the whole array as type object and that didn't work. I tried collections; no go. I read a lot on this and couldn't find anyone who could actually do this. So I gave up on doing this with ASMX and tried it with a WCF service. That did work nicely. I created a custom object. I created a strongly typed collection of this class and populated the collection. I then passed this collection to the WCF service and the method was able to see all the objects properly. So if anybody is stuck in the situation I was, try a regular WCF service.

AutoMapper: Action on each element during collection processing?

Is it possible to invoke a method on each object that is being copied from a source to a destination collection using AutoMapper? The destination object has a method called
Decrypt() and I would like it to be called for each CustomerDTO element that is created. The only thing that I can figure out is to perform the mapping conversion and then loop again to invoke the Decrypt() method. I'd appreciate your help with this question.
Thanks,
Mike
IQueryable<CustomerDTO> dtos = AutoMapper.Mapper.Map<IQueryable<CustomerEntity>, IQueryable<CustomerDTO>>((BaseRepository.List));
foreach (var item in dtos)
{
item.Decrypt(Seed);
}
It depends if you are decrypting just a property or the whole object. I wasn't sure based on your question.
If you are just decrypting properties, then I suggest that you look into AutoMapper's Custom Value Resolvers. They allow you to take control when resolving a destination property.
If you need to decrypt the whole object, then I suggest you look into AutoMapper's Custom Type Converters. That gives you complete control over the conversion, though it does sort of take the auto out of AutoMapper.

How can I check for a nil="true" value on XML that is passed back from webservice call?

I am receiving xml from a webservice call that contains a nil="true":
<cacheEntry>
<systemFK nil="true"/>
</cacheEntry>
I used the Flex DataService (webservice) wizard to create the service objects for the cacheEntry component. This object will be serialized later on a different webservice call and stored in a database.
I set a breakpoint on the set SystemFK method in the service object. It appears that the value passed in was an empty string!
Allowing this value to be an empty string will cause problems in the webservice implementation in Java on the other side. Since the database value was null it is expecting a null in return, If I avoid setting this value, the serviceObject should send back a null which will make the database happy.
My question is: How can I detect that a nil = true is present in the XML in order to avoid setting this value?
For some reason the ActionScript XML parsers don't know about Booleans. Without seeing the code that got generated for you, my guess is that somehow you're getting the string "true", instead of true, and that's what's causing your problem.
Make changes to the accessors to act as if #nil comes from the XML as a string, and then convert to Boolean manually.

Creating custom objects for wcf

I have an existing web application that uses EF and POCO objects. I want to improve the client experience by exposing some of my objects through WCF(JSON). I have this working fine but where I am unsure is how to handle derived objects(not sure if that is the correct term) or IEnumerable anonymous objects if you will.
Let's say I have 3 tables structured like so:
Templates
ID
Template
Groups
ID
Group
Instances
ID
TemplateID
GroupID
This is obviously a one-to-many type relationship. I have my navigation properties setup correctly and getting strongly typed object properties works great. However, how do I send serialized anonymous type object(s) over the wire. Like an object that sends all instances that are equal to groupid=1 and include the names of the template and the object.
Am I missing something or do I have to create another class object for WCF that would look like this:
WCF Object
InstanceID
TemplateID
TemplateName
GroupID
GroupName
I guess I could alter my tables to account for this but that seems wrong too. I know that IEnumerable objects can't be serialized and I know that throw away objects are probably not the way to go either. I want to do this the right way but I am not sure how to go about it.
Your suggestions are appreciated.
Regards
Based on what you're doing, I'd suggest looking at OData with WCF Data Services. You state that you want to be able to send all instances where the groupid=1 - OData is great at this type of filtering.
If you're want to stick with your current approach and not use OData, then my first question is why are you sending back anonymous types at all? You can do what you are seeking (all instances with a groupid=1) without sending back an anonymous type. In your select clause you just create new instances of your concrete objects rather than newing up anonymous types. If your query is really just filtering and not executing any meaningful projection with the selct to anonymous type, then I don't see any reason to send back your anonymous type at all.

Determining which properties to serialize in a class that's passed over a webservice

I'm using NHibernate to administer my entities, and to have lazy loading enabled I need to make my properties return an IList<>. Problem is that .NET throws an exception as it can't serialize an interface when I'm trying to pass the entity. This makes perfect sense.
What I need to know is how I can control which fields to serialize, and which not to? My best bet so far is to work around this problem by copying the contents of IList<> into a List<> before serializing the object, but to do that I need to tell .NET that I don't want the IList<> property serialized :)
Just wanted to let you guys know that I found the answer to be the
[System.Xml.Serialization.XmlIgnore] attribute :)
MSDN has an area on Serializing Objects, but what you want is Selective Serialization. So basically, you can mark any property you don't want serialized with the attribute, [NonSerialized]. There is an example in the second link.

Resources