private JSonSerializer(TextWriter writer, JSonSerializationSettings settings = null)
        {
            Contract.Requires(writer, "writer");

            _writer  = writer;
            _options = JSonSerializationOption.Json;
            if (settings != null)
            {
                _options    = settings.Options;
                _serializer = settings.Serializer;
                _schema     = settings.Schema;
            }

            if (((int)_options & 32) != 0) // Hyperstore guard
            {
                _options |= JSonSerializationOption.Hyperstore;
            }
            else
            {
                if (((int)_options & 256) == 0)
                {
                    throw new ArgumentException("Either Json or Hyperstore format must be set in the option parameter");
                }
            }
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  true this instance to the given stream.
        /// </summary>
        /// <param name="mel">
        ///  The mel.
        /// </param>
        /// <param name="settings">
        ///  Options for controlling the operation.
        /// </param>
        /// <returns>
        ///  A string.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public static string Serialize(IModelElement mel, JSonSerializationSettings settings)
        {
            Contract.Requires(mel, "mel");

            var list = new List <IModelElement>();

            list.Add(mel);

            return(Serialize(list, settings));
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  true this instance to the given stream.
        /// </summary>
        /// <param name="elements">
        ///  The elements.
        /// </param>
        /// <param name="settings">
        ///  Options for controlling the operation.
        /// </param>
        /// <returns>
        ///  A string.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public static string Serialize(IEnumerable <IModelElement> elements, JSonSerializationSettings settings)
        {
            Contract.Requires(elements, "elements");
            Contract.Requires(settings, "settings");

            StringBuilder sb  = new StringBuilder(256);
            StringWriter  sw  = new StringWriter(sb, CultureInfo.InvariantCulture);
            var           ser = new JSonSerializer(sw, settings);

            ser.Serialize(elements);
            return(sw.ToString());
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  true this instance to the given stream.
        /// </summary>
        /// <param name="domain">
        ///  The domain.
        /// </param>
        /// <param name="settings">
        ///  Options for controlling the operation.
        /// </param>
        /// <returns>
        ///  A string.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public static string Serialize(IDomainModel domain, JSonSerializationSettings settings)
        {
            Contract.Requires(domain, "domain");

            // Force hyperstore format
            if (settings == null)
            {
                settings = new JSonSerializationSettings();
            }
            settings.Options |= JSonSerializationOption.Hyperstore;
            settings.Options &= ~JSonSerializationOption.SerializeGraphObject;
            return(Serialize(domain.GetElements(), settings));
        }