Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / Markdown

APJSON

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
28 Aug 2013CPOL13 min read 41.7K   1.2K   34  
This is an alternative for "fastJSON"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Apolyton.FastJson.Common;
using Apolyton.FastJson.Registry;
using Apolyton.FastJson.Serialization;
using System.Runtime.Serialization;

namespace Apolyton.FastJson
{
    /// <summary>
    /// Represents the parameters for the json class. Changing instance values at runtime when a (de-)serialization is ongoing on that instance can lead to unexpected outcome. 
    /// Don't do it. 
    /// </summary>
    /// <remarks>
    /// http://www.codeproject.com/Articles/159450/fastJSON
    /// The version over there (2.0.9) could not be taken directly, as its serializer is taking all public properties, disregarding any attribute policy. This is
    /// not good for our case, as we want to return (portions of) data objects.
    /// <para/>
    /// Not thread safe.
    /// </remarks>
    public sealed class JsonParameters
    {
        private bool _useOptimizedDatasetSchema = true;
        private bool _useFastGuid = true;
        private bool _serializeNullValues = true;
        private bool _useUtcDateTime = true;

        private bool _useGlobalTypes = false;
        private bool _ignoreCaseOnDeserialize = false;
        private bool _enableAnonymousTypes = false;
        private bool _useTypeExtension = true;

        private Encoding _encoding = Encoding.UTF8;

        internal SerializationPolicy _serializationPolicy = SerializationPolicy.PropertyOptOut;

        /// <summary>
        /// Creates an instance of the json parameters. For optimal performance, you should keep the instance in order to be reused. 
        /// </summary>
        public JsonParameters()
        {
            Registry = new JsonRegistry(this);
        }

        /// <summary>
        /// Returns the list of members which are concerded by (de-)serialization according to the current default parameters.
        /// </summary>
        public IEnumerable<JsonSerializationInfo> GetSerializationMembers(Type type)
        {
            return Registry.GetPropertiesAndFields(type, null).Values.Select(info => info.ToSerializationInfo());
        }

        /// <summary>
        /// Registers the given type descriptor which handles the type-name / type relationship.
        /// </summary>
        public void RegisterTypeDescriptor(JsonTypeDescriptor descriptor)
        {
            Guard.ArgumentNotNull(descriptor, "descriptor");

            Registry.TypeDescriptor = descriptor;
        }

        /// <summary>
        /// Registers a custom type.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="serializer"></param>
        /// <param name="deserializer"></param>
        /// <exception cref="DuplicakeyException">If type is already registered.</exception>
        public void RegisterCustomType(Type type, SerializationHandler serializer, DeserializationHandler deserializer)
        {
            Guard.ArgumentNotNull(type, "type");

            Registry.RegisterCustomType(type, serializer, deserializer);
        }

        /// <summary>
        /// Registers a custom type.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="serializer"></param>
        /// <param name="deserializer"></param>
        public void RegisterCustomType(ICustomTypeSerializer customTypeSerializer)
        {
            Guard.ArgumentNotNull(customTypeSerializer, "customTypeSerializer");
            Guard.ArgumentNotNull(customTypeSerializer.Type, "customTypeSerializer.Type");

            Registry.RegisterCustomType(customTypeSerializer.Type, 
                (customTypeSerializer.CanSerialize ? customTypeSerializer.Serialize : (SerializationHandler) null),
                (customTypeSerializer.CanDeserialize ? customTypeSerializer.Deserialize : (DeserializationHandler)null));

            if (!String.IsNullOrEmpty(customTypeSerializer.TypeName))
            {
                Registry.TypeDescriptor.Register(customTypeSerializer.Type, customTypeSerializer.TypeName);
            }
        }

        /// <summary>
        /// Gets the internal registry.
        /// </summary>
        internal readonly JsonRegistry Registry;

        /// <summary>
        /// Gets or sets the encoding of (input) json strings.
        /// </summary>
        public Encoding Encoding
        {
            get { return _encoding; }
            set { _encoding = value; }
        }

        /// <summary>
        /// Use the optimized fast Dataset Schema format (default = True)
        /// </summary>
        public bool UseOptimizedDatasetSchema
        {
            get { return _useOptimizedDatasetSchema; }
            set { _useOptimizedDatasetSchema = value; }
        }

        /// <summary>
        /// Use the fast GUID format. (default = True). The fast format is the base64 encoded byte array of the underlying guid. Otherwise a string representation of it. 
        /// (eg. 12345678-ABCD-ABCD-ABCD-1234567890AB).
        /// </summary>
        public bool UseFastGuid
        {
            get { return _useFastGuid; }
            set { _useFastGuid = value; }
        }

        /// <summary>
        /// Serialize null values to the output (default = True)
        /// </summary>
        public bool SerializeNullValues
        {
            get { return _serializeNullValues; }
            set { _serializeNullValues = value; }
        }

        /// <summary>
        /// Use the UTC date format (default = True) for DateTime object with Kind=unspecified.
        /// </summary>
        public bool UseUtcDateTime
        {
            get { return _useUtcDateTime; }
            set { _useUtcDateTime = value; }
        }

        /// <summary>
        /// Defines the serialization policy.
        /// </summary>
        public SerializationPolicy SerializationPolicy
        {
            get { return _serializationPolicy; }
            set { _serializationPolicy = value; }
        }

        /// <summary>
        /// Use the $types extension to optimise the output json (default = false). Can overridden (=False or true) during parsing under some conditions.
        /// Side effect on UseTypeExtension.
        /// </summary>
        /// <remarks>
        /// The root level json object will contain a $types list which contains all identified types in the graph. Then, it the graph itself,
        /// an item might contain the $type extension which is an integer pointing to the index (starting at 1) of the global type.
        /// </remarks>
        /// <example>
        /// 
        /// </example>
        public bool UseGlobalTypes
        {
            get { return _useGlobalTypes; }
            set
            {
                _useGlobalTypes = value;

                if (_useGlobalTypes)
                {
                    _useTypeExtension = true;
                }
            }
        }

        /// <summary>
        /// ** work in progress
        /// </summary>
        [Obsolete]
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool IgnoreCaseOnDeserialize
        {
            get { return _ignoreCaseOnDeserialize; }
            set { _ignoreCaseOnDeserialize = value; }
        }


        /// <summary>
        /// Gets the currenty type descriptor. 
        /// </summary>
        /// <remarks>
        /// Use RegisterTypeDescriptor to set the value. Never set it while (de-) serialization is ongoing.
        /// </remarks>
        public JsonTypeDescriptor TypeDescriptor
        {
            get { return Registry.TypeDescriptor; }
        }

        /// <summary>
        /// Anonymous types have read only properties. Affects also serialization policy, use extension and using global types.
        /// </summary>
        public bool EnableAnonymousTypes
        {
            get { return _enableAnonymousTypes; }
            set
            {
                _enableAnonymousTypes = value;

                if (_enableAnonymousTypes)
                {
                    _serializationPolicy = SerializationPolicy.PropertyOptOut | SerializationPolicy.IncludeReadOnly;
                    _useTypeExtension = false;
                    _useGlobalTypes = false;
                }
            }
        }
       
        /// <summary>
        /// Enable fastJSON extensions $type (default = True).
        /// </summary>
        public bool UseTypeExtension
        {
            get { return _useTypeExtension; }
            set { _useTypeExtension = value; }
        }

        [Obsolete("Use UseTypeExtension instead.", true)]
        public bool UseExtensions
        {
            get;
            set;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions