public void SetNotification(string notification, string culture)
 {
     lock (locker)
     {
         notifications[culture] = notification;
         SetRecord record = new SetRecord();
         foreach (var pair in notifications)
         {
             record.Add(pair.Key, new ScalerRecord(pair.Value));
         }
         string     json   = Hake.Extension.ValueRecord.Json.Converter.Json(record);
         FileStream stream = File.OpenWrite(filePath);
         stream.SetLength(0);
         StreamWriter writer = new StreamWriter(stream);
         writer.Write(json);
         writer.Flush();
         stream.Flush();
         writer.Dispose();
         stream.Dispose();
     }
 }
Esempio n. 2
0
        public static RecordBase ToRecord(object input, bool ignoreKeyCase = false)
        {
            if (input == null)
            {
                return(new ScalerRecord(null));
            }

            Type valueType = input.GetType();

#if NETSTANDARD1_2
            TypeInfo valueTypeInfo = valueType.GetTypeInfo();
#endif

#if NETSTANDARD2_0 || NET452
            if (valueType.IsEnum)
#else
            if (valueTypeInfo.IsEnum)
#endif
            {
                string typeName   = $"{valueType.Namespace}.{valueType.Name}";
                string writeValue = $"{typeName}.{input}";
                return(new ScalerRecord(writeValue));
            }

#if NETSTANDARD2_0 || NET452
            if (valueType.IsPrimitive)
#else
            if (valueTypeInfo.IsPrimitive)
#endif
            { return(new ScalerRecord(input)); }

            if (valueType.Name == "String" && valueType.Namespace == "System")
            {
                return(new ScalerRecord(input));
            }

#if NETSTANDARD2_0 || NET452
            Type ienumType = valueType.GetInterface("System.Collections.IEnumerable");
#else
            Type     ienumType     = valueTypeInfo.ImplementedInterfaces.FirstOrDefault(t => t.FullName == "System.Collections.IEnumerable");
            TypeInfo ienumTypeInfo = ienumType == null ? null : ienumType.GetTypeInfo();
#endif
            if (ienumType != null)
            {
#if NETSTANDARD2_0 || NET452
                MethodInfo getEnumeratorMethod = ienumType.GetMethod("GetEnumerator");
#else
                MethodInfo getEnumeratorMethod = ienumTypeInfo.DeclaredMethods.FirstOrDefault(m => m.Name == "GetEnumerator");
#endif
                IEnumerator enumerator = (IEnumerator)getEnumeratorMethod.Invoke(input, null);
                ListRecord  listRecord = new ListRecord();
                while (enumerator.MoveNext())
                {
                    listRecord.Add(ToRecord(enumerator.Current, ignoreKeyCase));
                }
                return(listRecord);
            }

#if NETSTANDARD2_0 || NET452
            if (valueType.IsClass)
#else
            if (valueTypeInfo.IsClass)
#endif
            {
#if NETSTANDARD2_0 || NET452
                BindingFlags propertyFlags = BindingFlags.Instance;
#if PROPERTY_PUBLIC_ONLY
                propertyFlags |= BindingFlags.Public;
                PropertyInfo[] properties = valueType.GetProperties(propertyFlags);
#endif
#else
                PropertyInfo[] properties = valueTypeInfo.DeclaredProperties.ToArray();
#endif
                SetRecord            setRecord = new SetRecord(ignoreKeyCase);
                MapPropertyAttribute mapPropertyAttribute;
                MethodInfo           getMethod;
                string propertyName;
                object propertyValue;
                foreach (PropertyInfo property in properties)
                {
                    getMethod = property.GetMethod;
                    if (getMethod == null)
                    {
                        continue;
                    }
                    mapPropertyAttribute = property.GetCustomAttribute <MapPropertyAttribute>();
                    if (mapPropertyAttribute == null)
                    {
                        continue;
                    }
                    propertyName  = GetNameOrDefault(property, mapPropertyAttribute);
                    propertyValue = getMethod.Invoke(input, null);
                    if (mapPropertyAttribute.ConverterType == null)
                    {
                        setRecord.Add(propertyName, ToRecord(propertyValue, ignoreKeyCase));
                    }
                    else
                    {
#if NETSTANDARD2_0 || NET452
                        MethodInfo convertToScaler = mapPropertyAttribute.ConverterType.GetMethod(nameof(IScalerTargetTypeConverter <string, string> .ConvertToScaler));
#else
                        MethodInfo convertToScaler = mapPropertyAttribute.ConverterType.GetRuntimeMethods().First(method => method.Name == nameof(IScalerTargetTypeConverter <string, string> .ConvertToScaler));
#endif
                        object converterObject = Activator.CreateInstance(mapPropertyAttribute.ConverterType);
                        object scaler          = convertToScaler.Invoke(converterObject, new object[] { propertyValue });
                        setRecord.Add(propertyName, ToRecord(scaler, ignoreKeyCase));
                    }
                }
                return(setRecord);
            }

            throw new Exception($"can not map to record of type {valueType.Namespace}.{valueType.Name}");
        }
Esempio n. 3
0
        private static SetRecord ReadSet(InternalTextReader reader, bool ignoreKeyCase)
        {
            // resolve '{'
            reader.Read();

            SetRecord set = new SetRecord(ignoreKeyCase);
            char      peek;
            int       result;
            string    key      = "";
            int       state    = 0;
            int       oldstate = 0;

            while (true)
            {
                result = reader.Peek();
                if (result == -1)
                {
                    if (state == 0)
                    {
                        throw new Exception("'\"' excepted but end of stream reached");
                    }
                    else if (state == 1)
                    {
                        throw new Exception("':' excepted but end of stream reached");
                    }
                    else if (state == 2)
                    {
                        throw new Exception("',' or '}' excepted but end of stream reached");
                    }
                    else
                    {
                        throw new Exception($"unknown state of {state}");
                    }
                }

                peek = (char)result;
                if (state == 0)
                {
                    if (peek.IsWhiteSpace())
                    {
                        reader.Read();
                    }
                    else if (peek == '"')
                    {
                        key   = ReadStringOrThrow(reader);
                        state = 1;
                    }
                    else if (peek == '/')
                    {
                        oldstate = state; reader.Read(); state = 3;
                    }
                    else if (peek == '}')
                    {
                        reader.Read(); break;
                    }
                    else
                    {
                        throw BuildException($"'\"' excepted but '{peek}' scanned", reader);
                    }
                }
                else if (state == 1)
                {
                    if (peek.IsWhiteSpace())
                    {
                        reader.Read();
                    }
                    else if (peek == ':')
                    {
                        reader.Read();
                        RecordBase record = ReadJson(reader, false, ignoreKeyCase);
                        set.Add(key, record);
                        state = 2;
                    }
                    else if (peek == '/')
                    {
                        oldstate = state; reader.Read(); state = 3;
                    }
                    else
                    {
                        throw BuildException($"':' excepted but '{peek}' scanned", reader);
                    }
                }
                else if (state == 2)
                {
                    if (peek.IsWhiteSpace())
                    {
                        reader.Read();
                    }
                    else if (peek == '/')
                    {
                        oldstate = state; reader.Read(); state = 3;
                    }
                    else if (peek == ',')
                    {
                        reader.Read(); state = 0;
                    }
                    else if (peek == '}')
                    {
                        reader.Read(); break;
                    }
                    else
                    {
                        throw BuildException($"',' or '}}' excepted but '{peek}' scanned", reader);
                    }
                }
                else if (state == 3)
                {
                    if (peek == '/')
                    {
                        reader.Read(); state = 4;
                    }
                    else
                    {
                        throw BuildException($"'/' excepted but '{peek}' scanned", reader);
                    }
                }
                else if (state == 4)
                {
                    if (peek == '\n')
                    {
                        reader.Read(); state = oldstate;
                    }
                    else
                    {
                        reader.Read();
                    }
                }
                else
                {
                    throw new Exception($"unknown state of {state}");
                }
            }
            return(set);
        }