Exemple #1
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());

            DefaultContractResolver?resolver =
                serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName(
                (resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName
                );
            serializer.Serialize(
                writer,
                reflectionObject.GetValue(value, KeyName),
                reflectionObject.GetType(KeyName)
                );
            writer.WritePropertyName(
                (resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName
                );
            serializer.Serialize(
                writer,
                reflectionObject.GetValue(value, ValueName),
                reflectionObject.GetType(ValueName)
                );
            writer.WriteEndObject();
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            EnsureReflectionObject(value.GetType());
            DefaultContractResolver contractResolver = serializer.ContractResolver as DefaultContractResolver;
            string str  = (string)_reflectionObject.GetValue(value, "Key");
            object obj2 = _reflectionObject.GetValue(value, "Value");
            Type   type = obj2?.GetType();

            writer.WriteStartObject();
            writer.WritePropertyName((contractResolver != null) ? contractResolver.GetResolvedPropertyName("Key") : "Key");
            writer.WriteValue(str);
            writer.WritePropertyName((contractResolver != null) ? contractResolver.GetResolvedPropertyName("Type") : "Type");
            writer.WriteValue(type?.FullName);
            writer.WritePropertyName((contractResolver != null) ? contractResolver.GetResolvedPropertyName("Value") : "Value");
            if (type != null)
            {
                if (JsonSerializerInternalWriter.TryConvertToString(obj2, type, out string str2))
                {
                    writer.WriteValue(str2);
                }
                else
                {
                    writer.WriteValue(obj2);
                }
            }
            else
            {
                writer.WriteNull();
            }
            writer.WriteEndObject();
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            ReflectionObject        reflectionObject = KeyValuePairConverter.ReflectionObjectPerType.Get(value.GetType());
            DefaultContractResolver contractResolver = serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Key") : "Key"));
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Key"), reflectionObject.GetType("Key"));
            writer.WritePropertyName((contractResolver != null ? contractResolver.GetResolvedPropertyName("Value") : "Value"));
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Value"), reflectionObject.GetType("Value"));
            writer.WriteEndObject();
        }
Exemple #4
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter)
        {
            ReflectionObject reflectionObject = ReflectionObjectPerType.Get(value.GetType());

            DefaultContractResolver resolver = internalWriter.Serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyName) : KeyName);
            internalWriter.Serialize(writer, reflectionObject.GetValue(value, KeyName), reflectionObject.GetType(KeyName));
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValueName) : ValueName);
            internalWriter.Serialize(writer, reflectionObject.GetValue(value, ValueName), reflectionObject.GetType(ValueName));
            writer.WriteEndObject();
        }
Exemple #5
0
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            Attribute[] customAttributes = ReflectionUtils.GetAttributes(type, null, true);

            foreach (Attribute attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                // only test on attribute type name
                // attribute assembly could change because of type forwarding, etc
                if (string.Equals(attributeType.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            object[] customAttributes;
#if !PORTABLE
            customAttributes = type.GetCustomAttributes(false);
#else
            customAttributes = type.GetTypeInfo().GetCustomAttributes(false).Cast <object>().ToArray();
#endif

            foreach (var attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                // only test on attribute type name
                // attribute assembly could change because of type forwarding, etc
                if (string.Equals(attributeType.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
Exemple #7
0
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            Type metadataTypeAttributeType = GetMetadataTypeAttributeType();

            if (metadataTypeAttributeType == null)
            {
                return(null);
            }

            Attribute attribute = ReflectionUtils.GetAttributes(type, metadataTypeAttributeType, true).SingleOrDefault();

            if (attribute == null)
            {
                return(null);
            }

            const string metadataClassTypeName = "MetadataClassType";

            if (_metadataTypeAttributeReflectionObject == null)
            {
                _metadataTypeAttributeReflectionObject = ReflectionObject.Create(metadataTypeAttributeType, metadataClassTypeName);
            }

            return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
        }
        private static Type GetAssociateMetadataTypeFromAttribute(Type type)
        {
            object[] customAttributes;
#if !PORTABLE
            customAttributes = type.GetCustomAttributes(false);
#else
            customAttributes = type.GetTypeInfo().GetCustomAttributes(false).Cast <object>().ToArray();
#endif

            foreach (var attribute in customAttributes)
            {
                Type attributeType = attribute.GetType();

                if (string.Equals(attributeType.Name, "MetadataTypeAttribute", StringComparison.Ordinal) &&
                    attributeType.Assembly().FullName.StartsWith("System.ComponentModel.DataAnnotations", StringComparison.Ordinal))
                {
                    const string metadataClassTypeName = "MetadataClassType";

                    if (_metadataTypeAttributeReflectionObject == null)
                    {
                        _metadataTypeAttributeReflectionObject = ReflectionObject.Create(attributeType, metadataClassTypeName);
                    }

                    return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, metadataClassTypeName));
                }
            }

            return(null);
        }
Exemple #9
0
        // Token: 0x060014C0 RID: 5312 RVA: 0x0006E4E4 File Offset: 0x0006C6E4
        public override void WriteJson(JsonWriter writer, [Nullable(2)] object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            ReflectionObject        reflectionObject        = KeyValuePairConverter.ReflectionObjectPerType.Get(value.GetType());
            DefaultContractResolver defaultContractResolver = serializer.ContractResolver as DefaultContractResolver;

            writer.WriteStartObject();
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Key") : "Key");
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Key"), reflectionObject.GetType("Key"));
            writer.WritePropertyName((defaultContractResolver != null) ? defaultContractResolver.GetResolvedPropertyName("Value") : "Value");
            serializer.Serialize(writer, reflectionObject.GetValue(value, "Value"), reflectionObject.GetType("Value"));
            writer.WriteEndObject();
        }
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            EnsureReflectionObject(value.GetType());
            MiscellaneousUtils.Assert(_reflectionObject != null);

            DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;

            string keyName  = (string)_reflectionObject.GetValue(value, KeyPropertyName);
            object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);

            Type keyValueType = keyValue?.GetType();

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
            writer.WriteValue(keyName);
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
            writer.WriteValue(keyValueType?.FullName);

            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);

            if (keyValueType != null)
            {
                if (JsonSerializerInternalWriter.TryConvertToString(keyValue, keyValueType, out string valueJson))
                {
                    writer.WriteValue(valueJson);
                }
                else
                {
                    writer.WriteValue(keyValue);
                }
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
Exemple #11
0
        private byte[] GetByteArray(object value)
        {
            if (value.GetType().AssignableToTypeName(BinaryTypeName))
            {
                EnsureReflectionObject(value.GetType());
                return((byte[])_reflectionObject.GetValue(value, BinaryToArrayName));
            }

            throw new JsonSerializationException("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object value, JsonSerializerWriter internalWriter)
        {
            EnsureReflectionObject(value.GetType());

            DefaultContractResolver resolver = internalWriter.Serializer.ContractResolver as DefaultContractResolver;

            string keyName  = (string)_reflectionObject.GetValue(value, KeyPropertyName);
            object keyValue = _reflectionObject.GetValue(value, ValuePropertyName);

            Type keyValueType = (keyValue != null) ? keyValue.GetType() : null;

            writer.WriteStartObject();
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(KeyPropertyName) : KeyPropertyName);
            writer.WriteValue(keyName);
            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(TypePropertyName) : TypePropertyName);
            writer.WriteValue((keyValueType != null) ? keyValueType.FullName : null);

            writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(ValuePropertyName) : ValuePropertyName);

            if (keyValueType != null)
            {
                string valueJson;
                if (JsonSerializerWriter.TryConvertToString(keyValue, keyValueType, out valueJson))
                {
                    writer.WriteValue(valueJson);
                }
                else
                {
                    writer.WriteValue(keyValue);
                }
            }
            else
            {
                writer.WriteNull();
            }

            writer.WriteEndObject();
        }
        private byte[] GetByteArray(object value)
        {
#if HAVE_LINQ
            if (value.GetType().FullName == BinaryTypeName)
            {
                EnsureReflectionObject(value.GetType());
                return((byte[])_reflectionObject.GetValue(value, BinaryToArrayName));
            }
#endif
            if (value is SqlBinary)
            {
                return(((SqlBinary)value).Value);
            }

            throw new JsonSerializationException("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
 private static Type GetAssociateMetadataTypeFromAttribute(Type type)
 {
     foreach (Attribute attribute in ReflectionUtils.GetAttributes(type, null, true))
     {
         Type t = attribute.GetType();
         if (string.Equals(t.FullName, "System.ComponentModel.DataAnnotations.MetadataTypeAttribute", StringComparison.Ordinal))
         {
             if (_metadataTypeAttributeReflectionObject == null)
             {
                 string[] memberNames = new string[] { "MetadataClassType" };
                 _metadataTypeAttributeReflectionObject = ReflectionObject.Create(t, memberNames);
             }
             return((Type)_metadataTypeAttributeReflectionObject.GetValue(attribute, "MetadataClassType"));
         }
     }
     return(null);
 }
        private byte[] GetByteArray(object value)
        {
#if !(NET20)
            if (value.GetType().AssignableToTypeName(BinaryTypeName))
            {
                EnsureReflectionObject(value.GetType());
                return((byte[])_reflectionObject.GetValue(value, BinaryToArrayName));
            }
#endif
#if !UNITYPROFILE
            if (value is SqlBinary)
            {
                return(((SqlBinary)value).Value);
            }
#endif

            throw new JsonSerializationException("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
Exemple #16
0
        private byte[] GetByteArray(object value)
        {
#if HAVE_LINQ
            if (value.GetType().FullName == BinaryTypeName)
            {
                EnsureReflectionObject(value.GetType());
                MiscellaneousUtils.Assert(_reflectionObject != null);

                return((byte[])_reflectionObject.GetValue(value, BinaryToArrayName));
            }
#endif
#if HAVE_ADO_NET
            if (value is SqlBinary binary)
            {
                return(binary.Value);
            }
#endif

            throw new JsonSerializationException("Unexpected value type when writing binary: {0}".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }