Beispiel #1
0
        private static void AddObjectFields(RpcObject.Builder builder, object parent, IEnumerable <PropertyInfo> fields)
        {
            foreach (var field in fields)
            {
                var key = GetKeyFromObjectField(field);
                if (key.Equals("this$0"))
                {
                    continue;
                }

                var type = field.PropertyType;

                object fieldObject;

                try
                {
                    fieldObject = field.GetValue(parent);
                }
                catch (Exception)
                {
                    fieldObject = null;
                }

                // ReSharper disable once ExpressionIsAlwaysNull
                if (fieldObject != null || !type.IsInstanceOfType(fieldObject))
                {
                    var rpcItem = ToRpcItem(type, fieldObject);

                    if (rpcItem != null && !rpcItem.IsEmpty())
                    {
                        builder.Put(key, rpcItem);
                    }
                }
            }
        }
        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()));
                }
            }
        }
Beispiel #3
0
        private static RpcObject ToRpcObject(object @object)
        {
            var builder = new RpcObject.Builder();

            var props = @object.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            AddObjectFields(builder, @object, props);

            return(builder.Build());
        }