private static void AddRenderedValue(
            LogEventInfo source, IDictionary <string, object> dest,
            JsonEncodeLayoutRendererWrapper renderer, JsonAttribute attribute)
        {
            renderer.Inner      = attribute.Layout;
            renderer.JsonEncode = attribute.Encode;

            string renderedValue;

            try
            {
                renderedValue = renderer.Render(source);
            }
            catch (Exception ex)
            {
                renderedValue = $"Render failed: {ex.GetType().Name} {ex.Message}";
            }

            if (string.IsNullOrEmpty(renderedValue))
            {
                return;
            }

            Mapper.HarvestStringToDictionary(dest,
                                             attribute.Name, renderedValue, "attributes_");
        }
Exemple #2
0
        public T Read(string json)
        {
            if (string.IsNullOrEmpty(json))
            {
                return(Empty());
            }

            try
            {
                var resultJsonObj = new T();
                var jObj          = JsonConvert.DeserializeObject(json) as JObject;
                if (jObj != null)
                {
                    foreach (KeyValuePair <string, JToken> item in jObj)
                    {
                        JsonAttribute jsonAttribute = _converter[item.Value.Type].Convert(item);
                        resultJsonObj.Add(jsonAttribute);
                    }
                }
                return(resultJsonObj);
            }
            catch
            {
                return(new T());
            }
        }
        public JEVisAttributeWS(JEVisDataSourceWS ds, JsonAttribute json)
        {
            this.ds   = ds;
            this.json = json;

            Optimization.getInstance().addAttribute(this);
        }
        public void update(JsonAttribute json)
        {
            this.json = json;

            /**
             * TODO: may call event?
             */
        }
        public JEVisAttributeWS(JEVisDataSourceWS ds, JsonAttribute json, Long obj)
        {
            this.ds   = ds;
            this.json = json;
            this.json.setObjectID(obj);

            Optimization.getInstance().addAttribute(this);
        }
        public virtual Dictionary <ProfileAttributeType, String> GetAttributes(String identifyer)
        {
            Dictionary <ProfileAttributeType, String> attributes = new Dictionary <ProfileAttributeType, String>();

            System.Security.Cryptography.SHA256 sha256 = new System.Security.Cryptography.SHA256CryptoServiceProvider();
            Byte[] inputBytes;
            Byte[] outputBytes;
            string outputString;
            string data = "";

            switch (Format)
            {
            case TimestampFormat.aaaammgghhmmss:
                data = DateTime.Now.ToString("yyyyMMddHHmm");
                break;
            }

            // Calcolo della chiave di hash

            inputBytes   = ASCIIEncoding.Default.GetBytes(identifyer + data + SecretKey);
            outputBytes  = sha256.ComputeHash(inputBytes);
            outputString = Convert(outputBytes);
            String url = RemoteApiUrl;

            if (RemoteApiUrl.Contains("{0}") && RemoteApiUrl.Contains("{1}"))
            {
                try
                {
                    WebClient webClient = new WebClient();
                    string    json      = webClient.DownloadString(String.Format(RemoteApiUrl, identifyer, outputString));
                    if (!String.IsNullOrEmpty(json) && json != "{\"result\":\"\"}")
                    {
                        var deserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                        var result       = (Dictionary <String, object>)deserializer.DeserializeObject(json);
                        if (result != null && Attributes != null && Attributes.Where(a => a.Deleted == BaseStatusDeleted.None).Any())
                        {
                            foreach (Dictionary <String, object> jStruct in result.Values.Where(v => v != null))
                            {
                                foreach (String key in jStruct.Keys)
                                {
                                    JsonAttribute attribute = Attributes.Where(a => a.Deleted == BaseStatusDeleted.None && a.Name == key.ToLower()).FirstOrDefault();
                                    if (attribute != null)
                                    {
                                        attributes.Add(attribute.Type, jStruct[key].ToString());
                                    }
                                }
                            }
                        }
                    }
                }
                catch (ArgumentException e)
                {
                }
            }
            return(attributes);
        }
Exemple #7
0
        /// <summary>
        /// Initializes the target. Can be used by inheriting classes
        /// to initialize logging.
        /// </summary>
        protected override void InitializeTarget()
        {
            foreach (var prop in Properties)
            {
                var attr = new JsonAttribute(prop.Name, prop.Value, !prop.IsNumber);
                TextClefLayout.Attributes.Add(attr);
                TemplatedClefLayout.Attributes.Add(attr);
            }

            base.InitializeTarget();
        }
        public string Evaluate(string value, TestResult result)
        {
            string[] indexes = GetBodyIndexes(value);

            JsonAttribute json = GetJsonAttributeToEvaluate(result);

            foreach (var index in indexes)
            {
                json = json[index];
            }

            var returnValue = json?.GetValue()?.ToString() ?? string.Empty;

            if (string.IsNullOrWhiteSpace(returnValue))
            {
                throw new ArgumentException($"Depedency {value} not found");
            }
            return(returnValue);
        }
Exemple #9
0
        private static void addOrUpdateAttributesToJsonFile(Dictionary <string, string> additionalParams)
        {
            if (additionalParams == null || additionalParams.Count() == 0)
            {
                return;
            }
            FileTarget target    = (FileTarget)LogManager.Configuration.FindTargetByName("jsonFile");
            JsonLayout curlayout = (JsonLayout)target.Layout;

            //HashSet<JsonAttribute> curAttributes = new HashSet<JsonAttribute> ( curlayout.Attributes ); //HashSet due to time efficiency considerations

            foreach (KeyValuePair <string, string> param in additionalParams)
            {
                JsonAttribute attr = curlayout.Attributes.FirstOrDefault(a => String.Equals(a.Name, param.Key, StringComparison.OrdinalIgnoreCase));
                if (attr != null)
                {
                    curlayout.Attributes.Remove(attr);
                }
                curlayout.Attributes.Add(new JsonAttribute(param.Key, param.Value));
            }
            LogManager.ReconfigExistingLoggers();
            //TODO: foreach config param check if value changed and don't call LogManager.ReconfigExistingLoggers() if no value changed
        }
Exemple #10
0
        public CustomComponentResult Invoke(Dictionary <string, string> urlParams, Dictionary <string, string> routeParams, object objs)
        {
            object[] paramObjects = null;
            JObject  jObject      = null;

            if (objs != null && !string.IsNullOrWhiteSpace(objs.ToString()))
            {
                jObject = JObject.Parse(objs.ToString());
            }
            if (parametersCount > 0)
            {
                paramObjects = new object[parametersCount];
            }
            Dictionary <Type, Dictionary <string, string> > paramDict = new Dictionary <Type, Dictionary <string, string> >
            {
                { typeof(UrlParamAttribute), urlParams },
                { typeof(RouteParamAttribute), routeParams }
            };

            bool hasParam = urlParams != null && urlParams.Count > 0 || routeParams != null && routeParams.Count > 0;

            for (int i = 0; i < parametersCount; i++)
            {
                if (parametersDict[i].ParameterType == typeof(RouteContext))
                {
                    paramObjects[i] = new RouteContext(urlParams, routeParams, objs);
                    continue;
                }

                if (hasParam)
                {
                    AssembleParam <UrlParamAttribute>(paramDict, paramObjects, i);
                    AssembleParam <RouteParamAttribute>(paramDict, paramObjects, i);
                }

                if (jObject != null && jObject.GetValue(parametersDict[i].Name) != null)
                {
                    Type t = parametersDict[i].ParameterType;
                    paramObjects[i] = JsonConvert.DeserializeObject(jObject[parametersDict[i].Name].ToString(), t);
                }
            }

            CustomComponentResult result = new CustomComponentResult();

            try
            {
                result.Data      = methodInfo.Invoke(targetObj, paramObjects);
                result.MediaType = "text/plain";
            }
            catch (TargetInvocationException tex)            //反调调用的方法 抛出的异常被封装在TargetInvocationException 里
            {
                string msg = $"类名:{methodInfo.DeclaringType.FullName},方法名:{methodInfo.Name}";
                if (tex.InnerException is BusinessLogicException)
                {
                    throw tex.InnerException;
                }

                throw new TargetInvocationException(msg, tex.InnerException);
            }
            JsonAttribute jsonAttribute = methodInfo.GetCustomAttribute <JsonAttribute>(false);

            if (jsonAttribute != null)
            {
                result.Data      = JsonConvert.SerializeObject(result.Data, Formatting.Indented);
                result.MediaType = "application/json";
            }
            return(result);
        }
 public void Add(JsonAttribute attr)
 {
     this.attribute.Set(attr);
 }
 public JsonObject()
 {
     this.attribute = new JsonAttribute();
 }
Exemple #13
0
        public string stringify <T>(T obj)
        {
            // null
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            string r = "{";

            foreach (PropertyInfo prop in obj.GetType().GetProperties())
            {
                JsonAttribute attr = prop.GetCustomAttributes <JsonAttribute>(false).FirstOrDefault();
                if (attr != null && attr.decodec != null)
                {
                    // need to use user-defined decoder
                    Decodec impl = (Decodec)Activator.CreateInstance(
                        Type.GetType(attr.decodec.FullName));

                    typeof(Factory).GetMethod("Builder", new Type[] { typeof(Decodec) })
                    .MakeGenericMethod(prop.PropertyType)
                    .Invoke(this, new object[] { impl });
                }
                if ((prop.PropertyType.IsGenericType && typeof(System.Collections.IEnumerable).IsAssignableFrom(prop.PropertyType)) ||
                    prop.PropertyType.IsArray)
                {
                    //collection type
                    Type type = prop.PropertyType.GetElementType() ?? prop.PropertyType.GetGenericArguments().Single();
                    if (IsSystemType(type))
                    {
                        // not system-defined type
                        r = r + "\"" + prop.Name + "\":[";
                        foreach (var el in prop.GetValue(obj, null) as System.Collections.IEnumerable)
                        {
                            r = r + stringify(el) + ",";
                        }
                        r = r.Substring(0, r.Length - 1);
                        r = r + "],";
                    }
                    else
                    {
                        // system-defined type
                        r = r + "\"" + prop.Name + "\":[";
                        foreach (var el in prop.GetValue(obj, null) as System.Collections.IEnumerable)
                        {
                            Decodec _decodec = GetCodec <Decodec>(type);
                            r = r + _decodec.Convert(el) + ",";
                        }
                        r = r.Substring(0, r.Length - 1) + "],";
                    }
                }
                else if (IsSystemType(prop.PropertyType))
                {
                    // not system-defined type
                    r = r + "\"" + prop.Name + "\":" + stringify(prop.GetValue(obj)) + ",";
                }
                else
                {
                    Decodec _decodec = GetCodec <Decodec>(prop.PropertyType);
                    r = r + "\"" + prop.Name + "\":" + _decodec.Convert(prop.GetValue(obj, null)) + ",";
                }
            }

            // remove last comma
            r = r.Substring(0, r.Length - 1);

            r = r + "}";

            return(r);
        }