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
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
Apolyton.FastJson.pdb
consoletest.exe
consoletest.pdb
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.Json.Silverlight.pdb
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
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
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.Tests.pdb
Apolyton.FastJson.dll
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
build.force
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
consoletest.exe
consoletest.pdb
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.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Release x64
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
Release x86
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
Release
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.csproj.user
Apolyton.FastJson.Silverlight.csproj.user
Apolyton.FastJson.v11.suo
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
Release
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
Diagrams
Json.cd
JsonParameters.cd
TypeDescriptors.cd
Apolyton.FastJson.dll
Apolyton.FastJson.pdb
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferencesInput.cache
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.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
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.dll
Apolyton.Json.Silverlight.dll
Apolyton.Json.Silverlight.pdb
DesignTimeResolveAssemblyReferencesInput.cache
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
build.force
DesignTimeResolveAssemblyReferencesInput.cache
Apolyton.FastJson.Tests.csprojResolveAssemblyReference.cache
Apolyton.FastJSON.Tests.dll
Apolyton.FastJSON.Tests.pdb
DesignTimeResolveAssemblyReferencesInput.cache
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
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
DesignTimeResolveAssemblyReferencesInput.cache
consoletest.csprojResolveAssemblyReference.cache
consoletest.exe
consoletest.pdb
DesignTimeResolveAssemblyReferencesInput.cache
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.Runtime.Serialization;
using Apolyton.FastJson.Tests.Helpers;
using Apolyton.FastJson.Tests.Helpers.ComplexTypes;
using Apolyton.FastJson.Tests.Helpers.ParameterRelated;
using Apolyton.FastJson.TestsHelpers.ParameterRelated;
using Apolyton.FastJson.TestsHelpers.StandardTypes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Apolyton.FastJson.Tests.Helpers.StandardTypes;
using Apolyton.FastJson.Registry;
using Apolyton.FastJson.Tests.Helpers.Polymorphism;

namespace Apolyton.FastJson.Tests
{
    [TestClass]
    public class JsonSerializerTests
    {
        private JsonParameters CreateTestParameters()
        {
            return new JsonParameters()
            {
                SerializeNullValues = false,
                UseUtcDateTime = true,
                UseTypeExtension = false,
                UseGlobalTypes = false
            };
        }

        [TestInitialize]
        public void ResetRegistry()
        {
            //JsonRegistry.Current.Reset();
        }

        [TestMethod]
        public void JsonSerializer_Constructor()
        {
            JsonSerializer serializer = new JsonSerializer(new JsonParameters());
        }

        #region Serializing values.

        [TestMethod]
        public void JsonSerializer_Integer()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            IntClass given = new IntClass()
            {
                Integer = 1,
                NullableInteger = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Integer\":1,\"NullableInteger\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_UnsignedInteger()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            UnsignedIntClass given = new UnsignedIntClass()
            {
                Integer = 1,
                NullableInteger = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Integer\":1,\"NullableInteger\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Double()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            DoubleClass given = new DoubleClass()
            {
                Double = 1.1,
                NullableDouble = 2.2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Double\":1.1,\"NullableDouble\":2.2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Short()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            ShortClass given = new ShortClass()
            {
                Short = 1,
                NullableShort = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Short\":1,\"NullableShort\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_UnsignedShort()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            UnsignedShortClass given = new UnsignedShortClass()
            {
                Short = 1,
                NullableShort = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Short\":1,\"NullableShort\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Long()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            LongClass given = new LongClass()
            {
                Long = 1,
                NullableLong = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Long\":1,\"NullableLong\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_UnsignedLong()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            UnsignedLongClass given = new UnsignedLongClass()
            {
                Long = 1,
                NullableLong = 2
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Long\":1,\"NullableLong\":2}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_DateTime()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            DateTimeClass given = new DateTimeClass()
            {
                DateTime = new DateTime(1955, 3, 4, 10, 22, 33),
                NullableDateTime = new DateTime(1956, 4, 5, 10, 22, 33)
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual(TimeSpan.FromHours(1), TimeZone.CurrentTimeZone.GetUtcOffset(given.DateTime));
            Assert.AreEqual(TimeSpan.FromHours(2), TimeZone.CurrentTimeZone.GetUtcOffset((DateTime)given.NullableDateTime));
            Assert.IsTrue(CreateTestParameters().UseUtcDateTime, "Supposed to use the utc date.");

            Assert.AreEqual("{\"DateTime\":\"1955-03-04 10:22:33Z\",\"NullableDateTime\":\"1956-04-05 10:22:33Z\"}", jsonString, 
                "Since the kind of the date time is unknown, no adjustment to UTC should have happened.");
        }

        [TestMethod]
        public void JsonSerializer_DateTimeUtc()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            DateTimeClass given = new DateTimeClass()
            {
                DateTime = new DateTime(1955, 3, 4, 10, 22, 33).ToUniversalTime(),
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual(TimeSpan.FromHours(0), TimeZone.CurrentTimeZone.GetUtcOffset(given.DateTime));
            Assert.AreEqual("{\"DateTime\":\"1955-03-04 09:22:33Z\"}", jsonString, "No additional conversion should have happened, the date was already in utc.");
        }

        [TestMethod]
        public void JsonSerializer_TimeSpan()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            TimespanClass given = new TimespanClass()
            {
                TimeSpan = new TimeSpan(1, 2, 3),
                NullableTimeSpan = new TimeSpan(2, 3, 4)
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"TimeSpan\":\"01:02:03\",\"NullableTimeSpan\":\"02:03:04\"}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Bool()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            BoolClass given = new BoolClass()
            {
                Bool = true,
                NullableBool = true
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Bool\":true,\"NullableBool\":true}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Dictionary()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            DictionaryClass<int, int> given = new DictionaryClass<int, int>();
            given.Dictionary = new Dictionary<int, int>();
            given.Dictionary[1] = 2;
            given.Dictionary[3] = 5;

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Dictionary\":[{\"k\":1,\"v\":2},{\"k\":3,\"v\":5}]}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_StringDictionary()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            DictionaryClass<String, int> given = new DictionaryClass<String, int>();
            given.Dictionary = new Dictionary<String, int>();
            given.Dictionary["A"] = 2;
            given.Dictionary["B"] = 5;

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Dictionary\":{\"A\":2,\"B\":5}}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_String()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            StringClass given = new StringClass()
            {
                String = "A",
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"String\":\"A\"}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_List()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            List<String> list = new List<string>();
            list.Add("A");

            String jsonString = serializer.Serialize(list);
            Assert.AreEqual("[\"A\"]", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Enumeration()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            EnumerableClass given = new EnumerableClass()
            {
                Enumeration = new int[] { 1, 2, 3 },
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Enumeration\":[1,2,3]}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_ByteArray()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            ByteArrayClass given = new ByteArrayClass()
            {
                ByteArray = new byte[] { 1, 2, 3 },
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"ByteArray\":\"AQID\"}", jsonString, "Bytes should be encoded in Base64 String");
        }

        [TestMethod]
        public void JsonSerializer_ByteEnumeration()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            ByteArrayClass given = new ByteArrayClass()
            {
                ByteEnumeration = new List<byte>(new byte[] { 1, 2, 3 }),
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"ByteEnumeration\":\"AQID\"}", jsonString, "Bytes should be encoded in Base64 String");
        }

        [TestMethod]
        public void JsonSerializer_CustomStruct()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            CustomStruct given = new CustomStruct()
            {
                Text = "A"
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Text\":\"A\"}", jsonString);
        }

        #endregion

        #region Parameter Tests

        [TestMethod]
        public void JsonSerializer_Parameter_SerializeNullValues_False()
        {
            JsonParameters parameter = CreateTestParameters();
            parameter.SerializeNullValues = false;

            JsonSerializer serializer = new JsonSerializer(parameter);
            StringClass given = new StringClass()
            {
                String = null,
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_SerializeNullValues_True()
        {
            JsonParameters parameter = CreateTestParameters();
            parameter.SerializeNullValues = true;

            JsonSerializer serializer = new JsonSerializer(parameter);
            StringClass given = new StringClass()
            {
                String = null,
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"String\":null}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_OptOut()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            OptOutClass given = new OptOutClass()
            {
                Value = 1,
                NotSerialized = 2,
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Value\":1}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_OptIn()
        {
            var parameters = CreateTestParameters();
            parameters.SerializationPolicy = SerializationPolicy.PropertyOptIn;

            JsonSerializer serializer = new JsonSerializer(parameters);
            OptInClass given = new OptInClass()
            {
                Value = 1,
                NotSerialized = 2,
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Value\":1}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_ReadOnly_Excluded()
        {
            var parameters = CreateTestParameters();
            parameters.SerializationPolicy = SerializationPolicy.PropertyOptOut;

            JsonSerializer serializer = new JsonSerializer(parameters);
            var given = new ReadOnlyClass(5, 6);

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Value\":5}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_ReadOnly_Included()
        {
            var parameters = CreateTestParameters();
            parameters.SerializationPolicy = SerializationPolicy.PropertyOptOut | SerializationPolicy.IncludeReadOnly;

            JsonSerializer serializer = new JsonSerializer(parameters);
            var given = new ReadOnlyClass(5, 6);

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual("{\"Value\":5,\"ReadOnlyValue\":6}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_Parameter_UseUtcDateTime()
        {
            var parameters = CreateTestParameters();
            parameters.UseUtcDateTime = false;

            JsonSerializer serializer = new JsonSerializer(parameters);
            DateTimeClass given = new DateTimeClass()
            {
                DateTime = new DateTime(1955, 3, 4, 10, 22, 33),
                NullableDateTime = new DateTime(1956, 4, 5, 10, 22, 33)
            };

            String jsonString = serializer.Serialize(given);
            Assert.AreEqual(TimeSpan.FromHours(1), TimeZone.CurrentTimeZone.GetUtcOffset(given.DateTime));
            Assert.AreEqual(TimeSpan.FromHours(2), TimeZone.CurrentTimeZone.GetUtcOffset((DateTime)given.NullableDateTime));
            Assert.IsTrue(CreateTestParameters().UseUtcDateTime, "Supposed to use the utc date.");

            Assert.AreEqual("{\"DateTime\":\"1955-03-04 10:22:33\",\"NullableDateTime\":\"1956-04-05 10:22:33\"}", jsonString, "Although there are different time zones, the serialized output should not reflect the UTC difference");
        }

        #endregion

        #region Custom Types

        [TestMethod]
        public void JsonSerializer_CustomType_Serialize_ByDelegates()
        {
            JsonParameters parameters = CreateTestParameters();
            JsonSerializer serializer = new JsonSerializer(parameters);
            CustomTypeClass customTypeClass = new CustomTypeClass() { Custom = new CustomTypeClass.CustomType() };

            parameters.RegisterCustomType(typeof(CustomTypeClass.CustomType), (obj) => { return "yes"; }, (obj) => { return null; });

            String jsonString = serializer.Serialize(customTypeClass);
            Assert.AreEqual("{\"Custom\":\"yes\"}", jsonString);
        }

        [TestMethod]
        public void JsonSerializer_CustomType_Serialize_ByObject()
        {
            JsonParameters parameters = CreateTestParameters();
            JsonSerializer serializer = new JsonSerializer(parameters);
            CustomTypeClass customTypeClass = new CustomTypeClass() { Custom = new CustomTypeClass.CustomType() };

            parameters.RegisterCustomType(new CustomTypeSerializerFake());

            String jsonString = serializer.Serialize(customTypeClass);
            Assert.AreEqual("{\"Custom\":\"yes\"}", jsonString);
        }

        /// <summary>
        /// Verifies that when the value of the custom type is null, 'null' is serialized.
        /// </summary>
        [TestMethod]
        public void JsonSerializer_CustomType_Serialize_Null()
        {
            JsonParameters parameters = CreateTestParameters();
            parameters.SerializeNullValues = true;

            JsonSerializer serializer = new JsonSerializer(parameters);
            CustomTypeClass customTypeClass = new CustomTypeClass();

            parameters.RegisterCustomType(typeof(CustomTypeClass.CustomType), (obj) => { return "yes"; }, (obj) => { return null; });

            String jsonString = serializer.Serialize(customTypeClass);
            Assert.AreEqual("{\"Custom\":null}", jsonString);
        }

        #endregion

        #region Type Extensions

        [TestMethod]
        public void JsonSerializer_TypeExtensions()
        {
             var parameters = CreateTestParameters();
            parameters.UseTypeExtension = true;
            parameters.RegisterTypeDescriptor(new DataContractTypeDescriptor());

            Zoo zoo = new Zoo();
            zoo.Animals = new List<Animal>(new Animal[] { new Dog(), new Cat() });

            var jsonString = new JsonSerializer(parameters).Serialize(zoo);
            Assert.AreEqual(
                "{\"$type\":\"Apolyton.FastJson.Tests.Helpers.Polymorphism.Zoo\",\"Animals\":[{\"$type\":\"doggy\",\"Power\":4},{\"$type\":\"kitty\",\"Cuteness\":2}]}",
                jsonString);
        }

        [TestMethod]
        public void JsonSerializer_TypeExtensions_UseGlobalTypes()
        {
            JsonParameters parameter = CreateTestParameters();
            parameter.UseTypeExtension = true;
            parameter.UseGlobalTypes = true;

            JsonSerializer serializer = new JsonSerializer(parameter);
            NestingClass given = new NestingClass();
            given.Long = 12;
            given.NestedClass = new NestedClass();
            given.NestedClass.Integer = 11;

            String result = serializer.Serialize(given);
        }
        
        #endregion

        /// <summary>
        /// Verifies that when a class is passed without properties the serializer should either write 'null' or throw an exception. Otherwise non-trivial errors might
        /// appearch on client/ deserialization side.
        /// </summary>
        [TestMethod]
        [ExpectedException(typeof(SerializationException))]
        public void JsonSerializer_ClassWithNoProperties()
        {
            JsonSerializer serializer = new JsonSerializer(CreateTestParameters());
            ClassWithNoProperties given = new ClassWithNoProperties();

            serializer.Serialize(given);
        }
    }
}

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