예제 #1
0
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The reader object to be read.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object that represents the serialized JSON.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.AssertNotNull(nameof(reader));
            objectType.AssertNotNull(nameof(objectType));

            if (!objectType.IsEnum)
            {
                throw new JsonSerializationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "EnumJsonConverter cannot deserialize '{0}' values",
                              objectType.Name));
            }

            if (reader.TokenType == JsonToken.String)
            {
                return(Enum.Parse(objectType, JScriptToPascalCase(reader.Value.ToString())));
            }
            else if (reader.TokenType == JsonToken.Integer)
            {
                return(Enum.ToObject(objectType, reader.Value));
            }
            else
            {
                throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "EnumJsonConverter cannot deserialize '{0}' values", reader.TokenType));
            }
        }
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader" /> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        /// <exception cref="ArgumentNullException">
        /// reader
        /// or
        /// objectType
        /// </exception>
        /// <exception cref="JsonSerializationException">
        /// </exception>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.AssertNotNull(nameof(reader));
            objectType.AssertNotNull(nameof(objectType));

            if (!CanConvert(objectType))
            {
                throw new JsonSerializationException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "ResourceCollectionConverter cannot deserialize '{0}' values",
                              objectType.Name));
            }

            JObject jobject = JObject.Load(reader);

            object obj = (objectType != typeof(SeekBasedResourceCollection <T>)) ?
                         new ResourceCollection <T>(new List <T>()) :
                         new SeekBasedResourceCollection <T>(new List <T>(), (jobject["continuationToken"] ?? string.Empty).ToString());

            serializer.Populate(jobject.CreateReader(), obj);

            return(obj);
        }
예제 #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="reader"></param>
 /// <param name="objectType"></param>
 /// <param name="existingValue"></param>
 /// <param name="serializer"></param>
 /// <returns></returns>
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     reader.AssertNotNull("reader");
     return(DateTime.SpecifyKind(DateTime.Parse(reader.Value.ToString()), DateTimeKind.Utc).ToLocalTime());
 }