Advertisement
KristianIvanov00

Untitled

Mar 14th, 2024
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.11 KB | None | 0 0
  1. namespace Bookstore.Services.External
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     using System.Globalization;
  7.     using Newtonsoft.Json;
  8.     using Newtonsoft.Json.Converters;
  9.  
  10.     public partial class OpenLibraryBookDto
  11.     {
  12.         [JsonProperty("url")]
  13.         public Uri Url { get; set; }
  14.  
  15.         [JsonProperty("key")]
  16.         public string Key { get; set; }
  17.  
  18.         [JsonProperty("title")]
  19.         public string Title { get; set; }
  20.  
  21.         [JsonProperty("subtitle")]
  22.         public string Subtitle { get; set; }
  23.  
  24.         [JsonProperty("authors")]
  25.         public List<Author> Authors { get; set; }
  26.  
  27.         [JsonProperty("number_of_pages")]
  28.         public long NumberOfPages { get; set; }
  29.  
  30.         [JsonProperty("pagination")]
  31.         public string Pagination { get; set; }
  32.  
  33.         [JsonProperty("by_statement")]
  34.         public string ByStatement { get; set; }
  35.  
  36.         [JsonProperty("identifiers")]
  37.         public Dictionary<string, List<string>> Identifiers { get; set; }
  38.  
  39.         [JsonProperty("classifications")]
  40.         public Classifications Classifications { get; set; }
  41.  
  42.         [JsonProperty("publishers")]
  43.         public List<Publish> Publishers { get; set; }
  44.  
  45.         [JsonProperty("publish_places")]
  46.         public List<Publish> PublishPlaces { get; set; }
  47.  
  48.         [JsonProperty("publish_date")]
  49.         [JsonConverter(typeof(ParseStringConverter))]
  50.         public long PublishDate { get; set; }
  51.  
  52.         [JsonProperty("subjects")]
  53.         public List<Author> Subjects { get; set; }
  54.  
  55.         [JsonProperty("notes")]
  56.         public string Notes { get; set; }
  57.  
  58.         [JsonProperty("ebooks")]
  59.         public List<Ebook> Ebooks { get; set; }
  60.  
  61.         [JsonProperty("cover")]
  62.         public Cover Cover { get; set; }
  63.     }
  64.  
  65.     public partial class Author
  66.     {
  67.         [JsonProperty("url")]
  68.         public Uri Url { get; set; }
  69.  
  70.         [JsonProperty("name")]
  71.         public string Name { get; set; }
  72.     }
  73.  
  74.     public partial class Classifications
  75.     {
  76.         [JsonProperty("lc_classifications")]
  77.         public List<string> LcClassifications { get; set; }
  78.  
  79.         [JsonProperty("dewey_decimal_class")]
  80.         public List<string> DeweyDecimalClass { get; set; }
  81.     }
  82.  
  83.     public partial class Cover
  84.     {
  85.         [JsonProperty("small")]
  86.         public Uri Small { get; set; }
  87.  
  88.         [JsonProperty("medium")]
  89.         public Uri Medium { get; set; }
  90.  
  91.         [JsonProperty("large")]
  92.         public Uri Large { get; set; }
  93.     }
  94.  
  95.     public partial class Ebook
  96.     {
  97.         [JsonProperty("preview_url")]
  98.         public Uri PreviewUrl { get; set; }
  99.  
  100.         [JsonProperty("availability")]
  101.         public string Availability { get; set; }
  102.  
  103.         [JsonProperty("formats")]
  104.         public Formats Formats { get; set; }
  105.  
  106.         [JsonProperty("borrow_url")]
  107.         public Uri BorrowUrl { get; set; }
  108.  
  109.         [JsonProperty("checkedout")]
  110.         public bool Checkedout { get; set; }
  111.     }
  112.  
  113.     public partial class Formats
  114.     {
  115.     }
  116.  
  117.     public partial class Publish
  118.     {
  119.         [JsonProperty("name")]
  120.         public string Name { get; set; }
  121.     }
  122.  
  123.     public partial class OpenLibraryBookDto
  124.     {
  125.         public static OpenLibraryBookDto FromJson(string json) => JsonConvert.DeserializeObject<OpenLibraryBookDto>(json, Bookstore.Services.External.Converter.Settings);
  126.     }
  127.  
  128.     public static class Serialize
  129.     {
  130.         public static string ToJson(this OpenLibraryBookDto self) => JsonConvert.SerializeObject(self, Bookstore.Services.External.Converter.Settings);
  131.     }
  132.  
  133.     internal static class Converter
  134.     {
  135.         public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
  136.         {
  137.             MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
  138.             DateParseHandling = DateParseHandling.None,
  139.             Converters =
  140.             {
  141.                 new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
  142.             },
  143.         };
  144.     }
  145.  
  146.     internal class ParseStringConverter : JsonConverter
  147.     {
  148.         public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
  149.  
  150.         public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
  151.         {
  152.             if (reader.TokenType == JsonToken.Null) return null;
  153.             var value = serializer.Deserialize<string>(reader);
  154.             long l;
  155.             if (Int64.TryParse(value, out l))
  156.             {
  157.                 return l;
  158.             }
  159.             throw new Exception("Cannot unmarshal type long");
  160.         }
  161.  
  162.         public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
  163.         {
  164.             if (untypedValue == null)
  165.             {
  166.                 serializer.Serialize(writer, null);
  167.                 return;
  168.             }
  169.             var value = (long)untypedValue;
  170.             serializer.Serialize(writer, value.ToString());
  171.             return;
  172.         }
  173.  
  174.         public static readonly ParseStringConverter Singleton = new ParseStringConverter();
  175.     }
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement