Click here to Skip to main content
15,880,796 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.6K   1.2K   34  
This is an alternative for "fastJSON"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apolyton.FastJson.Registry;

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 _usingGlobalTypes = true;
        private bool _ignoreCaseOnDeserialize = false;
        private bool _enableAnonymousTypes = false;
        private bool _useExtensions = 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>
        /// Fixes values by disabling conflicting inputs.
        /// </summary>
        internal void FixValues()
        {
            if (UseExtensions == false) // disable conflicting params
            {
                UsingGlobalTypes = false;
            }
        }

        /// <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)
        /// </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 = True). Can overridden (=False or true) during parsing under some conditions.
        /// </summary>
        public bool UsingGlobalTypes
        {
            get { return _usingGlobalTypes; }
            set { _usingGlobalTypes = value; }
        }

        /// <summary>
        /// ** work in progress
        /// </summary>
        public bool IgnoreCaseOnDeserialize
        {
            get { return _ignoreCaseOnDeserialize; }
            set { _ignoreCaseOnDeserialize = value; }
        }

        /// <summary>
        /// Anonymous types have read only properties 
        /// </summary>
        public bool EnableAnonymousTypes
        {
            get { return _enableAnonymousTypes; }
            set { _enableAnonymousTypes = value; }
        }

        /// <summary>
        /// Enable fastJSON extensions $types, $type, $map (default = True). Can overridden (=False) during parsing under some conditions.
        /// </summary>
        public bool UseExtensions
        {
            get { return _useExtensions; }
            set { _useExtensions = value; }
        }
    }
}

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