protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
        {
            JsonPrimitiveContract jsonPrimitiveContract = new JsonPrimitiveContract(objectType);

            this.InitializeContract(jsonPrimitiveContract);
            return(jsonPrimitiveContract);
        }
Exemplo n.º 2
0
        private string GetPropertyName(JsonWriter writer, object name, JsonContract contract, out bool escape)
        {
            string propertyName;

            if (contract.ContractType == JsonContractType.Primitive)
            {
                JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
                if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTime || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
                {
                    escape = false;
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeString(sw, (DateTime)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(sw.ToString());
                }
                else
                {
                    escape = true;
                    return(Convert.ToString(name, CultureInfo.InvariantCulture));
                }
            }
            else if (TryConvertToString(name, name.GetType(), out propertyName))
            {
                escape = true;
                return(propertyName);
            }
            else
            {
                escape = true;
                return(name.ToString());
            }
        }
 private string GetPropertyName(JsonWriter writer, object name, JsonContract contract, out bool escape)
 {
     if (contract.ContractType == JsonContractType.Primitive)
     {
         JsonPrimitiveContract jsonPrimitiveContract = (JsonPrimitiveContract)contract;
         if (jsonPrimitiveContract.TypeCode == PrimitiveTypeCode.DateTime || jsonPrimitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
         {
             escape = false;
             StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
             DateTimeUtils.WriteDateTimeString(stringWriter, (DateTime)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
             return(stringWriter.ToString());
         }
         if (jsonPrimitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffset || jsonPrimitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
         {
             escape = false;
             StringWriter stringWriter2 = new StringWriter(CultureInfo.InvariantCulture);
             DateTimeUtils.WriteDateTimeOffsetString(stringWriter2, (DateTimeOffset)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
             return(stringWriter2.ToString());
         }
         escape = true;
         return(Convert.ToString(name, CultureInfo.InvariantCulture));
     }
     else
     {
         string result;
         if (JsonSerializerInternalWriter.TryConvertToString(name, name.GetType(), out result))
         {
             escape = true;
             return(result);
         }
         escape = true;
         return(name.ToString());
     }
 }
        protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
        {
            JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);

            this.method_7(contract);
            return(contract);
        }
        private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
        {
            if (contract.UnderlyingType == typeof(byte[]))
            {
                bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
                if (includeTypeDetails)
                {
                    writer.WriteStartObject();
                    WriteTypeProperty(writer, contract.CreatedType);
                    writer.WritePropertyName(JsonTypeReflector.ValuePropertyName);

                    if (contract.IsNullable)
                    {
                        writer.WriteValue(value, true);
                    }
                    else
                    {
                        writer.WriteValue(value);
                    }

                    writer.WriteEndObject();
                    return;
                }
            }

            if (contract.IsNullable)
            {
                writer.WriteValue(value, true);
            }
            else
            {
                writer.WriteValue(value);
            }
        }
    private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
    {
      if (contract.UnderlyingType == typeof (byte[]))
      {
        bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
        if (includeTypeDetails)
        {
          writer.WriteStartObject();
          WriteTypeProperty(writer, contract.CreatedType);
          writer.WritePropertyName(JsonTypeReflector.ValuePropertyName);

          if (contract.IsNullable)
            writer.WriteValue(value, true);
          else
            writer.WriteValue(value);

          writer.WriteEndObject();
          return;
        }
      }

      if (contract.IsNullable)
        writer.WriteValue(value, true);
      else
        writer.WriteValue(value);
    }
Exemplo n.º 7
0
 private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
 {
   if (contract.UnderlyingType == typeof (byte[]) && this.ShouldWriteType(TypeNameHandling.Objects, (JsonContract) contract, member, containerContract, containerProperty))
   {
     writer.WriteStartObject();
     this.WriteTypeProperty(writer, contract.CreatedType);
     writer.WritePropertyName("$value");
     writer.WriteValue(value);
     writer.WriteEndObject();
   }
   else
     writer.WriteValue(value);
 }
 private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContract collectionValueContract)
 {
     if (contract.UnderlyingType != typeof(byte[]) || !this.ShouldWriteType(TypeNameHandling.Objects, contract, member, collectionValueContract))
     {
         writer.WriteValue(value);
         return;
     }
     writer.WriteStartObject();
     this.WriteTypeProperty(writer, contract.CreatedType);
     writer.WritePropertyName("$value");
     writer.WriteValue(value);
     writer.WriteEndObject();
 }
Exemplo n.º 9
0
 private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
 {
     if (contract.TypeCode != PrimitiveTypeCode.Bytes || !this.ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty))
     {
         JsonWriter.WriteValue(writer, contract.TypeCode, value);
         return;
     }
     writer.WriteStartObject();
     this.WriteTypeProperty(writer, contract.CreatedType);
     writer.WritePropertyName("$value", false);
     JsonWriter.WriteValue(writer, contract.TypeCode, value);
     writer.WriteEndObject();
 }
Exemplo n.º 10
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType)
                ?? primitiveContract.UnderlyingType;

            if (type.GetTypeInfo().IsEnum)
                return CreateEnumSchema(primitiveContract, type);

            if (PrimitiveTypeMap.ContainsKey(type))
                return PrimitiveTypeMap[type]();

            // None of the above, fallback to string
            return new Schema { Type = "string" };
        }
Exemplo n.º 11
0
 private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
 {
     if (contract.UnderlyingType == typeof(byte[]) && this.ShouldWriteType(TypeNameHandling.Objects, (JsonContract)contract, member, containerContract, containerProperty))
     {
         writer.WriteStartObject();
         this.WriteTypeProperty(writer, contract.CreatedType);
         writer.WritePropertyName("$value");
         writer.WriteValue(value);
         writer.WriteEndObject();
     }
     else
     {
         writer.WriteValue(value);
     }
 }
        private string GetPropertyName(JsonWriter writer, DictionaryEntry entry, JsonContract contract, out bool escape)
        {
            object key = entry.Key;

            string propertyName;

            JsonPrimitiveContract primitiveContract = contract as JsonPrimitiveContract;

            if (primitiveContract != null)
            {
                if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTime || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
                {
                    escape = false;
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeString(sw, (DateTime)key, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(sw.ToString());
                }
#if !NET20
                else if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffset || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
                {
                    escape = false;
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeOffsetString(sw, (DateTimeOffset)key, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(sw.ToString());
                }
#endif
                else
                {
                    escape = true;
                    return(Convert.ToString(key, CultureInfo.InvariantCulture));
                }
            }
            else if (TryConvertToString(key, key.GetType(), out propertyName))
            {
                escape = true;
                return(propertyName);
            }
            else
            {
                escape = true;
                return(key.ToString());
            }
        }
        public void TryGetItemSerializationInfo_should_throw_when_contract_has_a_converter()
        {
            var wrappedSerializer = new Newtonsoft.Json.JsonSerializer();
            var intContract = new Newtonsoft.Json.Serialization.JsonPrimitiveContract(typeof(int))
            {
                Converter = Substitute.For<Newtonsoft.Json.JsonConverter>()
            };
            wrappedSerializer.ContractResolver = new DictionaryContractResolver(
                new Dictionary<Type, JsonContract>
                {
                    { typeof(int), intContract }
                });
            var subject = new JsonDotNetSerializer<int[]>(wrappedSerializer);

            BsonSerializationInfo info;
            Action action = () => subject.TryGetItemSerializationInfo(out info);

            action.ShouldThrow<BsonSerializationException>().And.Message.Contains("has a Converter");
        }
Exemplo n.º 14
0
        public void TryGetItemSerializationInfo_should_throw_when_contract_has_a_converter()
        {
            var wrappedSerializer = new Newtonsoft.Json.JsonSerializer();
            var intContract       = new Newtonsoft.Json.Serialization.JsonPrimitiveContract(typeof(int))
            {
                Converter = Substitute.For <Newtonsoft.Json.JsonConverter>()
            };

            wrappedSerializer.ContractResolver = new DictionaryContractResolver(
                new Dictionary <Type, JsonContract>
            {
                { typeof(int), intContract }
            });
            var subject = new JsonSerializerAdapter <int[]>(wrappedSerializer);

            BsonSerializationInfo info;
            Action action = () => subject.TryGetItemSerializationInfo(out info);

            action.ShouldThrow <BsonSerializationException>().And.Message.Contains("has a Converter");
        }
Exemplo n.º 15
0
        private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
        {
            if (contract.TypeCode == PrimitiveTypeCode.Bytes)
            {
                // if type name handling is enabled then wrap the base64 byte string in an object with the type name
                bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
                if (includeTypeDetails)
                {
                    writer.WriteStartObject();
                    WriteTypeProperty(writer, contract.CreatedType);
                    writer.WritePropertyName(JsonTypeReflector.ValuePropertyName, false);

                    JsonWriter.WriteValue(writer, contract.TypeCode, value);

                    writer.WriteEndObject();
                    return;
                }
            }

            JsonWriter.WriteValue(writer, contract.TypeCode, value);
        }
Exemplo n.º 16
0
    private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
    {
      if (contract.TypeCode == PrimitiveTypeCode.Bytes)
      {
        // if type name handling is enabled then wrap the base64 byte string in an object with the type name
        bool includeTypeDetails = ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty);
        if (includeTypeDetails)
        {
          writer.WriteStartObject();
          WriteTypeProperty(writer, contract.CreatedType);
          writer.WritePropertyName(JsonTypeReflector.ValuePropertyName, false);

          JsonWriter.WriteValue(writer, contract.TypeCode, value);

          writer.WriteEndObject();
          return;
        }
      }

      JsonWriter.WriteValue(writer, contract.TypeCode, value);
    }
        private string GetPropertyName(
            JsonWriter writer,
            object name,
            JsonContract contract,
            out bool escape)
        {
            if (contract.ContractType == JsonContractType.Primitive)
            {
                JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
                if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTime || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeNullable)
                {
                    DateTime dateTime = DateTimeUtils.EnsureDateTime((DateTime)name, writer.DateTimeZoneHandling);
                    escape = false;
                    StringWriter stringWriter = new StringWriter((IFormatProvider)CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeString((TextWriter)stringWriter, dateTime, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(stringWriter.ToString());
                }
                if (primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffset || primitiveContract.TypeCode == PrimitiveTypeCode.DateTimeOffsetNullable)
                {
                    escape = false;
                    StringWriter stringWriter = new StringWriter((IFormatProvider)CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeOffsetString((TextWriter)stringWriter, (DateTimeOffset)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(stringWriter.ToString());
                }
                escape = true;
                return(Convert.ToString(name, (IFormatProvider)CultureInfo.InvariantCulture));
            }
            string s;

            if (JsonSerializerInternalWriter.TryConvertToString(name, name.GetType(), out s))
            {
                escape = true;
                return(s);
            }
            escape = true;
            return(name.ToString());
        }
Exemplo n.º 18
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                ?? _jsonSerializerSettings.Converters.OfType<StringEnumConverter>().FirstOrDefault();

            if (_describeAllEnumsAsStrings || stringEnumConverter != null || _autoRestEnumSupport != null)
            {
                var camelCase = _describeStringEnumsInCamelCase
                    || (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                return new Schema
                {
                    type = "string",
                    @enum = camelCase
                        ? type.GetEnumNamesForSerialization().Select(name => name.ToCamelCase()).ToArray()
                        : type.GetEnumNamesForSerialization(),
                    xmsenum = _autoRestEnumSupport != null ? new MSEnumExtension
                        {
                            name = type.Name,
                            modelAsString = _autoRestEnumSupport == AutoRestEnumSupportType.StaticStrings,
                        } : null
                };
            }
            
            return new Schema
            {
                type = "integer",
                format = "int32",
                @enum = type.GetEnumValues().Cast<object>().ToArray()
            };
        }
 // Token: 0x06000BEE RID: 3054
 // RVA: 0x000466EC File Offset: 0x000448EC
 private void SerializePrimitive(JsonWriter writer, object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
 {
     if (contract.TypeCode == PrimitiveTypeCode.Bytes && this.ShouldWriteType(TypeNameHandling.Objects, contract, member, containerContract, containerProperty))
     {
         writer.WriteStartObject();
         this.WriteTypeProperty(writer, contract.CreatedType);
         writer.WritePropertyName("$value", false);
         JsonWriter.WriteValue(writer, contract.TypeCode, value);
         writer.WriteEndObject();
         return;
     }
     JsonWriter.WriteValue(writer, contract.TypeCode, value);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a <see cref="JsonPrimitiveContract"/> for the given type.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <returns>A <see cref="JsonPrimitiveContract"/> for the given type.</returns>
        protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
        {
            JsonPrimitiveContract contract = new JsonPrimitiveContract(objectType);
              InitializeContract(contract);

              return contract;
        }
Exemplo n.º 21
0
 protected virtual JsonPrimitiveContract CreatePrimitiveContract(Type objectType)
 {
   JsonPrimitiveContract primitiveContract = new JsonPrimitiveContract(objectType);
   this.InitializeContract((JsonContract) primitiveContract);
   return primitiveContract;
 }
Exemplo n.º 22
0
        private string GetPropertyName(JsonWriter writer, object name, JsonContract contract, out bool escape)
        {
            if (contract.ContractType == JsonContractType.Primitive)
            {
                JsonPrimitiveContract primitiveContract = (JsonPrimitiveContract)contract;
                switch (primitiveContract.TypeCode)
                {
                case PrimitiveTypeCode.DateTime:
                case PrimitiveTypeCode.DateTimeNullable:
                {
                    DateTime dt = DateTimeUtils.EnsureDateTime((DateTime)name, writer.DateTimeZoneHandling);

                    escape = false;
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeString(sw, dt, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(sw.ToString());
                }

#if HAVE_DATE_TIME_OFFSET
                case PrimitiveTypeCode.DateTimeOffset:
                case PrimitiveTypeCode.DateTimeOffsetNullable:
                {
                    escape = false;
                    StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                    DateTimeUtils.WriteDateTimeOffsetString(sw, (DateTimeOffset)name, writer.DateFormatHandling, writer.DateFormatString, writer.Culture);
                    return(sw.ToString());
                }
#endif
                case PrimitiveTypeCode.Double:
                case PrimitiveTypeCode.DoubleNullable:
                {
                    double d = (double)name;

                    escape = false;
                    return(d.ToString("R", CultureInfo.InvariantCulture));
                }

                case PrimitiveTypeCode.Single:
                case PrimitiveTypeCode.SingleNullable:
                {
                    float f = (float)name;

                    escape = false;
                    return(f.ToString("R", CultureInfo.InvariantCulture));
                }

                default:
                {
                    escape = true;

                    if (primitiveContract.IsEnum && EnumUtils.TryToString(primitiveContract.NonNullableUnderlyingType, name, null, out string enumName))
                    {
                        return(enumName);
                    }

                    return(Convert.ToString(name, CultureInfo.InvariantCulture));
                }
                }
            }
            else if (TryConvertToString(name, name.GetType(), out string propertyName))
            {
                escape = true;
                return(propertyName);
            }
            else
            {
                escape = true;
                return(name.ToString());
            }
        }
Exemplo n.º 23
0
        private object CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, object existingValue)
        {
            CheckedRead(reader);
            string text = null;

            if (reader.TokenType == JsonToken.PropertyName)
            {
                bool flag;
                do
                {
                    string a = reader.Value.ToString();
                    if (string.Equals(a, "$ref", StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        if (reader.TokenType != JsonToken.String && reader.TokenType != JsonToken.Null)
                        {
                            throw new JsonSerializationException("JSON reference {0} property must have a string or null value.".FormatWith(CultureInfo.InvariantCulture, "$ref"));
                        }
                        string text2 = ((reader.Value != null) ? reader.Value.ToString() : null);
                        CheckedRead(reader);
                        if (text2 != null)
                        {
                            if (reader.TokenType == JsonToken.PropertyName)
                            {
                                throw new JsonSerializationException("Additional content found in JSON reference object. A JSON reference object should only have a {0} property.".FormatWith(CultureInfo.InvariantCulture, "$ref"));
                            }
                            return(base.Serializer.ReferenceResolver.ResolveReference(this, text2));
                        }
                        flag = true;
                    }
                    else if (string.Equals(a, "$type", StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        string text3 = reader.Value.ToString();
                        CheckedRead(reader);
                        if ((member?.TypeNameHandling ?? base.Serializer.TypeNameHandling) != 0)
                        {
                            ReflectionUtils.SplitFullyQualifiedTypeName(text3, out var typeName, out var assemblyName);
                            Type type;
                            try
                            {
                                type = base.Serializer.Binder.BindToType(assemblyName, typeName);
                            }
                            catch (Exception innerException)
                            {
                                throw new JsonSerializationException("Error resolving type specified in JSON '{0}'.".FormatWith(CultureInfo.InvariantCulture, text3), innerException);
                            }
                            if (type == null)
                            {
                                throw new JsonSerializationException("Type specified in JSON '{0}' was not resolved.".FormatWith(CultureInfo.InvariantCulture, text3));
                            }
                            if (!(objectType?.IsAssignableFrom(type) ?? true))
                            {
                                throw new JsonSerializationException("Type specified in JSON '{0}' is not compatible with '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.AssemblyQualifiedName, objectType.AssemblyQualifiedName));
                            }
                            objectType = type;
                            contract   = GetContractSafe(type);
                        }
                        flag = true;
                    }
                    else if (string.Equals(a, "$id", StringComparison.Ordinal))
                    {
                        CheckedRead(reader);
                        text = ((reader.Value != null) ? reader.Value.ToString() : null);
                        CheckedRead(reader);
                        flag = true;
                    }
                    else
                    {
                        if (string.Equals(a, "$values", StringComparison.Ordinal))
                        {
                            CheckedRead(reader);
                            object result = CreateList(reader, objectType, contract, member, existingValue, text);
                            CheckedRead(reader);
                            return(result);
                        }
                        flag = false;
                    }
                }while (flag && reader.TokenType == JsonToken.PropertyName);
            }
            if (!HasDefinedType(objectType))
            {
                return(CreateJObject(reader));
            }
            if (contract == null)
            {
                throw new JsonSerializationException("Could not resolve type '{0}' to a JsonContract.".FormatWith(CultureInfo.InvariantCulture, objectType));
            }
            JsonDictionaryContract jsonDictionaryContract = contract as JsonDictionaryContract;

            if (jsonDictionaryContract != null)
            {
                if (existingValue == null)
                {
                    return(CreateAndPopulateDictionary(reader, jsonDictionaryContract, text));
                }
                return(PopulateDictionary(jsonDictionaryContract.CreateWrapper(existingValue), reader, jsonDictionaryContract, text));
            }
            JsonObjectContract jsonObjectContract = contract as JsonObjectContract;

            if (jsonObjectContract != null)
            {
                if (existingValue == null)
                {
                    return(CreateAndPopulateObject(reader, jsonObjectContract, text));
                }
                return(PopulateObject(existingValue, reader, jsonObjectContract, text));
            }
            JsonPrimitiveContract jsonPrimitiveContract = contract as JsonPrimitiveContract;

            if (jsonPrimitiveContract != null && reader.TokenType == JsonToken.PropertyName && string.Equals(reader.Value.ToString(), "$value", StringComparison.Ordinal))
            {
                CheckedRead(reader);
                object result2 = CreateValueInternal(reader, objectType, jsonPrimitiveContract, member, existingValue);
                CheckedRead(reader);
                return(result2);
            }
            JsonISerializableContract jsonISerializableContract = contract as JsonISerializableContract;

            if (jsonISerializableContract != null)
            {
                return(CreateISerializable(reader, jsonISerializableContract, text));
            }
            throw new JsonSerializationException("Cannot deserialize JSON object into type '{0}'.".FormatWith(CultureInfo.InvariantCulture, objectType));
        }
Exemplo n.º 24
0
        private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type type)
        {
            var stringEnumConverter = primitiveContract.Converter as StringEnumConverter
                ?? _jsonSerializerSettings.Converters.OfType<StringEnumConverter>().FirstOrDefault();

            if (_describeAllEnumsAsStrings || stringEnumConverter != null)
            {
                var camelCase = _describeStringEnumsInCamelCase
                    || (stringEnumConverter != null && stringEnumConverter.CamelCaseText);

                return new Schema
                {
                    type = "string",
                    @enum = camelCase
                        ? type.GetEnumNames().Select(name => name.ToCamelCase()).ToArray()
                        : type.GetEnumNames()
                };
            }

            return new Schema
            {
                type = "integer",
                format = "int32",
                @enum = type.GetEnumValues().Cast<object>().ToArray()
            };
        }
Exemplo n.º 25
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
            {
                var converter = primitiveContract.Converter;
                var describeAsString = _describeAllEnumsAsStrings 
                    || (converter != null && converter.GetType() == typeof(StringEnumConverter));

                return describeAsString
                    ? new Schema { type = "string", @enum = type.GetEnumNames() }
                    : new Schema { type = "integer", format = "int32", @enum = type.GetEnumValues().Cast<object>().ToArray() };
            }

            switch (type.FullName)
            {
                case "System.Int16":
                case "System.UInt16":
                case "System.Int32":
                case "System.UInt32":
                    return new Schema { type = "integer", format = "int32" };
                case "System.Int64":
                case "System.UInt64":
                    return new Schema { type = "integer", format = "int64" };
                case "System.Single":
                    return new Schema { type = "number", format = "float" };
                case "System.Double":
                case "System.Decimal":
                    return new Schema { type = "number", format = "double" };
                case "System.Byte":
                case "System.SByte":
                    return new Schema { type = "string", format = "byte" };
                case "System.Boolean":
                    return new Schema { type = "boolean" };
                case "System.DateTime":
                case "System.DateTimeOffset":
                    return new Schema { type = "string", format = "date-time" };
                default:
                    return new Schema { type = "string" };
            }
        }
Exemplo n.º 26
0
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
                return CreateEnumSchema(primitiveContract, type);

            switch (type.FullName)
            {
                case "System.Int16":
                case "System.UInt16":
                case "System.Int32":
                case "System.UInt32":
                    return new Schema { type = "integer", format = "int32" };
                case "System.Int64":
                case "System.UInt64":
                    return new Schema { type = "integer", format = "int64" };
                case "System.Single":
                    return new Schema { type = "number", format = "float" };
                case "System.Double":
                case "System.Decimal":
                    return new Schema { type = "number", format = "double" };
                case "System.Byte":
                case "System.SByte":
                    return new Schema { type = "string", format = "byte" };
                case "System.Boolean":
                    return new Schema { type = "boolean" };
                case "System.DateTime":
                case "System.DateTimeOffset":
                    return new Schema { type = "string", format = "date-time" };
                default:
                    return new Schema { type = "string" };
            }
        }
        private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract)
        {
            Schema schema;

            if (primitiveContract.PropertyInfo() != null && primitiveContract.PropertyInfo().CustomAttributes != null)
            {
                //var isIgnored = primitiveContract.PropertyInfo().CustomAttributes.FirstOrDefault(x => x.AttributeType == typeof(SwaggerIgnore));
                var isIgnored = primitiveContract.PropertyInfo().GetCustomAttribute<SwaggerIgnore>();
                if (isIgnored != null)
                {
                    return null;
                }

                //var customRouteName = primitiveContract.PropertyInfo().GetCustomAttribute<SwaggerRouteName>();
                //if (customRouteName != null)
                //{
                //    //
                //}
            }

            var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType;

            if (type.IsEnum)
                return CreateEnumSchema(primitiveContract, type);

            switch (type.FullName)
            {
                case "System.Int16":
                case "System.UInt16":
                case "System.Int32":
                case "System.UInt32":
                    schema = new Schema { type = "integer", format = "int32" };
                    break;

                case "System.Int64":
                case "System.UInt64":
                    schema = new Schema { type = "integer", format = "int64" };
                    break;

                case "System.Single":
                    schema = new Schema { type = "number", format = "float" };
                    break;

                case "System.Double":
                case "System.Decimal":
                    schema = new Schema { type = "number", format = "double" };
                    break;

                case "System.Byte":
                case "System.SByte":
                    schema = new Schema { type = "string", format = "byte" };
                    break;

                case "System.Boolean":
                    schema = new Schema { type = "boolean" };
                    break;

                case "System.DateTime":
                case "System.DateTimeOffset":
                    schema = new Schema { type = "string", format = "date-time" };
                    break;

                default:
                    schema = new Schema { type = "string" };
                    break;
            }

            schema.WithValidationProperties(primitiveContract);

            return schema;
        }