/// <summary>Deserializes the JSON to the specified .NET type using <see cref="JsonSerializer"/>.</summary> /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> used to deserialize the object.</param> /// <param name="textReader">The JSON to deserialize.</param> /// <param name="type">The type of the object to deserialize to.</param> /// <returns>The deserialized object from the JSON string.</returns> public static object DeserializeFromReader(this JsonSerializer jsonSerializer, TextReader textReader, Type type = null) { var isCheckAdditionalContentNoSet = !jsonSerializer.IsCheckAdditionalContentSetX(); try { // by default DeserializeObject should check for additional content if (isCheckAdditionalContentNoSet) { jsonSerializer.CheckAdditionalContent = true; } using (var reader = new JsonTextReader(textReader)) { reader.ArrayPool = JsonConvertX.GlobalCharacterArrayPool; reader.CloseInput = false; return(jsonSerializer.Deserialize(reader, type)); } } finally { if (isCheckAdditionalContentNoSet) { jsonSerializer.SetCheckAdditionalContent(); } } }
/// <summary>Deserializes the JSON to the specified .NET type using <see cref="JsonSerializer"/>.</summary> /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> used to deserialize the object.</param> /// <param name="value">The JSON to deserialize.</param> /// <param name="type">The type of the object to deserialize to.</param> /// <returns>The deserialized object from the JSON string.</returns> public static object DeserializeObject(this JsonSerializer jsonSerializer, string value, Type type = null) { if (null == value) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); } var isCheckAdditionalContentNoSet = !jsonSerializer.IsCheckAdditionalContentSetX(); try { // by default DeserializeObject should check for additional content if (isCheckAdditionalContentNoSet) { jsonSerializer.CheckAdditionalContent = true; } using (var reader = new JsonTextReader(new StringReader(value))) { reader.ArrayPool = JsonConvertX.GlobalCharacterArrayPool; reader.CloseInput = false; return(jsonSerializer.Deserialize(reader, type)); } } finally { if (isCheckAdditionalContentNoSet) { jsonSerializer.SetCheckAdditionalContent(); } } }
/// <summary>Deserializes the JSON to the specified .NET type using <see cref="JsonSerializer"/>.</summary> /// <param name="jsonSerializer">The <see cref="JsonSerializer"/> used to deserialize the object.</param> /// <param name="value">The JSON to deserialize.</param> /// <param name="offset"></param> /// <param name="count"></param> /// <param name="type">The type of the object to deserialize to.</param> /// <returns>The deserialized object from the JSON string.</returns> public static object DeserializeFromByteArray(this JsonSerializer jsonSerializer, byte[] value, int offset, int count, Type type = null) { var isCheckAdditionalContentNoSet = !jsonSerializer.IsCheckAdditionalContentSetX(); try { // by default DeserializeObject should check for additional content if (isCheckAdditionalContentNoSet) { jsonSerializer.CheckAdditionalContent = true; } using (var reader = new JsonTextReader(new StreamReaderX(new MemoryStream(value, offset, count), Encoding.UTF8))) { reader.ArrayPool = JsonConvertX.GlobalCharacterArrayPool; reader.CloseInput = false; return(jsonSerializer.Deserialize(reader, type)); } } finally { if (isCheckAdditionalContentNoSet) { jsonSerializer.SetCheckAdditionalContent(); } } }