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
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_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.dll
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
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_Binaries__v1.0_.zip
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
consoletest.vshost.exe.manifest
fastJSON.dll
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
fastJSON.dll
fastJSON.pdb
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
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
Common
Data
Diagram.cd
obj
Debug
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Release x64
Apolyton.FastJson.dll
Release x86
Apolyton.FastJson.dll
Release
Apolyton.FastJson.dll
Properties
Registry
Serialization
Apolyton.FastJson.Tests
bin
Release
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Data
Helpers
ComplexTypes
ParameterRelated
Polymorphism
RegistryHelpers
StandardTypes
obj
Debug
Apolyton.FastJSON.Tests.dll
build.force
Release (x86, CUSTOM TYPES)
build.force
Release
Apolyton.FastJSON.Tests.dll
Properties
Registry
Serialization
Console Test
bin
Debug
Apolyton.FastJson.dll
consoletest.exe
consoletest.vshost.exe
fastJSON.dll
DataObjects
obj
Debug
build.force
consoletest.exe
Release x64
consoletest.exe
Release x86
consoletest.exe
Release
build.force
consoletest.exe
Apolyton_FastJson_Sources__v0.93_.zip
Debug
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
x86
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
x86
Debug
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.dll
Apolyton.FastJSON.Tests.dll
build.force
consoletest.vshost.exe
consoletest.exe
consoletest.exe
consoletest.exe
consoletest.exe
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.pdb
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
Json.cd
JsonParameters.cd
TypeDescriptors.cd
Apolyton.FastJson.csprojResolveAssemblyReference.cache
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
build.force
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.pdb
consoletest.vshost.exe
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
fastJSON.dll
fastJSON.pdb
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
fastJSON.dll
fastJSON.pdb
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
Release (x86, CUSTOM TYPES)
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
TempPE
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apolyton.FastJson.Serialization;
using System.Globalization;
using System.Diagnostics;

namespace Apolyton.FastJson.Data
{
    /// <summary>
    /// Represents a json primitive value.
    /// </summary>
    public struct JsonPrimitive : IJsonValue, IConvertible
    {
        private readonly String localValue;
        private object typedValue;

        /// <summary>
        /// Creates an instance of the type.
        /// </summary>
        /// <param name="localValue"></param>
        public JsonPrimitive(String localValue)
        {
            this.localValue = localValue;
            this.typedValue = null;
        }

        /// <summary>
        /// Creates an instance of the type.
        /// </summary>
        /// <param name="localValue"></param>
        public JsonPrimitive(bool boolean)
        {
            this.localValue = null;
            this.typedValue = boolean;
        }

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

        /// <summary>
        /// Gets the value as string.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public string AsString
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = Deserialization.ChangeType(localValue, typeof(String));
                }

                return (string)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as integer.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public int AsInteger
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = Int32.Parse(localValue);
                }

                return (int)typedValue;
            }
        }


        /// <summary>
        /// Gets the value as bool.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public bool AsBool
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = bool.Parse(localValue);
                }

                return (bool)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as double.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public Double AsDouble
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = double.Parse(localValue);
                }

                return (Double)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as float.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public float AsFloat
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = float.Parse(localValue);
                }

                return (float)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as date time.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public DateTime AsDateTime
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = Deserialization.CreateDateTime(localValue);
                }

                return (DateTime)typedValue;
            }
        }

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

                return (TimeSpan)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as guid.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public Guid AsGuid
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = Deserialization.CreateGuid(localValue);
                }

                return (Guid)typedValue;
            }
        }

        /// <summary>
        /// Gets the value as long.
        /// </summary>
        [DebuggerDisplay("Click to expand. This operation has side effects.")]
        public long AsLong
        {
            get
            {
                if (typedValue == null)
                {
                    typedValue = Deserialization.CreateLong(localValue);
                }

                return (long)typedValue;
            }
        }

        public int Count
        {
            get { throw new NotSupportedException(); }
        }

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

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

        #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;
        }

        bool IConvertible.ToBoolean(IFormatProvider provider)
        {
            return AsBool;
        }

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

        char IConvertible.ToChar(IFormatProvider provider)
        {
            if (typedValue == null)
            {
                typedValue = localValue.ToCharArray().First();
            }

            return (char)typedValue;
        }

        DateTime IConvertible.ToDateTime(IFormatProvider provider)
        {
            return AsDateTime;
        }

        decimal IConvertible.ToDecimal(IFormatProvider provider)
        {
            return AsInteger;
        }

        double IConvertible.ToDouble(IFormatProvider provider)
        {
            return AsDouble;
        }

        short IConvertible.ToInt16(IFormatProvider provider)
        {
            return (short)AsInteger;
        }

        int IConvertible.ToInt32(IFormatProvider provider)
        {
            return AsInteger;
        }

        long IConvertible.ToInt64(IFormatProvider provider)
        {
            return AsLong;
        }

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

        float IConvertible.ToSingle(IFormatProvider provider)
        {
            return AsFloat;
        }

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

        object IConvertible.ToType(Type conversionType, IFormatProvider provider)
        {
            if (conversionType == typeof(Guid))
            {
                return AsGuid;
            }
            else if (conversionType == typeof(TimeSpan))
            {
                return AsTimeSpan;
            }
            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 (conversionType == typeof(int?))
            {
                if (localValue != null) { return AsInteger; }
                else { return null; }
            }
            else if (conversionType == typeof(double?))
            {
                if (localValue != null) { return AsDouble; }
                else { return null; }
            }
            else if (conversionType == typeof(Guid?))
            {
                if (localValue != null) { return AsGuid; }
                else { return null; }
            }
            else if (conversionType == typeof(short?))
            {
                if (localValue != null) { return (short)AsInteger; }
                else { return null; }
            }
            else if (conversionType == typeof(long?))
            {
                if (localValue != null) { return AsLong; }
                else { return null; }
            }
            else if (conversionType == typeof(DateTime?))
            {
                if (localValue != null) { return AsDateTime; }
                else { return null; }
            }
            else if (conversionType == typeof(TimeSpan?))
            {
                if (localValue != null) { return AsTimeSpan; }
                else { return null; }
            }
            else if (conversionType == typeof(float?))
            {
                if (localValue != null) { return AsFloat; }
                else if (localValue == null) { return null; }
                else { return null; }
            }
            else if (conversionType == typeof(uint?))
            {
                if (typedValue != null) { return typedValue; }
                else if (localValue == null) { return null; }
                else { typedValue = uint.Parse(localValue); return typedValue; }
            }
            else if (conversionType == typeof(ushort?))
            {
                if (typedValue != null) { return typedValue; }
                else if (localValue == null) { return null; }
                else { typedValue = ushort.Parse(localValue); return typedValue; }
            }
            else if (conversionType == typeof(ulong?))
            {
                if (typedValue != null) { return typedValue; }
                else if (localValue == null) { return null; }
                else { typedValue = ulong.Parse(localValue); return typedValue; }
            }
            else if (conversionType == typeof(decimal))
            {
                if (typedValue != null) { return typedValue; }
                else { typedValue = decimal.Parse(localValue); return typedValue; }
            }
            else if (conversionType == typeof(decimal?))
            {
                if (typedValue != null) { return typedValue; }
                else if (localValue == null) { return null; }
                else { typedValue = decimal.Parse(localValue); return typedValue; }
            }
            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
    }
}

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)

About the Author

Aron Kovacs

Germany Germany
No Biography provided

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