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

APJSON

, 28 Aug 2013 CPOL
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.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Apolyton.FastJson.Tests.Helpers;
using Apolyton.FastJson.TestsHelpers.StandardTypes;
using Apolyton.FastJson;

namespace Apolyton.FastJson.Tests
{
    [TestClass]
    public class JsonTests
    {
        #region JsonObject Method Tests

        [TestMethod]
        public void Json_ReadObject_IntClass()
        {
            String jsonString = "{\"Integer\":1,\"NullableInteger\":2}";

            var result = (IntClass)Json.Current.ReadObject(jsonString, typeof(IntClass));
            Assert.AreEqual(1, result.Integer);
            Assert.AreEqual(2, result.NullableInteger);
        }

        [TestMethod]
        public void Json_Parameter_IgnoreCaseOnDeserialize()
        {
            Assert.Inconclusive("Not Implemented");
            String jsonString = "{\"integer\":1,\"nullableInteger\":2}";

            var result = (IntClass)Json.Current.ReadObject(jsonString, typeof(IntClass));
            Assert.AreEqual(1, result.Integer);
            Assert.AreEqual(2, result.NullableInteger);
        }

        [TestMethod]
        public void Json_ReadObject_UnsignedIntClass()
        {
            String jsonString = "{\"Integer\":1,\"NullableInteger\":2}";

            var result = (UnsignedIntClass)Json.Current.ReadObject(jsonString, typeof(UnsignedIntClass));
            Assert.AreEqual((uint)1, result.Integer);
            Assert.AreEqual((uint)2, result.NullableInteger);
        }

        [TestMethod]
        public void Json_ReadObject_LongClass()
        {
            String jsonString = "{\"Long\":1,\"NullableLong\":2}";

            var result = (LongClass)Json.Current.ReadObject(jsonString, typeof(LongClass));
            Assert.AreEqual((long)1, result.Long);
            Assert.AreEqual((long)2, result.NullableLong);
        }

        [TestMethod]
        public void Json_ReadObject_TimeSpanClass()
        {
            String jsonString = "{\"TimeSpan\":\"01:02:03\",\"NullableTimeSpan\":\"04:05:06\"}";

            var result = (TimespanClass)Json.Current.ReadObject(jsonString, typeof(TimespanClass));
            Assert.AreEqual(new TimeSpan(1, 2, 3), result.TimeSpan);
            Assert.AreEqual(new TimeSpan(4, 5, 6), result.NullableTimeSpan);
        }

        [TestMethod]
        public void Json_ReadObject_UnsignedLongClass()
        {
            String jsonString = "{\"Long\":1,\"NullableLong\":2}";

            var result = (UnsignedLongClass)Json.Current.ReadObject(jsonString, typeof(UnsignedLongClass));
            Assert.AreEqual((ulong)1, result.Long);
            Assert.AreEqual((ulong)2, result.NullableLong);
        }

        [TestMethod]
        public void Json_ReadObject_ShortClass()
        {
            String jsonString = "{\"Short\":1,\"NullableShort\":2}";

            var result = (ShortClass)Json.Current.ReadObject(jsonString, typeof(ShortClass));
            Assert.AreEqual((short)1, result.Short);
            Assert.AreEqual((short)2, result.NullableShort);
        }

        [TestMethod]
        public void Json_ReadObject_UnsignedShortClass()
        {
            String jsonString = "{\"Short\":1,\"NullableShort\":2}";

            var result = (UnsignedShortClass)Json.Current.ReadObject(jsonString, typeof(UnsignedShortClass));
            Assert.AreEqual((ushort)1, result.Short);
            Assert.AreEqual((ushort)2, result.NullableShort);
        }

        [TestMethod]
        public void Json_ReadObject_DoubleClass()
        {
            String jsonString = "{\"Double\":1,\"NullableDouble\":2}";

            var result = (DoubleClass)Json.Current.ReadObject(jsonString, typeof(DoubleClass));
            Assert.AreEqual(1, result.Double);
            Assert.AreEqual(2, result.NullableDouble);
        }

        [TestMethod]
        public void Json_ReadObject_StringClass()
        {
            String jsonString = "{\"String\":\"A\"}";

            var result = (StringClass)Json.Current.ReadObject(jsonString, typeof(StringClass));
            Assert.AreEqual("A", result.String);
        }

        /// <summary>
        /// Verifies that the proper exception is thrown when the format doesn't match.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void Json_ReadObject_DateTimeClass_CustomFormat_InvalidFormat()
        {
            String jsonString = "{\"CustomFormatDateTime\":\"1955-03-04 09:22:33Z\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
        }

        /// <summary>
        /// Verifies that the 'zulu' trick is not supported when using custom format.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void Json_ReadObject_DateTimeClass_CustomFormat_InvalidFormat2()
        {
            String jsonString = "{\"CustomFormatDateTime\":\"1955.03.04Z\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
        }

        /// <summary>
        /// Verifies that the 'zulu' trick is not supported when using custom format.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass_CustomFormat()
        {
            String jsonString = "{\"CustomFormatDateTime\":\"1955.03.04\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(DateTimeKind.Local, result.CustomFormatDateTime.Kind, "The kind should be local, but no conversion is done, since the format is custom");
            Assert.AreEqual(new DateTime(1955, 3, 4, 0, 0, 0), result.CustomFormatDateTime, "The custom date has been parsed badly.");
        }

        /// <summary>
        /// Verifies that the date time values are properly read.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass()
        {
            String jsonString = "{\"DateTime\":\"1955-03-04 09:22:33Z\",\"NullableDateTime\":\"1956-04-05 08:22:33Z\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(DateTimeKind.Utc, result.DateTime.Kind);
            Assert.AreEqual(DateTimeKind.Utc, result.NullableDateTime.Value.Kind);
            Assert.AreEqual(new DateTime(1955, 3, 4, 9, 22, 33), result.DateTime, "DateTime.Kind is unspecified; no transformation should have been done.");
            Assert.AreEqual(new DateTime(1956, 4, 5, 8, 22, 33), result.NullableDateTime, "DateTime.Kind is unspecified; no transformation should have been done.");
        }

        /// <summary>
        /// Verifies that the local date time which is receiving a utc value is properly converted.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass_DateTimeOptions_Local_UtcInput()
        {
            String jsonString = "{\"LocalDateTime\":\"1955-03-04 09:22:33Z\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(DateTimeKind.Local, result.LocalDateTime.Kind);
            Assert.AreEqual(new DateTime(1955, 3, 4, 10, 22, 33), result.LocalDateTime, "Given value was in utc, property declares local time: should have moved to local time.");
        }

        /// <summary>
        /// Verifies that the local date time which is receiving an unspecified date kind value is not converted.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass_DateTimeOptions_Local_UnspecifiedInput()
        {
            String jsonString = "{\"LocalDateTime\":\"1955-03-04 09:22:33\"}"; // Note the missing 'z' which indicates Utc kind

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(DateTimeKind.Local, result.LocalDateTime.Kind);
            Assert.AreEqual(new DateTime(1955, 3, 4, 9, 22, 33), result.LocalDateTime, "DateTime.Kind is unspecified; no transformation should have been done.");
        }

        /// <summary>
        /// Verifies that the utc date which receives a utc input is not converted.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass_DateTimeOptions_Utc_UtcInput()
        {
            String jsonString = "{\"UtcDateTime\":\"1955-03-04 09:22:33Z\"}";

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(new DateTime(1955, 3, 4, 9, 22, 33), result.UtcDateTime, "Given value was in utc, property declares utc time: should not have moved to utc time.");
            Assert.AreEqual(DateTimeKind.Utc, result.UtcDateTime.Kind);
        }

        /// <summary>
        /// Verifies that the utc date which receives an unspecified date kind value is not converted.
        /// </summary>
        [TestMethod]
        public void Json_ReadObject_DateTimeClass_DateTimeOptions_Utc_UnspecifiedInput()
        {
            String jsonString = "{\"UtcDateTime\":\"1955-03-04 09:22:33\"}";// Note the missing 'z' which indicates Utc kind

            var result = (DateTimeClass)Json.Current.ReadObject(jsonString, typeof(DateTimeClass));
            Assert.AreEqual(DateTimeKind.Utc, result.UtcDateTime.Kind);
            Assert.AreEqual(new DateTime(1955, 3, 4, 9, 22, 33), result.UtcDateTime, "Given value was not specified, property declares utc time: should have moved to utc time according to local time zone.");
        }

        [TestMethod]
        public void Json_ReadObject_ByteArrayClass()
        {
            String jsonString = "{\"ByteArray\":\"AQID\",\"ByteEnumeration\":null}";

            var result = (ByteArrayClass)Json.Current.ReadObject(jsonString, typeof(ByteArrayClass));
            Assert.AreEqual(1, result.ByteArray[0]);
            Assert.AreEqual(2, result.ByteArray[1]);
            Assert.AreEqual(3, result.ByteArray[2]);
        }

        [TestMethod]
        public void Json_ReadObject_ByteEnumerationClass()
        {
            String jsonString = "{\"ByteArray\":null,\"ByteEnumeration\":\"AQID\"}";

            var result = (ByteArrayClass)Json.Current.ReadObject(jsonString, typeof(ByteArrayClass));
            Assert.IsTrue(new byte[] { 1, 2, 3 }.SequenceEqual(result.ByteEnumeration));
        }

        [TestMethod]
        public void Json_ReadObject_DictionaryClass()
        {
            String jsonString = "{\"Dictionary\":[{\"k\":1,\"v\":2},{\"k\":3,\"v\":5}]}";

            var result = (DictionaryClass<int, int>)Json.Current.ReadObject(jsonString, typeof(DictionaryClass<int, int>));
            Assert.AreEqual(2, result.Dictionary[1]);
            Assert.AreEqual(5, result.Dictionary[3]);
        }

        [TestMethod]
        public void Json_ReadObject_Enumeration()
        {
            String jsonString = "{\"Enumeration\":[1,2,3]}";

            var result = (EnumerableClass)Json.Current.ReadObject(jsonString, typeof(EnumerableClass));
            Assert.AreEqual((Int64)1, result.Enumeration.OfType<Object>().ElementAt(0));
            Assert.AreEqual((Int64)2, result.Enumeration.OfType<Object>().ElementAt(1));
        }

        #endregion

        [TestMethod]
        public void Json_ToJson_AnonymousType()
        {
            var q = new { Name = "asassa", Address = "asadasd", Age = 12 };
            string sq = Json.Current.ToJson(q, new JsonParameters { EnableAnonymousTypes = true });
            Console.WriteLine(sq);
        }
    }
}

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