Пример #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // ReSharper disable once ConvertIfStatementToSwitchStatement
            if (reader.TokenType == JsonToken.StartObject)
            {
                var    builder   = new RpcObject.Builder();
                string fieldName = null;
                while (reader.Read())
                {
                    // ReSharper disable once SwitchStatementMissingSomeCases
                    switch (reader.TokenType)
                    {
                    case JsonToken.PropertyName:
                        fieldName = reader.Value.ToString();
                        break;

                    case JsonToken.EndObject:
                        return(builder.Build());

                    default:
                        builder.Put(fieldName, (RpcItem)ReadJson(reader, objectType, existingValue, serializer));
                        break;
                    }
                }
                return(builder.Build());
            }
            else if (reader.TokenType == JsonToken.StartArray)
            {
                var builder = new RpcArray.Builder();
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.EndArray)
                    {
                        return(builder.Build());
                    }

                    var item = (RpcItem)ReadJson(reader, objectType, existingValue, serializer);
                    builder.Add(item);
                }
                return(builder.Build());
            }
            else
            {
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (reader.TokenType)
                {
                case JsonToken.Boolean:
                    return(new RpcValue((bool)reader.Value));

                case JsonToken.Integer:
                    return(new RpcValue(BigInteger.Parse(reader.Value.ToString())));

                case JsonToken.Null:
                    return(new RpcValue((string)null));

                default:
                    return(new RpcValue(reader.Value.ToString()));
                }
            }
        }
Пример #2
0
        private static RpcArray ToRpcArray(object obj)
        {
            var componentType = obj.GetType();

            if (componentType == typeof(bool) || !componentType.IsPrimitive)
            {
                var builder = new RpcArray.Builder();

                var castedArray = (IEnumerable)obj;
                foreach (var item in castedArray)
                {
                    builder.Add(ToRpcItem(item));
                }

                return(builder.Build());
            }
            return(null);
        }