Ejemplo n.º 1
0
        private void ReadType(SmileType type)
        {
            switch (type.TypeClass)
            {
            case SmileTypeClass.TinyASCII:
            {
                int length = type.Value + 1;
                SetToken(JsonToken.String, ReadStringInLength(length));
                break;
            }

            case SmileTypeClass.ShortASCII:
            {
                int length = type.Value + 33;
                SetToken(JsonToken.String, ReadStringInLength(length));
                break;
            }

            case SmileTypeClass.TinyUnicode:
            {
                int length = type.Value + 2;
                SetToken(JsonToken.String, ReadStringInLength(length));
                break;
            }

            case SmileTypeClass.ShortUnicode:
            {
                int length = type.Value + 34;
                SetToken(JsonToken.String, ReadStringInLength(length));
                break;
            }

            case SmileTypeClass.SmallIntegers:
            {
                SetToken(JsonToken.Integer, ReadZigzagNumber(type.Value));
                break;
            }

            case SmileTypeClass.SimpleLiteralsNumbers:
            {
                switch (type.Value)
                {
                case 0x00:
                    SetToken(JsonToken.String, String.Empty);
                    break;

                case 0x01:
                    SetToken(JsonToken.Null);
                    break;

                case 0x02:
                    SetToken(JsonToken.Boolean, false);
                    break;

                case 0x03:
                    SetToken(JsonToken.Boolean, true);
                    break;

                case 0x04:
                    SetToken(JsonToken.Integer, ReadZigzag32());
                    break;

                case 0x05:
                    SetToken(JsonToken.Integer, ReadZigzag64());
                    break;

                case 0x06:
                    SetToken(JsonToken.Integer, ReadBigInteger());
                    break;

                case 0x08:
                    SetToken(JsonToken.Float, ReadFloat32());
                    break;

                case 0x09:
                    SetToken(JsonToken.Float, ReadFloat64());
                    break;

                case 0x0A:
                    SetToken(JsonToken.Float, ReadBigDecimal());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                    break;
                }
                break;
            }

            case SmileTypeClass.START_OBJECT:
            {
                SetToken(JsonToken.StartObject);
                PushContext(SmileType.NewObject());
                break;
            }

            case SmileTypeClass.START_ARRAY:
            {
                SetToken(JsonToken.StartArray);
                PushContext(SmileType.NewArray());
                break;
            }

            case SmileTypeClass.Binary:
            {
                SmileBinaryType binaryType;
                byte[]          data = ReadBinary(out binaryType);

                object value = (binaryType != SmileBinaryType.Uuid)
                                                        ? data
                                                        : (object)new Guid(data);

                SetToken(JsonToken.Bytes, value);
                break;
            }

            case SmileTypeClass.Binary7bitEncoded:
            {
                byte[] data = Read7BitBinaryWithLength();
                SetToken(JsonToken.Bytes, data);
                break;
            }

            case SmileTypeClass.LongASCIIText:
            {
                byte[] data = ReadFCStringBuff();
                string text;
                if (!SmileUtil.IsASCIIString(data, out text))
                {
                    throw new FormatException("Wrong encoding.");
                }
                SetToken(JsonToken.String, text);
            }
            break;

            case SmileTypeClass.LongUnicodeText:
            {
                byte[] data = ReadFCStringBuff();
                string text = Encoding.UTF8.GetString(data, 0, data.Length);
                SetToken(JsonToken.String, text);
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("type", "Unexpected SmileType value: " + type.TypeClass);
            }
        }
Ejemplo n.º 2
0
        private bool ReadNormal()
        {
            switch (CurrentState)
            {
            case State.Start:
            {
                JsonToken token = (!_readRootValueAsArray) ? JsonToken.StartObject : JsonToken.StartArray;
                SmileType type  = (!_readRootValueAsArray) ? SmileType.NewObject() : SmileType.NewArray();

                _currentElementType = ReadType();
                type = _currentElementType;

                SetToken(token);
                PushContext(type);
                return(true);
            }

            case State.Complete:
            case State.Closed:
                return(false);

            case State.Property:
            {
                ReadType(_currentElementType);
                return(true);
            }

            case State.ObjectStart:
            case State.ArrayStart:
            case State.PostValue:
            {
                SmileType context = _currentContext;
                if (context == null)
                {
                    return(false);
                }

                byte keyType = this.ReadByte();
                if (keyType == (byte)0xF9 || keyType == (byte)0xFB)
                {
                    PopContext();
                    JsonToken endToken = (context.TypeClass == SmileTypeClass.START_OBJECT) ? JsonToken.EndObject : JsonToken.EndArray;
                    SetToken(endToken);
                    return(true);
                }

                if (_currentContext.TypeClass == SmileTypeClass.START_ARRAY)
                {
                    _currentElementType = SmileType.Parse(keyType);
                    ReadType(_currentElementType);
                }
                else
                {
                    SetToken(JsonToken.PropertyName, ReadElement(keyType));
                }

                return(true);
            }

            case State.ConstructorStart:
                break;

            case State.Constructor:
                break;

            case State.Error:
                break;

            case State.Finished:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(false);
        }