fastJSON 的一个扩展版本:PowerJSON

jopen 9年前

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  }

项目主页:http://www.open-open.com/lib/view/home/1431395082435