PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/SourceCode_3rdParty_Dlls/JayRock [JSON lib]/Jayrock.Json/Json/Conversion/Converters/DateTimeImporter.cs

https://bitbucket.org/VahidN/ntextcat
C# | 100 lines | 68 code | 13 blank | 19 comment | 5 complexity | 8d19eaa537eca58643894b7e0f754d5f MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause
  1. #region License, Terms and Conditions
  2. //
  3. // Jayrock - JSON and JSON-RPC for Microsoft .NET Framework and Mono
  4. // Written by Atif Aziz (www.raboof.com)
  5. // Copyright (c) 2005 Atif Aziz. All rights reserved.
  6. //
  7. // This library is free software; you can redistribute it and/or modify it under
  8. // the terms of the GNU Lesser General Public License as published by the Free
  9. // Software Foundation; either version 2.1 of the License, or (at your option)
  10. // any later version.
  11. //
  12. // This library is distributed in the hope that it will be useful, but WITHOUT
  13. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  15. // details.
  16. //
  17. // You should have received a copy of the GNU Lesser General Public License
  18. // along with this library; if not, write to the Free Software Foundation, Inc.,
  19. // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. //
  21. #endregion
  22. namespace Jayrock.Json.Conversion.Converters
  23. {
  24. #region Imports
  25. using System;
  26. using System.Diagnostics;
  27. using System.Globalization;
  28. using System.Xml;
  29. #endregion
  30. public sealed class DateTimeImporter : ImporterBase
  31. {
  32. public DateTimeImporter() :
  33. base(typeof(DateTime)) {}
  34. protected override object ImportFromString(ImportContext context, JsonReader reader)
  35. {
  36. Debug.Assert(context != null);
  37. Debug.Assert(reader != null);
  38. try
  39. {
  40. return ReadReturning(reader, XmlConvert.ToDateTime(reader.Text
  41. #if !NET_1_0 && !NET_1_1
  42. , XmlDateTimeSerializationMode.Local
  43. #endif
  44. ));
  45. }
  46. catch (FormatException e)
  47. {
  48. throw new JsonException("Error importing JSON String as System.DateTime.", e);
  49. }
  50. }
  51. protected override object ImportFromNumber(ImportContext context, JsonReader reader)
  52. {
  53. Debug.Assert(context != null);
  54. Debug.Assert(reader != null);
  55. string text = reader.Text;
  56. double time;
  57. try
  58. {
  59. time = Convert.ToDouble(text, CultureInfo.InvariantCulture);
  60. }
  61. catch (FormatException e)
  62. {
  63. throw NumberError(e, text);
  64. }
  65. catch (OverflowException e)
  66. {
  67. throw NumberError(e, text);
  68. }
  69. try
  70. {
  71. return ReadReturning(reader, UnixTime.ToDateTime(time));
  72. }
  73. catch (ArgumentException e)
  74. {
  75. throw NumberError(e, text);
  76. }
  77. }
  78. private static JsonException NumberError(Exception e, string text)
  79. {
  80. return new JsonException(string.Format("Error importing JSON Number {0} as System.DateTime.", text), e);
  81. }
  82. protected override JsonException GetImportException(string jsonValueType)
  83. {
  84. return new JsonException(string.Format("Found {0} where expecting a JSON String in ISO 8601 time format or a JSON Number expressed in Unix time.", jsonValueType));
  85. }
  86. }
  87. }