void DeserializeTo <TMode>(byte *bytes, int length, IDataWriter dataWriter, MessagePackFormatterOptions options) where TMode : struct
        {
            var deserializer = new MessagePackDeserializer <TMode>(this, bytes, length, MaxDepth, options);

            if (typeof(TMode) == typeof(MessagePackDeserializeModes.ReferenceMode))
            {
                deserializer.InitReferences();
            }

            switch (deserializer.GetToken())
            {
            case MessagePackToken.Map:
                if (dataWriter is IDataWriter <string> objectWriter)
                {
                    deserializer.InternalReadObject(objectWriter);
                }
                else
                {
                    ((IAsDataWriter)dataWriter.As <string>()).InvokeTIn(new MessagePackDeserializer <TMode> .InternalWriteMapInvoker(deserializer, dataWriter));
                }
                break;

            case MessagePackToken.Array:
                deserializer.InternalReadArray(dataWriter.As <int>());
                break;

            default:
                dataWriter.Content = XConvert.Cast(deserializer.DirectRead(), dataWriter.ContentType);
                break;
            }
        }
Beispiel #2
0
        public void ReadMap <TKey>(IDataWriter <TKey> mapWriter)
        {
            var length = TryReadMapHead();

            if (length >= 0)
            {
                mapWriter.Initialize(length);

                for (int i = 0; i < length; i++)
                {
                    mapWriter.OnWriteValue(ValueInterface <TKey> .ReadValue(this), this);
                }

                return;
            }

            if (TryReadNull())
            {
                return;
            }

            if (IsArrayHead())
            {
                ReadArray(mapWriter.As <int>());

                return;
            }

            throw new InvalidOperationException("The msgpack node is not a map.");
        }
Beispiel #3
0
        public void ReadObject(IDataWriter <string> valueWriter)
        {
            var size = TryReadMapHead();

            if (size >= 0)
            {
                valueWriter.Initialize(size);

                if (valueWriter is IId64DataRW <byte> fastWriter)
                {
                    for (int i = 0; i < size; i++)
                    {
                        var bytesLength = TryReadStringHead();

                        if (bytesLength >= 0)
                        {
                            var id64 = fastWriter.GetId64(ref current[0], bytesLength);

                            current += bytesLength;

                            fastWriter.OnWriteValue(id64, this);
                        }
                        else
                        {
                            valueWriter.OnWriteValue(ReadString(), this);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < size; i++)
                    {
                        valueWriter.OnWriteValue(ReadString(), this);
                    }
                }


                return;
            }

            if (TryReadNull())
            {
                return;
            }

            if (IsArrayHead())
            {
                ReadArray(valueWriter.As <int>());

                return;
            }

            throw new InvalidOperationException("The msgpack node is not an object.");
        }
        private void InternalDeserializeTo <TMode>(char *chars, int length, IDataWriter dataWriter) where TMode : struct
        {
            var jsonDeserializer = new JsonDeserializer <TMode>(chars, length);

            if (jsonDeserializer.IsObject)
            {
                if (dataWriter is IId64DataRW <char> fastWriter)
                {
                    jsonDeserializer.FastReadObject(fastWriter);
                }
                else
                {
                    jsonDeserializer.SlowReadObject(dataWriter.As <string>());
                }
            }
            else if (jsonDeserializer.IsArray)
            {
                jsonDeserializer.NoInliningReadArray(dataWriter.As <int>());
            }
            else
            {
                RWHelper.SetContent(dataWriter, jsonDeserializer.DirectRead());
            }
        }
Beispiel #5
0
 public static void Copy(IDataReader dataReader, IDataWriter dataWriter)
 {
     Copy(dataReader.As <object>(), dataWriter.As <object>());
 }
Beispiel #6
0
 public static void Copy <T>(IDataReader <T> dataReader, IDataWriter dataWriter, IValueFilter <T> valueFilter)
 {
     Copy(dataReader, dataWriter.As <T>(), valueFilter);
 }
Beispiel #7
0
 public static void Copy <T>(IDataReader <T> dataReader, IDataWriter dataWriter)
 {
     Copy(dataReader, dataWriter.As <T>());
 }
Beispiel #8
0
 public static void Copy(ITableReader tableReader, IDataWriter dataWriter)
 {
     Copy(new TableToArrayReader(tableReader), dataWriter.As <int>());
 }
        public void ReadArray(IDataWriter <int> valueWriter)
        {
            switch (GetValueType())
            {
            case JsonValueTypes.String:
                throw new InvalidCastException("Cannot convert String to array.");

            case JsonValueTypes.Number:
                throw new InvalidCastException("Cannot convert Number to array.");

            case JsonValueTypes.Object:
                ReadObject(valueWriter.As <string>());
                return;

            case JsonValueTypes.True:
                throw new InvalidCastException("Cannot convert Boolean to array.");

            case JsonValueTypes.Null:
                /* 空对象直接返回 */
                this.index += 4;
                return;

            case JsonValueTypes.Undefined:
                this.index += 9;
                return;
            }

            int index = 0;

            while (this.index < length)
            {
                switch (chars[this.index])
                {
                case ' ':
                case '\n':
                case '\r':
                case '\t':

                    ++this.index;

                    continue;

                case '[':

                    valueWriter.Initialize();

                    goto case ',';

                case ']':
EndCase:

                    ++this.index;

                    goto ReturnValue;

                case ',':

                    ++this.index;

                    while (this.index < length)
                    {
                        switch (chars[this.index])
                        {
                        case ' ':
                        case '\n':
                        case '\r':
                        case '\t':
                            ++this.index;
                            continue;

                        case ']':
                            goto EndCase;

                        default:
                            goto ReadValue;
                        }
                    }

                    goto Exception;

ReadValue:

                    valueWriter.OnWriteValue(index, this);

                    ++index;

                    continue;

                default:

                    goto Exception;
                }
            }

Exception:
            throw GetException();

ReturnValue:

            return;
        }
        public void ReadObject(IDataWriter <string> valueWriter)
        {
            switch (GetValueType())
            {
            case JsonValueTypes.String:
                throw new InvalidCastException("Cannot convert String to object.");

            case JsonValueTypes.Number:
                throw new InvalidCastException("Cannot convert Number to object.");

            case JsonValueTypes.Array:
                ReadArray(valueWriter.As <int>());
                return;

            case JsonValueTypes.True:
            case JsonValueTypes.False:
                throw new InvalidCastException("Cannot convert Boolean to object.");

            case JsonValueTypes.Null:
                /* 空对象直接返回 */
                index += 4;
                return;

            case JsonValueTypes.Undefined:
                index += 9;
                return;
            }

            while (index < length)
            {
                switch (chars[index])
                {
                case ' ':
                case '\n':
                case '\r':
                case '\t':
                    ++index;

                    continue;

                case '{':

                    valueWriter.Initialize();

                    goto case ',';

                case '}':
EndCase:

                    ++index;

                    goto ReturnValue;

                case ',':

Loop:

                    ++index;

                    if (index >= length)
                    {
                        throw GetException();
                    }

                    char c = chars[index];

                    string name;

                    int flag;

                    switch (c)
                    {
                    case ' ':
                    case '\n':
                    case '\r':
                    case '\t':
                        goto Loop;

                    case '}':
                        goto EndCase;

                    case '"':
                    case '\'':
                        name = InternalReadString();

                        flag = StringHelper.IndexOf(chars, ':', index, length);

                        break;

                    default:
                        flag = StringHelper.IndexOf(chars, ':', index, length);

                        name = StringHelper.Trim(chars, index, flag);

                        break;
                    }

                    if (flag == -1)
                    {
                        goto Exception;
                    }

                    index = flag + 1;

                    while (index < length)
                    {
                        switch (chars[index])
                        {
                        case ' ':
                        case '\n':
                        case '\r':
                        case '\t':
                            ++index;
                            continue;

                        default:
                            goto ReadValue;
                        }
                    }

                    goto Exception;

ReadValue:

                    valueWriter.OnWriteValue(name, this);

                    continue;

                default:
                    goto Exception;
                }
            }


Exception:
            throw GetException();

ReturnValue:

            return;
        }
Beispiel #11
0
        void IValueReader.ReadObject(IDataWriter <string> valueWriter)
        {
            switch (GetValueType())
            {
            case JsonValueTypes.String:
                throw new InvalidCastException("Cannot convert String to object.");

            case JsonValueTypes.Number:
                throw new InvalidCastException("Cannot convert Number to object.");

            case JsonValueTypes.Array:
                ReadArray(valueWriter.As <int>());
                return;

            case JsonValueTypes.True:
            case JsonValueTypes.False:
                throw new InvalidCastException("Cannot convert Boolean to object.");

            case JsonValueTypes.Null:
                /* 空对象直接返回 */
                index += 4;
                return;

            case JsonValueTypes.Undefined:
                index += 9;
                return;
            }

            var haveRef = false;

            while (index < length)
            {
                switch (chars[index])
                {
                case ' ':
                case '\n':
                case '\r':
                case '\t':
                    ++index;

                    continue;

                case '{':

                    valueWriter.Initialize();

                    goto case ',';

                case '}':
EndCase:

                    ++index;

                    goto ReturnValue;

                case ',':

Loop:

                    ++index;

                    if (index >= length)
                    {
                        throw GetException();
                    }

                    char c = chars[index];

                    string name;

                    int flag;

                    switch (c)
                    {
                    case ' ':
                    case '\n':
                    case '\r':
                    case '\t':
                        goto Loop;

                    case '}':
                        goto EndCase;

                    case '"':
                    case '\'':
                        name = InternalReadString();

                        flag = StringHelper.IndexOf(chars, ':', index, length);

                        break;

                    default:
                        flag = StringHelper.IndexOf(chars, ':', index, length);

                        name = StringHelper.Trim(chars, index, flag);

                        break;
                    }

                    if (flag == -1)
                    {
                        goto Exception;
                    }

                    index = flag + 1;

                    while (index < length)
                    {
                        switch (chars[index])
                        {
                        case ' ':
                        case '\n':
                        case '\r':
                        case '\t':
                            ++index;
                            continue;

                        default:
                            goto ReadValue;
                        }
                    }

                    goto Exception;

ReadValue:

                    if (TryReadRefReferenceTarget())
                    {
                        valueWriter.OnWriteValue(name, RWHelper.DefaultValueReader);

                        references.AddLast(new ReferenceInfo(referenceTarget, new SourcePathInfo(name, null, valueWriter)));

                        if (valueWriter is IDataReader reader && reader.ReferenceToken == null)
                        {
                            updateBase = true;
                        }

                        haveRef = true;
                    }
                    else
                    {
                        valueWriter.OnWriteValue(name, this);

                        if (updateBase)
                        {
                            var last = references.Last.Value;

                            last.source = new SourcePathInfo(name, last.source, valueWriter);

                            updateBase = false;

                            haveRef = true;
                        }
                    }

                    continue;

                default:
                    goto Exception;
                }
            }


Exception:
            throw GetException();

ReturnValue:

            if (haveRef && valueWriter is IDataReader dataReader && dataReader.ReferenceToken == null)
            {
                writer = valueWriter;

                updateBase = true;
            }

            return;
        }
Beispiel #12
0
        void IValueReader.ReadArray(IDataWriter <int> valueWriter)
        {
            switch (GetValueType())
            {
            case JsonValueTypes.String:
                throw new InvalidCastException("Cannot convert String to array.");

            case JsonValueTypes.Number:
                throw new InvalidCastException("Cannot convert Number to array.");

            case JsonValueTypes.Object:
                ReadObject(valueWriter.As <string>());
                return;

            case JsonValueTypes.True:
                throw new InvalidCastException("Cannot convert Boolean to array.");

            case JsonValueTypes.Null:
                /* 空对象直接返回 */
                this.index += 4;
                return;

            case JsonValueTypes.Undefined:
                this.index += 9;
                return;
            }

            var haveRef = false;

            int index = 0;

            while (this.index < length)
            {
                switch (chars[this.index])
                {
                case ' ':
                case '\n':
                case '\r':
                case '\t':

                    ++this.index;

                    continue;

                case '[':

                    valueWriter.Initialize();

                    goto case ',';

                case ']':
EndCase:

                    ++this.index;

                    goto ReturnValue;

                case ',':

                    ++this.index;

                    while (this.index < length)
                    {
                        switch (chars[this.index])
                        {
                        case ' ':
                        case '\n':
                        case '\r':
                        case '\t':
                            ++this.index;
                            continue;

                        case ']':
                            goto EndCase;

                        default:
                            goto ReadValue;
                        }
                    }

                    goto Exception;

ReadValue:

                    if (TryReadRefReferenceTarget())
                    {
                        valueWriter.OnWriteValue(index, RWHelper.DefaultValueReader);

                        references.AddLast(new ReferenceInfo(referenceTarget, new SourcePathInfo(index, null, valueWriter)));

                        if (valueWriter is IDataReader reader && reader.ReferenceToken == null)
                        {
                            updateBase = true;
                        }

                        haveRef = true;
                    }
                    else
                    {
                        valueWriter.OnWriteValue(index, this);

                        if (updateBase)
                        {
                            var last = references.Last.Value;

                            last.source = new SourcePathInfo(index, last.source, valueWriter);

                            updateBase = false;

                            haveRef = true;
                        }
                    }

                    ++index;

                    continue;

                default:

                    goto Exception;
                }
            }

Exception:
            throw GetException();

ReturnValue:

            if (haveRef && valueWriter is IDataReader dataReader && dataReader.ReferenceToken == null)
            {
                writer = valueWriter;

                updateBase = true;
            }

            return;
        }