示例#1
0
        protected private JsonProperty(JsonObjectContract <TOwner> parent, MemberInfo member)
        {
            if (member == null)
            {
                throw new ArgumentNullException(nameof(member));
            }

            Parent         = parent ?? throw new ArgumentNullException(nameof(parent));
            DeclaringType  = member.DeclaringType;
            UnderlyingName = member.Name;

            var propertyAttribute = ReflectionHelpers.GetAttribute <JsonPropertyAttribute>(member);
            var mappedName        = member.Name;

            if (propertyAttribute != null)
            {
                Order          = propertyAttribute.Order;
                Required       = propertyAttribute.Required;
                SerializeNulls = propertyAttribute.ShouldSerializeNulls() ?? parent.Settings.SerializeNulls;

                if (propertyAttribute.Name != null)
                {
                    mappedName = propertyAttribute.Name;
                }
            }
            else
            {
                SerializeNulls = parent.Settings.SerializeNulls;
            }

            Name                    = JsonPropertyName.GetOrCreate(mappedName);
            PropertyType            = ReflectionHelpers.GetFieldOrPropertyType(member);
            NonNullablePropertyType = Nullable.GetUnderlyingType(PropertyType) ?? PropertyType;
            Converter               = ReflectionHelpers.GetAttribute <JsonConverterAttribute>(member)?.CreateInstance(NonNullablePropertyType);
        }
示例#2
0
        public override void WriteValue(JsonWriter writer, Object value)
        {
            writer.WriteStartObject();

            foreach (var item in (IEnumerable <KeyValuePair <String, Object> >)value)
            {
                writer.WritePropertyName(JsonPropertyName.GetOrCreate(item.Key));
                writer.WriteValue(item.Value);
            }

            writer.WriteEndObject();
        }