Just downloaded ServiceStack.Text to use it in my ASP.NET. I have class with many properties and would like to serialize five of them(string, integer, binary) to JSON. Could anyone post simple example how to create JSon object from my class?
相关问题
- Jackson Deserialization not calling deserialize on
- Sorting 3 numbers without branching [closed]
- How to maintain order of key-value in DataFrame sa
- Graphics.DrawImage() - Throws out of memory except
- StackExchange API - Deserialize Date in JSON Respo
servicestack's test proves that by providing the
[DataContract]
and[DataMember]
attribute allows you to determine which one is being serialized and which doesn't.Test: https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/DataContractTests.cs
objects in test: https://github.com/ServiceStack/ServiceStack.Text/blob/master/tests/ServiceStack.Text.Tests/Support/DdnDtos.cs
You can use the
[Serializable()]
attribute on your custom class and then:To ignore specific properties in the object you're serializing, simply place the
[NonSerialized]
attribure on them.Update:
Given that you want to use ServiceStack to do your serialization, the ServiceStack website gives the following example:
Source: http://www.servicestack.net/mythz_blog/?p=344
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:
Then you can use either the static utility methods on JsonSerializer to (De)serialize it, or the more terse extension methods, e.g:
Edit:
As @Noah mentions (from comments) you can also use the [IgnoreDataMember] attribute to exclude a single property.