Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

APJSON

, 28 Aug 2013
This is an alternative for "fastJSON"
Apolyton_FastJson_Binaries__v0.92_.zip
Output
Debug
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Release x64
Apolyton.FastJson.dll
Release x86
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
Release
Apolyton.FastJson.dll
Silverlight
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
Apolyton_FastJson_Binaries__v0.93_.zip
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Apolyton.Json.Silverlight.dll
Apolyton_FastJson_Binaries__v0.9_.zip
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton_FastJson_Binaries__v1.0_.zip
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Apolyton.Json.Silverlight.dll
apolyton_fastjson_sources__v0.92_.zip
Apolyton.FastJson
Apolyton.FastJson.csproj.user
Apolyton.FastJson.Silverlight.csproj.user
Apolyton.FastJson.v11.suo
Bin
Debug
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
Common
Data
Diagram.cd
obj
Debug
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release x64
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release x86
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
Registry
Serialization
Apolyton.FastJson.Tests
bin
Debug
Release
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
Data
Helpers
ComplexTypes
ParameterRelated
Polymorphism
RegistryHelpers
StandardTypes
obj
Debug
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
build.force
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release (x86, CUSTOM TYPES)
build.force
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Properties
Registry
Serialization
Console Test
bin
Debug
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.pdb
consoletest.vshost.exe
fastJSON.dll
fastJSON.pdb
x86
DataObjects
obj
Debug
build.force
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release (x86, CUSTOM TYPES)
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release x64
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release x86
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release
build.force
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
x86
Debug
TempPE
Apolyton_FastJson_Sources__v0.93_.zip
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.dll
build.force
Apolyton.FastJSON.Tests.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Release
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Release (x86, CUSTOM TYPES)
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Release x64
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
Release x64
consoletest.exe
Release x86
consoletest.exe
Release
consoletest.exe
Apolyton.FastJson.csproj.user
Apolyton.FastJson.Silverlight.csproj.user
Apolyton.FastJson.v11.suo
Apolyton.Json.Silverlight.dll
Release
Apolyton.Json.Silverlight.dll
Diagrams
Json.cd
JsonParameters.cd
TypeDescriptors.cd
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.FastJson.dll
Apolyton.FastJson.dll
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton_FastJson_Sources__v0.9_.zip
Apolyton.FastJson.csproj.user
Diagram.cd
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
build.force
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.vshost.exe
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton_FastJson_Sources__v1.0_.zip
Apolyton.FastJson.csproj.user
Apolyton.FastJson.Silverlight.csproj.user
Apolyton.FastJson.v11.suo
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.dll
Json.cd
JsonParameters.cd
TypeDescriptors.cd
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.FastJson.dll
Apolyton.FastJson.dll
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.dll
build.force
Apolyton.FastJSON.Tests.dll
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
Apolyton.FastJson.dll
consoletest.exe
fastJSON.dll
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
using System;
using System.Diagnostics;
using System.Linq;
using Apolyton.FastJson.Registry;
using Apolyton.FastJson.Serialization;
using System.Globalization;

namespace Apolyton.FastJson.Data
{
    /// <summary>
    /// Represents a json primitive value.
    /// </summary>
    public sealed class JsonPrimitive : IJsonValue, IConvertible
    {
        private readonly JsonRegistry _registry;
        private readonly String _localValue;
        private object _typedValue;
        private DeserializationHandler _deserializationHandler;
        
        /// <summary>
        /// Defines the Info property for this json primitive.
        /// </summary>
        internal JsonPropertyInfo FieldInfo;

        #region Constructors

        /// <summary>
        /// Creates an instance of the type.
        /// </summary>
        /// <param name="localValue">The string representation of the Json Primitive.</param>
        ///<remarks>Primarily used for unit tests.</remarks>
        internal JsonPrimitive(String localValue)
        {
            this._deserializationHandler = null;
            this._registry = null;
            this._localValue = localValue;
            this._typedValue = null;

            this.FieldInfo = JsonPropertyInfo.Empty;
        }

        /// <summary>
        /// Creates an instance of the type.
        /// </summary>
        /// <param name="boolean"></param>
        internal JsonPrimitive(bool boolean)
        {
            this._deserializationHandler = null;
            this._registry = null;
            this._localValue = null;
            this._typedValue = boolean;

            this.FieldInfo = JsonPropertyInfo.Empty;
        }

        /// <summary>
        /// Creates an instance of the primitive.
        /// </summary>
        /// <param name="localValue">The string representation of the Json Primitive.</param>
        /// <param name="registry">The registry which is used to resolve the custom deserializer, if required.</param>
        internal JsonPrimitive(String localValue, JsonRegistry registry)
        {
            this._deserializationHandler = null;
            this._registry = registry;
            this._localValue = localValue;
            this._typedValue = null;

            this.FieldInfo = JsonPropertyInfo.Empty;
        }

        #endregion

        /// <summary>
        /// Gets the json primitive type.
        /// </summary>
        public JsonType Type
        {
            get { return JsonType.JsonPrimitive; }
        }

        /// <summary>
        /// Returns json primitive.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return "JsonPrimitive";
        }

        #region Explicit operators

        /// <summary>
        /// Returns an int representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator int(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToInt32(null);
        }

        /// <summary>
        /// Returns an int representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator short(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToInt16(null);
        }

        /// <summary>
        /// Returns an string representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator String(JsonPrimitive primitive)
        {
            if (primitive._typedValue == null)
            {
                primitive._typedValue = Deserialization.ChangeType(primitive._localValue, typeof(String));
            }

            return (string)primitive._typedValue;
        }

        /// <summary>
        /// Returns an bool representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator bool(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToBoolean(null);
        }

        /// <summary>
        /// Returns an double representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator double(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToDouble(null);
        }

        /// <summary>
        /// Returns an double representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator float(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToSingle(null);
        }

        /// <summary>
        /// Returns an double representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator DateTime(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToDateTime(null);
        }

        /// <summary>
        /// Returns an timespan representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator TimeSpan(JsonPrimitive primitive)
        {
            if (primitive._typedValue == null)
            {
                primitive._typedValue = TimeSpan.Parse(primitive._localValue);
            }

            return (TimeSpan)primitive._typedValue;
        }

        /// <summary>
        /// Returns an guid representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator Guid(JsonPrimitive primitive)
        {
            if (primitive._typedValue == null)
            {
                primitive._typedValue = Deserialization.CreateGuid(primitive._localValue);
            }

            return (Guid)primitive._typedValue;
        }

        /// <summary>
        /// Returns a long representing the json primitive.
        /// </summary>
        /// <param name="primitive"></param>
        /// <returns></returns>
        public static explicit operator long(JsonPrimitive primitive)
        {
            return ((IConvertible)primitive).ToInt64(null);
        }

        #endregion

        #region Accessor Members

        /// <summary>
        /// Gets the value as string.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public string AsString
        {
            get { return (String)this; }
        }

        /// <summary>
        /// Gets the value as integer.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public int AsInteger
        {
            get { return (int)this; }
        }


        /// <summary>
        /// Gets the value as bool.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public bool AsBool
        {
            get { return (bool)this; }
        }

        /// <summary>
        /// Gets the value as double.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public Double AsDouble
        {
            get { return (Double)this; }
        }

        /// <summary>
        /// Gets the value as float.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public float AsFloat
        {
            get { return (float)this; }
        }

        /// <summary>
        /// Gets the value as date time.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public DateTime AsDateTime
        {
            get { return (DateTime)this; }
        }

        /// <summary>
        /// Gets the value as time span.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public TimeSpan AsTimeSpan
        {
            get { return (TimeSpan)this; }
        }

        /// <summary>
        /// Gets the value as guid.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public Guid AsGuid
        {
            get { return (Guid)this; }
        }

        /// <summary>
        /// Gets the value as long.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public long AsLong
        {
            get { return (long)this; }
        }

        /// <summary>
        /// Gets the value as single.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public float AsSingle
        {
            get { return (float)this; }
        }

        /// <summary>
        /// Gets the value as short.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public short AsShort
        {
            get { return (short)this; }
        }

        #endregion

        #region IConvertible Members

        TypeCode IConvertible.GetTypeCode()
        {
            return TypeCode.Boolean | TypeCode.Char | TypeCode.DateTime | TypeCode.Decimal | TypeCode.Double | TypeCode.Int16 | TypeCode.Int32 | TypeCode.Int64
                | TypeCode.String | TypeCode.UInt16 | TypeCode.UInt32 | TypeCode.UInt64;
        }

        byte IConvertible.ToByte(IFormatProvider provider)
        {
            throw new NotSupportedException();
        }

        bool IConvertible.ToBoolean(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = bool.Parse(_localValue);
            }

            return (bool)_typedValue;
        }

        char IConvertible.ToChar(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = _localValue.ToCharArray().Single();
            }

            return (char)_typedValue;
        }

        DateTime IConvertible.ToDateTime(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                if (FieldInfo != JsonPropertyInfo.Empty)
                {
                    _typedValue = Deserialization.CreateDateTime(_localValue, FieldInfo.DateTimeFormat, FieldInfo.DateTimeKind);
                }
                else
                {
                    _typedValue = Deserialization.CreateDateTime(_localValue, DateTimeKind.Unspecified);
                }
            }

            return (DateTime)_typedValue;
        }

        decimal IConvertible.ToDecimal(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = decimal.Parse(_localValue);
            }

            return (decimal)_typedValue;
        }

        double IConvertible.ToDouble(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = double.Parse(_localValue);
            }

            return (Double)_typedValue;
        }

        short IConvertible.ToInt16(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = Int16.Parse(_localValue);
            }

            return (short)_typedValue;
        }

        int IConvertible.ToInt32(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = Int32.Parse(_localValue);
            }

            return (int)_typedValue;
        }

        long IConvertible.ToInt64(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = Deserialization.CreateLong(_localValue);
            }

            return (long)_typedValue;
        }

        sbyte IConvertible.ToSByte(IFormatProvider provider)
        {
            throw new NotImplementedException();
        }

        float IConvertible.ToSingle(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = float.Parse(_localValue);
            }

            return (float)_typedValue;
        }

        string IConvertible.ToString(IFormatProvider provider)
        {
            return (String)this;
        }

        object IConvertible.ToType(Type conversionType, IFormatProvider provider)
        {
            if (conversionType == typeof(Guid))
            {
                return (Guid)this;
            }
            else if (conversionType == typeof(TimeSpan))
            {
                return (TimeSpan)this;
            }
            else if (conversionType.IsEnum)
            {
                if (_typedValue != null) { return _typedValue; }
                else { _typedValue = Deserialization.CreateEnum(conversionType, _localValue); return _typedValue; }
            }
            else if (conversionType == typeof(byte[]))
            {
                if (_typedValue != null) { return _typedValue; }
                else { _typedValue = Deserialization.CreateByteArray(_localValue, typeof(byte[])); return _typedValue; }
            }
            else if (_registry != null && _registry._customDeserializer.TryGetValue(conversionType, out _deserializationHandler))
            {
                if (_typedValue != null) { return _typedValue; }
                else { _typedValue = _deserializationHandler(_localValue); return _typedValue; }
            }
            else if (conversionType == typeof(int?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToInt32(null); }
                else { return null; }
            }
            else if (conversionType == typeof(double?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToDouble(null); }
                else { return null; }
            }
            else if (conversionType == typeof(Guid?))
            {
                if (_localValue != null) { return (Guid)this; }
                else { return null; }
            }
            else if (conversionType == typeof(short?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToInt16(null); }
                else { return null; }
            }
            else if (conversionType == typeof(long?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToInt64(null); }
                else { return null; }
            }
            else if (conversionType == typeof(DateTime?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToDateTime(null); }
                else { return null; }
            }
            else if (conversionType == typeof(TimeSpan?))
            {
                if (_localValue != null) { return (TimeSpan)this; }
                else { return null; }
            }
            else if (conversionType == typeof(float?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToSingle(null); }
                else { return null; }
            }
            else if (conversionType == typeof(uint?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToUInt32(null); }
                else { return null; }
            }
            else if (conversionType == typeof(ushort?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToUInt16(null); }
                else { return null; }
            }
            else if (conversionType == typeof(ulong?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToUInt64(null); }
                else { return null; }
            }
            else if (conversionType == typeof(decimal?))
            {
                if (_localValue != null) { return ((IConvertible)this).ToDecimal(null); }
                else { return null; }
            }
            else
            {
                throw new NotSupportedException("Cannot convert json primitive to: " + conversionType.ToString());
            }
        }

        ushort IConvertible.ToUInt16(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = ushort.Parse(_localValue);
            }

            return (ushort)_typedValue;
        }

        uint IConvertible.ToUInt32(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = UInt32.Parse(_localValue);
            }

            return (UInt32)_typedValue;
        }

        ulong IConvertible.ToUInt64(IFormatProvider provider)
        {
            if (_typedValue == null)
            {
                _typedValue = UInt64.Parse(_localValue);
            }

            return (UInt64)_typedValue;
        }

        #endregion

        #region Not supported members

        int IJsonValue.Count
        {
            get { throw new NotSupportedException(); }
        }

        IJsonValue IJsonValue.this[int i]
        {
            get { throw new NotSupportedException(); }
        }

        IJsonValue IJsonValue.this[string key]
        {
            get { throw new NotSupportedException(); }
        }

        #endregion
    }
}

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)

Share

About the Author

Aron Kovacs

Germany Germany
No Biography provided

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 28 Aug 2013
Article Copyright 2012 by Aron Kovacs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid