A possible way is to declare these fields as private or internal .
An alternative solution is to use the DataContractJsonSerializer class. In this case, you add the DataContract attribute to your class. You can manage the members that you want to serialize with the DataMember - all the elements marked with it are serialized, and the rest are not.
You should rewrite your ToJson method as follows:
public string ToJson() { DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(<your class name>)); MemoryStream ms = new MemoryStream(); jsonSerializer.WriteObject(ms, this); string json = Encoding.Default.GetString(ms.ToArray()); ms.Dispose(); return json; }
Eugene
source share