fastJSON 的一個擴展版本:PowerJSON
PowerJSON 是 fastJSON 的一個擴展版本,提供一些新方法、類和接口,以及對發現的問題進行修復。
PowerJSON的特性
新特性
- Rename serialized members.
- Rename serialized Enum values.
- Deserializing non-public members.
- Polymorphic serialization without JSON extensions.
- Write out additional key-value pairs in the serialized JSON.
- Conditional serialization.
- Noninvasive control of serialization.
- A comprehensive documentation.
新的類和接口
The newly introduced classes and interfaces have brought more control onto each aspect in serialization.
- SerializationManager: Caches the reflection result and controls the serialization.
- IReflectionController: Controls every possible aspect in reflection for serialization.
- IJsonInterceptor: Intercepts JSON serialization and enables conditional serialization.
- IJsonConverter: Converts between various value types.
擴展的屬性
This fork has empowered fastJSON to serialize and deserialize objects with additional attribute types. The attributes are listed below:
- JsonInterceptorAttribute: 1) controls the procedure of serialization. 2) allows conditional serialization. 3) allows writing out extra key-value pairs in the serialized object.
- JsonFieldAttribute: 1) controls the name of the serialized field or property. 2) allows serializing interface or abstract types with the Type and Name conbination in the attribute.
- JsonIncludeAttribute: 1) explicitly controls whether a field or property is serialized or not. 2) allows serializing readonly property even when theShowReadOnlyPropertiessetting is turned off.
- JsonConverterAttribute: allows transforming data before serialization.
- JsonEnumValueAttribute: controls the serialized literal name of anEnumvalue.
- JsonSerializableAttribute: enables serializing and deserializing private or internal types.
Some .NET built-in attributes are also supported.
- DefaultValueAttribute: values equal toDefaultValueAttribute.Valueare not serialized.
- ReadOnlyAttribute: values are not deserialized whenReadOnlyAttribute.IsReadOnlyis set to true.
JSONParameters中的新設置
This fork introduced the following settings inJSONParameters:
- NamingConvention: control the naming convention of serialized fields and properties. It has added support for camel-case, uppercase names.
- SerializeStaticMembers: control whether static fields or properties should be serialized. (2015-4-2)
- ShowReadOnlyFields: control whether readonly fields should be serialized. (2015-4-7)
- SerializeEmptyCollections: control whether zero length collections, arrays, lists, dictionaries, etc. are serialized. (2015-4-25)
示例代碼:
// marks the internal DemoClass class deserializable
[JsonSerializable]
internal class DemoClass
{
// marks MyProperty property to be serialized to a field named "prop"
[JsonField ("prop")]
public string MyProperty { get; set; }
// marks MyEnumProperty property to be serialized to a field named "enum"
[JsonField ("enum")]
public MyEnum MyEnumProperty { get; set; }
// marks not to serialize the Number property, if its value is 0
[System.ComponentModel.DefaultValue (0)]
public int Number { get; set; }
// marks the serialized name of Identifier will be "a", if its type is ClassA,
// and "b" for ClassB, and "variant" for other types
[JsonField ("a", typeof (ClassA))]
[JsonField ("b", typeof (ClassB))]
[JsonField ("variant")]
public object Identifier { get; set; }
// marks the InternalValue property will not be serialized
[JsonInclude (false)]
// marks the InternalValue property will not be deserialized
[System.ComponentModel.ReadOnly (true)]
public int InternalValue { get; set; }
}
public enum MyEnum
{
None,
// marks the serialized name of Vip to "VIP"
[JsonEnumValue ("VIP")]
Vip
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!