예제 #1
0
        private void SerializeObject(object obj)
        {
            var lazyMgr = obj.As <ILazyManager>();
            var flag    = new AssertFlag();
            var type    = obj.GetType();

            _jsonWriter.WriteStartObject();

            foreach (var metadata in _context.GetProperties(type, () => _option.ContractResolver.GetProperties(type)))
            {
                if (metadata.Filter(metadata.PropertyInfo, lazyMgr))
                {
                    continue;
                }

                var value = metadata.Getter.Invoke(obj);
                if (_option.NullValueHandling == NullValueHandling.Ignore && value == null)
                {
                    continue;
                }

                if (!flag.AssertTrue())
                {
                    _jsonWriter.WriteComma();
                }

                _jsonWriter.WriteKey(SerializeName(metadata.PropertyName));

                _context.SerializeInfo = new PropertySerialzeInfo(metadata);

                if (value == null)
                {
                    _jsonWriter.WriteNull();
                }
                else
                {
                    //如果在属性上指定了 JsonConverter
                    if (metadata.Converter is JsonConverter converter)
                    {
                        converter.WriteJson(_serializer, _jsonWriter, value);
                    }
                    else
                    {
                        var objType = metadata.PropertyInfo.PropertyType == typeof(object) ? value.GetType() : metadata.PropertyInfo.PropertyType;
                        Serialize(value, objType);
                    }
                }

                _context.SerializeInfo = null;
            }

            _jsonWriter.WriteEndObject();
        }
예제 #2
0
        private void SerializeObject(object obj, bool startEle)
        {
            var lazyMgr = obj.As <ILazyManager>();
            var flag    = new AssertFlag();
            var type    = obj.GetType();

            if (startEle)
            {
                _xmlWriter.WriteStartElement(GetElementName(type));
            }

            var queue = new PriorityActionQueue();

            foreach (var metadata in _context.GetProperties(type, () => _option.ContractResolver.GetProperties(type)))
            {
                if (metadata.Filter(metadata.PropertyInfo, lazyMgr))
                {
                    continue;
                }

                var value = metadata.Getter.Invoke(obj);
                if ((_option.NullValueHandling == NullValueHandling.Ignore && value == null) ||
                    (value == null && _option.NodeStyle == XmlNodeStyle.Attribute))
                {
                    continue;
                }

                var objType = metadata.PropertyInfo.PropertyType == typeof(object) && value != null?value.GetType() : metadata.PropertyInfo.PropertyType;

                if (_option.NodeStyle == XmlNodeStyle.Attribute && objType.IsStringable())
                {
                    queue.Add(0, () =>
                    {
                        _context.SerializeInfo = new PropertySerialzeInfo(metadata);
                        if (metadata.Converter != null)
                        {
                            _xmlWriter.WriteAttributeString(metadata.PropertyName, metadata.Converter.WriteObject(_serializer, value));
                        }
                        else
                        {
                            _xmlWriter.WriteAttributeString(metadata.PropertyName, value.ToString());
                        }

                        _context.SerializeInfo = null;
                    });
                }
                else
                {
                    queue.Add(1, () =>
                    {
                        _context.SerializeInfo = new PropertySerialzeInfo(metadata);
                        if (metadata.Converter != null && metadata.Converter is XmlConverter converter)
                        {
                            WriteXmlElement(metadata.PropertyName, true, () => converter.WriteXml(_serializer, _xmlWriter, value));
                        }
                        else
                        {
                            WriteXmlElement(metadata.PropertyName, true, () => Serialize(value, type: objType));
                        }

                        _context.SerializeInfo = null;
                    });
                }
            }

            queue.Invoke();

            if (startEle)
            {
                _xmlWriter.WriteEndElement();
            }
        }