Last active
May 9, 2017 00:25
-
-
Save solvingj/3c112bba2fb22b86386910a0ea49625a to your computer and use it in GitHub Desktop.
Works with no annotations...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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