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
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;
using System.Collections.Generic;
using Apolyton.FastJson.Common;
using Apolyton.FastJson.Data;
using Apolyton.FastJson.Registry;
using Apolyton.FastJson.Serialization;

#if DESKTOP 
using System.Data;
#endif 

namespace Apolyton.FastJson
{
    /// <summary>
    /// Entry point for json related operations. 
    /// </summary>
    /// <remarks>
    /// http://www.codeproject.com/Articles/159450/fastJSON
    /// The version over there (2.0.9) could not be taken directly, as its serializer is taking all public properties, disregarding any attribute policy. This is
    /// not good for our case, as we want to return (portions of) data objects as well.
    /// </remarks>
    public sealed class Json
    {
        private static Json _current;
        private JsonParameters _defaultParameters;

        private Json()
        {
            _defaultParameters = new JsonParameters();
        }

        /// <summary>
        /// Returns the list of members which are concerded by (de-)serialization according to the current default parameters.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IEnumerable<JsonSerializationInfo> GetSerializationMembers(Type type)
        {
            return _defaultParameters.GetSerializationMembers(type);
        }

        /// <summary>
        /// Returns a json string for the given object.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string ToJson(object obj)
        {
            return ToJson(obj, _defaultParameters);
        }

        /// <summary>
        /// Returns the bytes of the produced json string according to the encoding as defined in the default parameters.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public byte[] ToJsonBytes(object obj)
        {
            String jsonString = ToJson(obj, _defaultParameters);

            return _defaultParameters.Encoding.GetBytes(jsonString);
        }

        /// <summary>
        /// Returns a json string for the given object.
        /// </summary>
        public string ToJson(object source, JsonParameters param)
        {
            Guard.ArgumentNotNull(source, "obj");
            Guard.ArgumentNotNull(param, "param");

            Type t = null;

            if (source == null)
            {
                return "null";
            }

            if (source.GetType().IsGenericType)
            {
                t = source.GetType().GetGenericTypeDefinition();
            }

            return new JsonSerializer(param).Serialize(source);
        }

        /// <summary>
        /// Builds the instance up with the provided values from the value object. Normally used after ReadJsonValue(String).
        /// </summary>
        /// <param name="instanceToBuild"></param>
        /// <param name="jsonValue"></param>
        public void BuildUp(Object instanceToBuild, IJsonValue jsonValue)
        {
            Guard.ArgumentNotNull(instanceToBuild, "instanceToBuild");

            if (jsonValue == null) { return; }

            JsonObject valueAsObject = jsonValue as JsonObject;
            if (valueAsObject != null)
            {
                new JsonValueDeserializer(_defaultParameters).BuildUp(instanceToBuild, valueAsObject);
            }
            else 
            {
                JsonArray valueAsArray = jsonValue as JsonArray;
                if (valueAsArray != null)
                {
                    new JsonValueDeserializer(_defaultParameters).BuildUp(instanceToBuild, valueAsArray);
                }
                else
                {
                    throw new NotSupportedException("This type of json value is not supported.");
                }
            }
        }

        /// <summary>
        /// Returns the parsed json string as an object which is IDictionary or IList. Number types are parsed, however all nested objects remain generic implementations
        /// of IDictionary or IList.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public IEnumerable ReadSomeObject(string jsonString)
        {
            Guard.ArgumentNotNullOrEmpty(jsonString, "jsonString");

            return new JsonHybridDeserializer(_defaultParameters).Deserialize(ref jsonString);
        }

        /// <summary>
        /// Returns the strongly typed object for the given json string.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public T ReadObject<T>(string jsonString)
        {
            Guard.ArgumentNotNullOrEmpty(jsonString, "jsonString");

            return (T)ReadObjectInternal(ref jsonString, typeof(T), _defaultParameters);
        }

        /// <summary>
        /// Returns the object with the best fitting type for the given json string.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public object ReadObject(string jsonString)
        {
            Guard.ArgumentNotNullOrEmpty(jsonString, "jsonString");

            return ReadObjectInternal(ref jsonString, null, _defaultParameters);
        }

        /// <summary>
        /// Returns the object with the specified type for the given json string and the defined target type.
        /// </summary>
        public object ReadObject(string jsonString, Type targetType)
        {
            Guard.ArgumentNotNullOrEmpty(jsonString, "jsonString");
            Guard.ArgumentNotNull(targetType, "targetType");

            return ReadObjectInternal(ref jsonString, targetType, _defaultParameters);
        }

        /// <summary>
        /// Returns the object for the given json string and the defined target type.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <param name="targetType"></param>
        /// <returns></returns>
        private object ReadObjectInternal(ref string jsonString, Type targetType, JsonParameters parameters)
        {
            var deserializer = new JsonValueDeserializer(_defaultParameters);
            var jsonValue = deserializer.Deserialize(ref jsonString);

            if (targetType == null)
            {
                if (jsonValue.Type == JsonType.JsonObject)
                {
                    JsonObject valueAsObject = (JsonObject)jsonValue;
                    String rootLevelTypeName = valueAsObject.TryGetTypeName();

                    parameters.Registry.TypeDescriptor.TryGetType(rootLevelTypeName, out targetType);
                }
                else
                {
                    targetType = typeof(List<Object>);
                }
            }

            if (targetType == null) { throw new InvalidOperationException("Cannot deserialize when target type is not specified and json string does not contain a resolvable $type field."); }
            if (targetType.IsAbstract) { throw new InvalidOperationException("Cannot read into an abstract object and custom types are not yet supported at this level. You might want to use ReadJsonVlaue and BuildUp instead."); }

            var targetInstance = _defaultParameters.Registry.CreateInstanceFast(targetType);

            deserializer.BuildUp(targetInstance, (JsonObject)jsonValue);

            return targetInstance;
        }

        /// <summary>
        /// Returns the json value object for the given json string.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public IJsonValue ReadJsonValue(string jsonString)
        {
            Guard.ArgumentNotNullOrEmpty(jsonString, "jsonString");

            return new JsonValueDeserializer(_defaultParameters).Deserialize(ref jsonString);
        }

#if DESKTOP
        /// <summary>
        /// Returns the data set represented by the given json string.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public DataSet ReadDataSet(string jsonString)
        {
            return (DataSet)new JsonObjectDeserializer(_defaultParameters).Deserialize(ref jsonString, typeof(DataSet));
        }

        /// <summary>
        /// Returns the data table represented by the given json string.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public DataTable ReadDataTable(string jsonString)
        {
            return (DataTable)new JsonObjectDeserializer(_defaultParameters).Deserialize(ref jsonString, typeof(DataTable));
        }

#endif

        /// <summary>
        /// Builds up the instance based on the given json object values.
        /// </summary>
        /// <param name="instanceToBuild">A non-null instance which is populated with the provided values</param>
        /// <param name="values">The values which should fill the given instance.</param>
        /// <param name="throwOnMissingValue">If true, it throws an exception when a specified (by the instance) value is not present in the json object.</param>
        /// <exception cref="ArgumentNullException"/>
        internal void BuildUp(Object instanceToBuild, JsonObject values, bool throwOnMissingValue)
        {
            Guard.ArgumentNotNull(instanceToBuild, "instanceToBuild");
            Guard.ArgumentNotNull(values, "values");

            new JsonValueDeserializer(_defaultParameters).BuildUp(instanceToBuild, values);
        }

        /// <summary>
        /// Returns a beautified string of the given json string.
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public string Beautify(string jsonString)
        {
            Guard.ArgumentNotNull(jsonString, "jsonString");

            if (jsonString == String.Empty) { return String.Empty; }

            return Formatter.PrettyPrint(jsonString);
        }

        /// <summary>
        /// Creates a clone of the object by serialize and deserialize it again.
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public object DeepCopy(object source)
        {
            Guard.ArgumentNotNull(source, "source");

            return ReadObject(ToJson(source));
        }

        /// <summary>
        /// Creates a clone of the object by serialize and deserialize it again.
        /// </summary>
        public T DeepCopy<T>(T source)
        {
            Guard.ArgumentNotNull(source, "source");

            return ReadObject<T>(ToJson(source));
        }

        /// <summary>
        /// Registers a custom type.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="serializer"></param>
        /// <param name="deserializer"></param>
        public void RegisterCustomType(Type type, SerializationHandler serializer, DeserializationHandler deserializer)
        {
            _defaultParameters.RegisterCustomType(type, serializer, deserializer);
        }

        /// <summary>
        /// Gets the json instance.
        /// </summary>
        public static Json Current
        {
            get { return _current ?? (_current = new Json()); }
        }

        /// <summary>
        /// Gets or sets the default parameters.
        /// </summary>
        public JsonParameters DefaultParameters
        {
            get { return _defaultParameters; }
            set { _defaultParameters = value; }
        }
    }

}

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