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

namespace Apolyton.FastJson.Serialization
{
    /// <summary>
    /// Represents a base class for json related deserializations.
    /// </summary>
    internal abstract class JsonDeserializer
    {
        /// <summary>
        /// The original json string.
        /// </summary>
        protected char[] _jsonOriginal;

        /// <summary>
        /// The current processing index.
        /// </summary>
        protected int _index;

        private Token _lookAheadToken = Token.None;
        private readonly StringBuilder _localString;

        /// <summary>
        /// Creates an instance of the deserializer.
        /// </summary>
        public JsonDeserializer()
        {
            _localString = new StringBuilder();
        }

        #region Token Helpers

        protected Token LookAhead()
        {
            if (_lookAheadToken != Token.None) { return _lookAheadToken; }

            return _lookAheadToken = NextTokenCore();
        }

        protected void ConsumeToken()
        {
            _lookAheadToken = Token.None;
        }

        protected Token NextToken()
        {
            var result = _lookAheadToken != Token.None ? _lookAheadToken : NextTokenCore();

            _lookAheadToken = Token.None;

            return result;
        }

        private Token NextTokenCore()
        {
            char c;

            try
            {
                // Skip past whitespace
                do
                {
                    c = _jsonOriginal[_index];

                    if (c > ' ') break;
                    if (c != ' ' && c != '\t' && c != '\n' && c != '\r') break;

                } while (++_index < _jsonOriginal.Length);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new SerializationException("String syntax error. See inner exception. ", e);
            }

            if (_index == _jsonOriginal.Length)
            {
                throw new SerializationException("Reached end of string unexpectedly");
            }

            c = _jsonOriginal[_index];

            _index++;

            switch (c)
            {
                case '{':
                    return Token.Curly_Open;

                case '}':
                    return Token.Curly_Close;

                case '[':
                    return Token.Squared_Open;

                case ']':
                    return Token.Squared_Close;

                case ',':
                    return Token.Comma;

                case '"':
                    return Token.String;

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '-':
                case '+':
                case '.':
                    return Token.Number;

                case ':':
                    return Token.Colon;

                case 'f':
                    if (_jsonOriginal.Length - _index >= 4 &&
                        _jsonOriginal[_index + 0] == 'a' &&
                        _jsonOriginal[_index + 1] == 'l' &&
                        _jsonOriginal[_index + 2] == 's' &&
                        _jsonOriginal[_index + 3] == 'e')
                    {
                        _index += 4;
                        return Token.False;
                    }
                    break;

                case 't':
                    if (_jsonOriginal.Length - _index >= 3 &&
                        _jsonOriginal[_index + 0] == 'r' &&
                        _jsonOriginal[_index + 1] == 'u' &&
                        _jsonOriginal[_index + 2] == 'e')
                    {
                        _index += 3;
                        return Token.True;
                    }
                    break;

                case 'n':
                    if (_jsonOriginal.Length - _index >= 3 &&
                        _jsonOriginal[_index + 0] == 'u' &&
                        _jsonOriginal[_index + 1] == 'l' &&
                        _jsonOriginal[_index + 2] == 'l')
                    {
                        _index += 3;
                        return Token.Null;
                    }
                    break;

            }

            throw new SerializationException("Could not find token at index " + --_index);
        }

        #endregion

        #region Value Conversion and Parsing

        /// <summary>
        /// Returns the parsed string from 
        /// </summary>
        /// <returns></returns>
        protected string ParseString()
        {
            ConsumeToken(); // "

            _localString.Length = 0;

            int runIndex = -1;

            while (_index < _jsonOriginal.Length)
            {
                var c = _jsonOriginal[_index++];

                if (c == '"')
                {
                    if (runIndex != -1)
                    {
                        if (_localString.Length == 0)
                        {
                            return new string(_jsonOriginal, runIndex, _index - runIndex - 1);
                        }

                        _localString.Append(_jsonOriginal, runIndex, _index - runIndex - 1);
                    }

                    return _localString.ToString();
                }

                if (c != '\\')
                {
                    if (runIndex == -1)
                    {
                        runIndex = _index - 1;
                    }

                    continue;
                }

                if (_index == _jsonOriginal.Length) { break; }

                if (runIndex != -1)
                {
                    _localString.Append(_jsonOriginal, runIndex, _index - runIndex - 1);
                    runIndex = -1;
                }

                switch (_jsonOriginal[_index++])
                {
                    case '"':
                        _localString.Append('"');
                        break;

                    case '\\':
                        _localString.Append('\\');
                        break;

                    case '/':
                        _localString.Append('/');
                        break;

                    case 'b':
                        _localString.Append('\b');
                        break;

                    case 'f':
                        _localString.Append('\f');
                        break;

                    case 'n':
                        _localString.Append('\n');
                        break;

                    case 'r':
                        _localString.Append('\r');
                        break;

                    case 't':
                        _localString.Append('\t');
                        break;

                    case 'u':
                        {
                            int remainingLength = _jsonOriginal.Length - _index;

                            if (remainingLength < 4) { break; }

                            // parse the 32 bit hex into an integer codepoint
                            uint codePoint = ParseUnicode(_jsonOriginal[_index], _jsonOriginal[_index + 1], _jsonOriginal[_index + 2], _jsonOriginal[_index + 3]);
                            _localString.Append((char)codePoint);

                            // skip 4 chars
                            _index += 4;
                        }
                        break;
                }
            }

            throw new SerializationException("Unexpectedly reached the end of string.");
        }

        private uint ParseUnicode(char c1, char c2, char c3, char c4)
        {
            uint p1 = ParseSingleChar(c1, 0x1000);
            uint p2 = ParseSingleChar(c2, 0x100);
            uint p3 = ParseSingleChar(c3, 0x10);
            uint p4 = ParseSingleChar(c4, 1);

            return p1 + p2 + p3 + p4;
        }

        private uint ParseSingleChar(char c1, uint multipliyer)
        {
            uint p1 = 0;

            if (c1 >= '0' && c1 <= '9')
            {
                p1 = (uint)(c1 - '0') * multipliyer;
            }
            else if (c1 >= 'A' && c1 <= 'F')
            {
                p1 = (uint)((c1 - 'A') + 10) * multipliyer;
            }
            else if (c1 >= 'a' && c1 <= 'f')
            {
                p1 = (uint)((c1 - 'a') + 10) * multipliyer;
            }

            return p1;
        }

        #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)

Share

About the Author

Aron Kovacs

Germany Germany
No Biography provided

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