Esempio n. 1
0
        public void ToDynamicRow_FromString()
        {
            var str = @"{name: ""Orlov"", dob: ""02/12/2007 6:45 PM"", certified: true, serviceyears: 12, salary: 145000}";

            var row = new DynamicDoc(Schema.GetForTypedDoc(typeof(MySimpleData)));

            JSONReader.ToDoc(row, str.JSONToDataObject() as JSONDataMap);

            Aver.AreObjectsEqual("Orlov", row["Name"]);
            Aver.AreObjectsEqual(new DateTime(2007, 2, 12, 18, 45, 0), row["DOB"]);
            Aver.AreObjectsEqual(true, row["Certified"]);
            Aver.AreObjectsEqual(12, row["ServiceYears"]);
            Aver.AreObjectsEqual(145000m, row["Salary"]);
        }
Esempio n. 2
0
        /// <summary>
        /// Fills method invocation param array with args doing some interpretation for widely used types like JSONDataMaps, Rows etc..
        /// </summary>
        protected virtual void BindParameters(Controller controller, string action, ActionAttribute attrAction, MethodInfo method, WorkContext work, out object[] args)
        {
            var mpars = method.GetParameters();

            args = new object[mpars.Length];

            if (mpars.Length == 0)
            {
                return;
            }

            var requested = work.WholeRequestAsJSONDataMap;

            var strictParamBinding = attrAction.StrictParamBinding;

            //check for complex type
            for (var i = 0; i < mpars.Length; i++)
            {
                var ctp = mpars[i].ParameterType;
                if (ctp == typeof(object) || ctp == typeof(JSONDataMap) || ctp == typeof(Dictionary <string, object>))
                {
                    args[i] = requested;
                    continue;
                }
                if (typeof(TypedDoc).IsAssignableFrom(ctp))
                {
                    try
                    {
                        args[i] = JSONReader.ToDoc(ctp, requested);
                        continue;
                    }
                    catch (Exception error)
                    {
                        throw new HTTPStatusException(WebConsts.STATUS_400,
                                                      WebConsts.STATUS_400_DESCRIPTION,
                                                      error.ToMessageWithType(),
                                                      error);
                    }
                }
            }

            for (var i = 0; i < args.Length; i++)
            {
                if (args[i] != null)
                {
                    continue;
                }

                var mp = mpars[i];

                var got = requested[mp.Name];

                if (got == null)
                {
                    if (mp.HasDefaultValue)
                    {
                        args[i] = mp.DefaultValue;
                    }
                    continue;
                }

                if (got is byte[])
                {
                    if (mp.ParameterType == typeof(byte[]))
                    {
                        args[i] = got;
                        continue;
                    }
                    if (mp.ParameterType == typeof(Stream) || mp.ParameterType == typeof(MemoryStream))
                    {
                        args[i] = new MemoryStream((byte[])got, false);
                        continue;
                    }
                    if (strictParamBinding)
                    {
                        throw new HTTPStatusException(Web.WebConsts.STATUS_400,
                                                      Web.WebConsts.STATUS_400_DESCRIPTION,
                                                      StringConsts.MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                      .Args(
                                                          controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                          "strict",
                                                          action,
                                                          mp.Name,
                                                          mp.ParameterType.DisplayNameWithExpandedGenericArgs(), "byte[]"));
                    }
                }//got byte[]

                var strVal = got.AsString();
                try
                {
                    args[i] = strVal.AsType(mp.ParameterType, strictParamBinding);
                }
                catch
                {
                    const int MAX_LEN = 30;
                    if (strVal.Length > MAX_LEN)
                    {
                        strVal = strVal.Substring(0, MAX_LEN) + "...";
                    }
                    throw new HTTPStatusException(WebConsts.STATUS_400,
                                                  WebConsts.STATUS_400_DESCRIPTION,
                                                  StringConsts.MVC_CONTROLLER_ACTION_PARAM_BINDER_ERROR
                                                  .Args(
                                                      controller.GetType().DisplayNameWithExpandedGenericArgs(),
                                                      strictParamBinding ? "strict" : "relaxed",
                                                      action,
                                                      mp.Name,
                                                      mp.ParameterType.DisplayNameWithExpandedGenericArgs(), strVal));
                }
            }
        }