Пример #1
0
        public void Serialize(Serializer so, object val, bool noMarker = false)
        {
            var properties = val.GetType().GetProperties()
                             .Where(p => p.Name != p.Name.ToLower())
                             .ToArray();

            var invalid = properties.FirstOrDefault(p => !SerializedTypeHelper.InverseFieldsMap.ContainsKey(p.Name));

            if (invalid != null)
            {
                throw new InvalidOperationException("JSON contains unknown field: '" + invalid.Name + "'");
            }

            properties = SerializedTypeHelper.SortProperties(properties);

            foreach (var property in properties)
            {
                var pval = property.GetValue(val);
                if (pval == null)
                {
                    continue;
                }

                SerializedTypeHelper.Serialize(so, property.Name, pval);
            }

            if (!noMarker)
            {
                //Object ending marker
                SerializedTypeHelper.STInt8.Serialize(so, 0xe1);
            }
        }
Пример #2
0
        public void Serialize(Serializer so, IEnumerable val, bool noMarker = false)
        {
            foreach (var item in val)
            {
                if (item == null || item.GetType().IsValueType)
                {
                    throw new ArgumentException("Cannot serialize an array containing null or value type object.");
                }

                var properties = item.GetType().GetProperties();
                if (properties.Length != 1)
                {
                    throw new ArgumentException("Cannot serialize an array containing non-single-key objects");
                }

                var property = properties.First();
                SerializedTypeHelper.Serialize(so, property.Name, property.GetValue(item));
            }

            //Array ending marker
            SerializedTypeHelper.STInt8.Serialize(so, 0xf1);
        }
Пример #3
0
        public void Serialize(Serializer so, MemoDataInfo val, bool noMarker = false)
        {
            var properties = val.GetType().GetProperties()
                             .Where(p => p.Name != p.Name.ToLower())
                             .ToArray();

            var invalid = properties.FirstOrDefault(p => !SerializedTypeHelper.InverseFieldsMap.ContainsKey(p.Name));

            if (invalid != null)
            {
                throw new InvalidOperationException("JSON contains unknown field: '" + invalid.Name + "'");
            }

            properties = SerializedTypeHelper.SortProperties(properties);

            // memo format always be string
            var isJson = val.MemoFormat == "json";

            foreach (var property in properties)
            {
                object value = property.GetValue(val);
                if (value == null)
                {
                    continue;
                }

                switch (property.Name)
                {
                case "MemoType":
                    value = SerializedTypeHelper.ConvertStringToHex(val.MemoType);
                    break;

                case "MemoFormat":
                    value = SerializedTypeHelper.ConvertStringToHex(val.MemoFormat);
                    break;

                case "MemoData":
                    if (val.MemoData is string str)
                    {
                        value = SerializedTypeHelper.ConvertStringToHex(str);
                        break;
                    }

                    if (isJson)
                    {
                        var json = JsonConvert.SerializeObject(val.MemoData);
                        value = SerializedTypeHelper.ConvertStringToHex(json);
                        break;
                    }

                    throw new InvalidOperationException("MemoData can only be a JSON object with a valid json MemoFormat.");
                }

                SerializedTypeHelper.Serialize(so, property.Name, value);
            }

            if (!noMarker)
            {
                //Object ending marker
                SerializedTypeHelper.STInt8.Serialize(so, 0xe1);
            }
        }