Esempio n. 1
0
            public void Export(ExportContext context, JsonWriter writer, object source)
            {
                if (context == null)
                {
                    throw new ArgumentNullException("context");
                }
                if (writer == null)
                {
                    throw new ArgumentNullException("writer");
                }
                if (source == null)
                {
                    throw new ArgumentNullException("source");
                }

                object value = _property.GetValue(source);

                if (JsonNull.LogicallyEquals(value) || value.Equals(_defaultValue))
                {
                    return;
                }

                writer.WriteMember(_property.Name);
                context.Export(value, writer);
            }
        public virtual void Export(object value, JsonWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (JsonNull.LogicallyEquals(value))
            {
                writer.WriteNull();
            }
            else
            {
                IExporter exporter = FindExporter(value.GetType());

                if (exporter != null)
                {
                    exporter.Export(this, value, writer);
                }
                else
                {
                    writer.WriteString(value.ToString());
                }
            }
        }
Esempio n. 3
0
        static void TestValue(object expected, JsonReader reader, string path)
        {
            if (JsonNull.LogicallyEquals(expected))
            {
                Assert.AreEqual(JsonTokenClass.Null, reader.TokenClass, path);
            }
            else
            {
                var expectedType = Type.GetTypeCode(expected.GetType());

                if (expectedType == TypeCode.Object)
                {
                    if (expected.GetType().IsArray)
                    {
                        TestArray((Array)expected, reader, path);
                    }
                    else
                    {
                        TestObject((JsonObject)expected, reader, path);
                    }
                }
                else
                {
                    switch (expectedType)
                    {
                    case TypeCode.String: Assert.AreEqual(expected, reader.ReadString(), path); break;

                    case TypeCode.Int32: Assert.AreEqual(expected, (int)reader.ReadNumber(), path); break;

                    default: Assert.Fail("Don't know how to handle {0} values.", expected.GetType()); break;
                    }
                }
            }
        }
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);
            if (_properties.Count == 0)
            {
                writer.WriteString(value.ToString());
                return;
            }
            ObjectReferenceTracker objectReferenceTracker = null;

            try
            {
                writer.WriteStartObject();
                foreach (PropertyDescriptor property in _properties)
                {
                    object value2 = property.GetValue(value);
                    if (!JsonNull.LogicallyEquals(value2))
                    {
                        writer.WriteMember(property.Name);
                        if (objectReferenceTracker == null)
                        {
                            objectReferenceTracker = TrackObject(context, value);
                        }
                        context.Export(value2, writer);
                    }
                }
                writer.WriteEndObject();
            }
            finally
            {
                objectReferenceTracker?.Pop(value);
            }
        }
Esempio n. 5
0
        protected override void ExportValue(ExportContext context, object value, JsonWriter writer)
        {
            Debug.Assert(context != null);
            Debug.Assert(value != null);
            Debug.Assert(writer != null);

            if (_properties.Count == 0)
            {
                writer.WriteString(value.ToString());
            }
            else
            {
                ObjectReferenceTracker tracker = null;

                try
                {
                    writer.WriteStartObject();

                    foreach (PropertyDescriptor property in _properties)
                    {
                        object propertyValue = property.GetValue(value);

                        if (!JsonNull.LogicallyEquals(propertyValue))
                        {
                            writer.WriteMember(property.Name);

                            if (tracker == null)
                            {
                                //
                                // We are about to enter a deeper scope so
                                // start tracking the current object being
                                // exported. This will help to detect
                                // recursive references that may occur
                                // through this exporter deeper in the tree.
                                //

                                tracker = TrackObject(context, value);
                            }

                            context.Export(propertyValue, writer);
                        }
                    }

                    writer.WriteEndObject();
                }
                finally
                {
                    if (tracker != null)
                    {
                        tracker.Pop(value);
                    }
                }
            }
        }
Esempio n. 6
0
        public virtual void Export(ExportContext context, object value, JsonWriter writer)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            if (writer == null)
                throw new ArgumentNullException("writer");

            if (JsonNull.LogicallyEquals(value))
                writer.WriteNull();
            else
                ExportValue(context, value, writer);
        }
Esempio n. 7
0
        public static object GetResult(IDictionary response, Type resultType)
        {
            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            object errorObject = response["error"];

            if (errorObject != null)
            {
                IDictionary error = errorObject as IDictionary;

                string message = null;

                if (!JsonNull.LogicallyEquals(error))
                {
                    object messageObject = error["message"];

                    if (!JsonNull.LogicallyEquals(messageObject))
                    {
                        message = messageObject.ToString();
                    }
                }
                else
                {
                    message = error.ToString();
                }

                throw new JsonRpcException(message);
            }

            if (!response.Contains("result"))
            {
                throw new ArgumentException("Response object is not valid because it does not contain the expected 'result' member.", "response");
            }

            object result = response["result"];

            if (resultType == null)
            {
                return(result);
            }

            JsonRecorder recorder = new JsonRecorder();

            // FIXME: Allow the context to be passed in.
            JsonConvert.Export(result, recorder);
            return(JsonConvert.Import(resultType, recorder.CreatePlayer()));
        }
Esempio n. 8
0
    public void Export(ExportContext context, object value, JsonWriter writer)
    {
        var properties = value.GetType().GetProperties();

        writer.WriteStartObject();
        foreach (var property in properties)
        {
            var propertyValue = property.GetValue(value, null);
            if (!JsonNull.LogicallyEquals(propertyValue))
            {
                writer.WriteMember(property.Name);
                context.Export(propertyValue, writer);
            }
        }
        writer.WriteEndObject();
    }
Esempio n. 9
0
        public virtual void Export(ExportContext context, object value, JsonWriter writer)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            if (JsonNull.LogicallyEquals(value))
            {
                writer.WriteNull();
            }
            else
            {
                ExportValue(context, value, writer);
            }
        }
Esempio n. 10
0
        /* TODO: Add async processing.
         *
         *  IAsyncResult BeginProcess(JsonReader input, JsonWriter output, AsyncCallback callback, object asyncState);
         *  void BeginProcess(IAsyncResult asyncResult); */

        public virtual IDictionary Invoke(IDictionary request, bool authorised)
        {
            if (request == null)
            {
                throw new ArgumentNullException();
            }

            //
            // Get the ID of the request.
            //

            object id = request["id"];

            //
            // If the ID is not there or was not set then this is a notification
            // request from the client that does not expect any response. Right
            // now, we don't support this.
            //

            bool isNotification = JsonNull.LogicallyEquals(id);

            if (isNotification)
            {
                throw new NotSupportedException("Notification are not yet supported.");
            }

            if (JsonRpcTrace.TraceInfo)
            {
                JsonRpcTrace.Info("Received request with the ID {0}.", id.ToString());
            }

            //
            // Get the method name and arguments.
            //

            string methodName = Mask.NullString((string)request["method"]);

            if (methodName.Length == 0)
            {
                throw new JsonRpcException("No method name supplied for this request.");
            }

            //TODO2: change RE authorisation options deny setclientname - just remove it entirely?!?
            if (!authorised && (methodName != "Authenticate" && methodName != "SetClientName"))
            {
                throw new JsonRpcException("Not authorised.");
            }

            if (JsonRpcTrace.Switch.TraceInfo)
            {
                JsonRpcTrace.Info("Invoking method {1} on service {0}.", ServiceName, methodName);
            }

            //
            // Invoke the method on the service and handle errors.
            //

            object error  = null;
            object result = null;

            try
            {
                IService service = Service;
                Method   method  = service.GetClass().GetMethodByName(methodName);

                if (RequireIdempotency && !method.Idempotent)
                {
                    throw new JsonRpcException(string.Format("Method {1} on service {0} is not allowed for idempotent type of requests.", ServiceName, methodName));
                }

                object[] args;
                string[] names = null;

                object      argsObject = request["params"];
                IDictionary argByName  = argsObject as IDictionary;

                if (argByName != null)
                {
                    names = new string[argByName.Count];
                    argByName.Keys.CopyTo(names, 0);
                    args = new object[argByName.Count];
                    argByName.Values.CopyTo(args, 0);
                }
                else
                {
                    args = CollectionHelper.ToArray((ICollection)argsObject);
                }

                result = method.Invoke(service, names, args);
            }
            catch (MethodNotFoundException e)
            {
                error = OnError(e, request);
            }
            catch (InvocationException e)
            {
                error = OnError(e, request);
            }
            catch (TargetMethodException e)
            {
                error = OnError(e.InnerException, request);
            }
            catch (Exception e)
            {
                if (JsonRpcTrace.Switch.TraceError)
                {
                    JsonRpcTrace.Error(e);
                }

                throw;
            }

            //
            // Setup and return the response object.
            //

            return(CreateResponse(request, result, error));
        }