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

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.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;

namespace Apolyton.FastJson.Serialization
{
    /// <summary>
    /// This class contains deserialization helpers 
    /// </summary>
    internal static class Deserialization
    {
        private static HashSet<Type> internallyManagedTypes;

        static Deserialization()
        {
            internallyManagedTypes = new HashSet<Type>();
            internallyManagedTypes.Add(typeof(DBNull));
            internallyManagedTypes.Add(typeof(string));
            internallyManagedTypes.Add(typeof(char));
            internallyManagedTypes.Add(typeof(Guid));
            internallyManagedTypes.Add(typeof(bool));
            internallyManagedTypes.Add(typeof(int));
            internallyManagedTypes.Add(typeof(long));
            internallyManagedTypes.Add(typeof(double));
            internallyManagedTypes.Add(typeof(decimal));
            internallyManagedTypes.Add(typeof(float));
            internallyManagedTypes.Add(typeof(byte));
            internallyManagedTypes.Add(typeof(short));
            internallyManagedTypes.Add(typeof(sbyte));
            internallyManagedTypes.Add(typeof(ushort));
            internallyManagedTypes.Add(typeof(uint));
            internallyManagedTypes.Add(typeof(ulong));
            internallyManagedTypes.Add(typeof(DateTime));
            internallyManagedTypes.Add(typeof(TimeSpan));
            internallyManagedTypes.Add(typeof(IDictionary));
            internallyManagedTypes.Add(typeof(byte[]));
            internallyManagedTypes.Add(typeof(IEnumerable));
            internallyManagedTypes.Add(typeof(Enum));

#if DESKTOP
            internallyManagedTypes.Add(typeof(System.Data.DataSet));
            internallyManagedTypes.Add(typeof(System.Data.DataTable));
#endif
        }

        /// <summary>
        /// Returns true, if the given type is supported by FastJson itself.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        internal static bool IsTypeInternallyManaged(Type type)
        {
            return internallyManagedTypes.Contains(type);
        }

        /// <summary>
        /// Changes the value type to the expected one.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="convertTo"></param>
        /// <returns></returns>
        internal static object ChangeType(object value, Type convertTo)
        {
            if (convertTo == typeof(int))
                return (int)((long)value);

            else if (convertTo == typeof(long))
                return (long)value;

            else if (convertTo == typeof(string))
                return (string)value;

            else if (convertTo == typeof(Guid))
                return CreateGuid((string)value);

            else if (convertTo == typeof(TimeSpan))
                return CreateTimeSpan((String)value);

            else if (convertTo.IsEnum)
                return CreateEnum(convertTo, (string)value);

            return Convert.ChangeType(value, convertTo, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// Returns an enum value for the given type.
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="enumString"></param>
        /// <returns></returns>
        internal static object CreateEnum(Type enumType, string enumString)
        {
            // TODO : optimize create enum
            return Enum.Parse(enumType, enumString, true);
        }

        /// <summary>
        /// Returns a guid for the given string.
        /// </summary>
        /// <param name="base64OrGuidString"></param>
        /// <returns></returns>
        internal static Guid CreateGuid(string base64OrGuidString)
        {
            if (base64OrGuidString.Length > 30)
            {
                return new Guid(base64OrGuidString);
            }
            else
            {
                return new Guid(Convert.FromBase64String(base64OrGuidString));
            }
        }

        /// <summary>
        /// Returns a time span for the given string.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        internal static TimeSpan CreateTimeSpan(String s)
        {
            return TimeSpan.Parse(s);
        }

        /// <summary>
        /// Returns the parsed date time.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        internal static DateTime CreateDateTime(string value, DateTimeKind expectedKind)
        {
            //                   0123456789012345678
            // datetime format = yyyy-MM-dd HH:mm:ss
            int year = (int)CreateLong(value.Substring(0, 4));
            int month = (int)CreateLong(value.Substring(5, 2));
            int day = (int)CreateLong(value.Substring(8, 2));
            int hour = (int)CreateLong(value.Substring(11, 2));
            int min = (int)CreateLong(value.Substring(14, 2));
            int sec = (int)CreateLong(value.Substring(17, 2));

            if (!value.EndsWith("Z"))
            {
                return new DateTime(year, month, day, hour, min, sec);
            }
            else
            {
                if (expectedKind == DateTimeKind.Local)
                {
                    return new DateTime(year, month, day, hour, min, sec, DateTimeKind.Utc).ToLocalTime();
                }
                else
                {
                    return new DateTime(year, month, day, hour, min, sec, DateTimeKind.Utc);
                }
            }
        }

        /// <summary>
        /// Returns a long value for the given string.
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        internal static long CreateLong(string s)
        {
            long num = 0;
            bool neg = false;

            foreach (char cc in s)
            {
                if (cc == '-')
                {
                    neg = true;
                }
                else if (cc == '+')
                {
                    neg = false;
                }
                else
                {
                    num *= 10;
                    num += (int)(cc - '0');
                }
            }

            return neg ? -num : num;
        }

        internal static IEnumerable<Byte> CreateByteArray(string base64ByteString, Type targetType)
        {
            if (targetType.IsArray || typeof(IEnumerable).IsAssignableFrom(targetType))
            {
                return Convert.FromBase64String(base64ByteString);
            }

            throw new SerializationException("Cannot create instance for type " + targetType);
        }
    }
}

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.140926.1 | Last Updated 28 Aug 2013
Article Copyright 2012 by Aron Kovacs
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid