Skip to content

Instantly share code, notes, and snippets.

@solvingj
Last active May 9, 2017 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solvingj/3c112bba2fb22b86386910a0ea49625a to your computer and use it in GitHub Desktop.
Save solvingj/3c112bba2fb22b86386910a0ea49625a to your computer and use it in GitHub Desktop.
Works with no annotations...
[Fact]
public void Should_serialize_Test()
{
var sets = new DataContractJsonSerializerSettings { SerializeReadOnlyTypes = true };
Test test = new Test { Name = "name", City = "city" };
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Test), sets);
MemoryStream ms = new MemoryStream();
js.WriteObject(ms, test);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string serialized = sr.ReadToEnd();
sr.Dispose();
ms.Dispose();
serialized.ShouldBeEquivalentTo("{\"City\":\"city\",\"Name\":\"name\"}");
}
public class Test : ISerializable
{
public bool IsFailure { get; }
public string Name;
public string City;
void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
{
oInfo.AddValue("IsFailure", IsFailure);
oInfo.AddValue("Name", Name);
if (IsFailure)
{
oInfo.AddValue("City", City);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment