protected virtual object ParseRequest(TextReader input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            JsonReader reader = (JsonReader) _serviceProvider.GetService(typeof(JsonReader));

            if (reader == null)
                reader = new JsonTextReader(input);
            
            ImportContext importContext = new ImportContext();
            
            JsonObject request = new JsonObject();
            Method method = null;
            JsonReader paramsReader = null;
            object args = null;
            
            reader.ReadToken(JsonTokenClass.Object);
            
            while (reader.TokenClass != JsonTokenClass.EndObject)
            {
                string memberName = reader.ReadMember();
                
                switch (memberName)
                {
                    case "id" :
                    {
                        request["id"] = importContext.Import(reader);
                        break;
                    }
                    case "method" :
                    {
                        string methodName = reader.ReadString();
                        request["method"] = methodName;
                        method = _service.GetClass().GetMethodByName(methodName);
                        
                        if (paramsReader != null)
                        {
                            //
                            // If the parameters were already read in and
                            // buffer, then deserialize them now that we know
                            // the method we're dealing with.
                            //
                            
                            args = ReadParameters(method, paramsReader, importContext);
                            paramsReader = null;
                        }
                        
                        break;
                    }
                    case "params" :
                    {
                        //
                        // Is the method already known? If so, then we can
                        // deserialize the parameters right away. Otherwise
                        // we record them until hopefully the method is
                        // encountered.
                        //
                        
                        if (method != null)
                        {
                            args = ReadParameters(method, reader, importContext);
                        }
                        else
                        {
                            JsonRecorder recorder = new JsonRecorder();
                            recorder.WriteValueFromReader(reader);
                            paramsReader = recorder.CreatePlayer();
                        }

                        break;
                    }
                }
            }
            
            reader.Read();

            if (args != null)
                request["params"] = args;
            
            return request;
        }
Exemplo n.º 2
0
        public static JsonRecorder Record(JsonReader reader)
        {
            if (reader == null)
                throw new ArgumentNullException("reader");

            JsonRecorder recorder = new JsonRecorder();
            recorder.WriteValueFromReader(reader);
            return recorder;
        }