Skip to content

Instantly share code, notes, and snippets.

@gongdo
Last active December 14, 2015 11:08
Show Gist options
  • Save gongdo/233b66d8d015b3c064f7 to your computer and use it in GitHub Desktop.
Save gongdo/233b66d8d015b3c064f7 to your computer and use it in GitHub Desktop.
How to set different json property name between serialization and deserialization.
public class Document
{
// actual property
[JsonIgnore]
public string Value { get; set; }
// performs serialization only.
[JsonProperty("PresentationValueName")]
private string ValueGetter { get { return Value; } }
// performs deserialization only.
[JsonProperty("PersistenceValueName")]
private string ValueSetter { set { Value = value; } }
}
/* JsonConvert.SerializeObject(new Document { Value = "gongdo" });
{ "PresentationValueName":"gongdo" }
*/
/* JsonConvert.DeserializeObject<Document>("{ \"PersistenceValueName\":\"gongdo\" }");
// Document.Value: "gongdo"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment