ServiceStack will deserialize all public properties of a POCO by default.
If you only want to serialize just a few of the properties then you want to decorate your class with [DataContract], [DataMember] attributes (in the same way you would if you were using MS DataContractJsonSerializer), e.g:
[DataContract]
public class MyClass
{
public string WillNotSerializeString { get; set; }
[DataMember]
public string WillSerializeString { get; set; }
[DataMember]
public int WillSerializeInt { get; set; }
[DataMember]
public byte[] WillSerializeByteArray { get; set; }
}
Then you can use either the static utility methods on JsonSerializer to (De)serialize it, or the more terse extension methods, e.g:
var dto = new MyClass { WillSerializeString = "some text" };
string json = dto.ToJson();
MyClass fromJson = json.FromJson<MyClass>();
Edit:
As @Noah mentions (from comments) you can also use the [IgnoreDataMember] attribute to exclude a single property.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…