Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to set the values of some private properties with JSON values that I am getting from a web service.

A) Is this possible
B) If so, how do I do it?

EDIT: Let me re-iterate. I need the public property to have a private setter. I have tried setting the setter to internal and then changing the AssemblyInfo file to expose internals to Newtonsoft.Json. This doesnt work either?

Here is my code:

using (WebClient wc = new WebClient())
{
        DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(MyAmazingObject));
        return (FacebookUser)js.ReadObject(wc.OpenRead("URL_HERE"));
}
Posted
Updated 29-Jun-11 0:53am
v3

1 solution

The answer by Vivek is unfortunately incorrect.

You can use serialization of private members if you use Data Contract. You need to use System.Runtime.Serialization.Json.DataContractJsonSerializer, see http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^].

To get the idea, read this: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

When you use Data Contract, the meta-data is defined not just by access specifier, but by special attributes such as [DataMember]. Data Contract is much more non-intrusive and beneficial over regular "legacy" serialization.

Please see also my past answer where I advocate Data Contract approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900