This project has moved and is read-only. For the latest updates, please go here.

serialize only a couple of properties.

Feb 11, 2011 at 3:40 PM

Hi, im using to download a json string from my server and convert to object, next i edit the object and after i need to convert again to json to send to my server again.

the problem is that when i deserialize i need to read and set all the properties of my object, but when i serielize it i need to only serialize a ocuple of properties, i tried with [JsonProperty] but it applies for both serialization and deserialization, and i need to set it only for serialize,


what i can do?

Feb 11, 2011 at 3:53 PM

I would have said that in an ideal world the same JSON should be sent both ways.

However, I admit I have found myself wanting support for this at times, mostly when dealing with legacy JSON.

Perhaps the simplest option in your case would just be to have two different objects on the server: one which you use for deserialising into, and a second (constructed from the first, or perhaps a base type of the first) which is serialized to the client.

Feb 11, 2011 at 5:00 PM

Im a C# novice, can you guide me on how to convert from List<App> to List<AppSerializable>?

I have a list of App, i just want to remove the setup_html when serialize the object of every App. 

string sJSON = JsonConvert.SerializeObject(this.Application);

public List<App> Application { get; set; }

    public class App{ //si le llamo Application a esto da error Program.cs 
        public int id { get; set; }
        public string name { get; set; }
        public string setup_html { get; set; }
        public string download_url { get; set; }
        public string execution_params { get; set; }

        public string not_necessary_for_commission { get; set; }
        public string only_if_no_previous_was_installed { get; set; }
        public string last_to_install_if_installed { get; set; }

        private bool _showed = false;
        public bool showed { get { return _showed; } set { _showed = value;  } }
        private bool _installed = false;
        public bool installed { get { return _installed; } set { _installed = value; } }

    public class AppSerializable : App
        public string setup_html{get; set;}
Feb 14, 2011 at 10:39 AM

It's a bit boilerplate-like, but I think the simplest way to do this would be a constructor for AppSerializable which takes an instance of App and copies over all the properties.  Alternatively you could have a static method on App which returns the serializable version; the end result is much the same.  Then, you can write something like:

List<App> listOfApps = ...
List<AppSerializable> appsToSerialize = listOfApps.Select(app => new AppSerializable(app));
Mar 26, 2011 at 12:50 AM

manolet: I think your best bet is to go for a ContractResolver and remove the properties that you don't want to send, or, maybe, use Json.Linq to remove it and then serialize that.

Another option is a JsonConverter.