Advertisement
commodore73

Convert Entry JSON to Entry Model

Jul 22nd, 2020
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1. namespace csclt
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     using Newtonsoft.Json;
  7.  
  8.     using Contentstack.Core;
  9.     using Contentstack.Core.Configuration;
  10.     using Contentstack.Core.Models;
  11.  
  12.     public class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             ContentstackClient stack = new ContentstackClient(new ContentstackOptions()
  17.             {
  18.                 ApiKey = "blt94519f01d8f92c86",
  19.                 AccessToken = "cs99af6674ef2a7a53770da6b7",
  20.                 Environment = "contentdelivery",
  21.             });
  22.             Entry entry = stack.ContentType("fieldtypes").Entry(
  23.                 "blt63b6985324826b78").Fetch<Entry>().GetAwaiter().GetResult();
  24.             Console.WriteLine("Title Property: " + entry.Title);
  25.             Console.WriteLine("Title Field from JSON: " + entry.Object["title"]);
  26.             JsonSerializer serializer = JsonSerializer.Create(stack.SerializerSettings);
  27.             FieldTypesEntry entryModel = entry.As<FieldTypesEntry>(serializer);
  28.             entryModel.Render();
  29.         }
  30.     }
  31.  
  32.     public static class EntryExtensions
  33.     {
  34.         public static TEntryModel As<TEntryModel>(this Entry entry, JsonSerializer serializer)
  35.         {
  36.             return entry.ToJson().ToObject<TEntryModel>(serializer);
  37.         }
  38.     }
  39.    
  40.     public class AssetMetadata
  41.     {
  42.         public string Url { get; set; }
  43.     }
  44.        
  45.     public class FieldTypesEntry
  46.     {
  47.         public List<string> StringField { get; set; }
  48.  
  49.         [JsonProperty(propertyName: "datefield")]
  50.         public List<DateTime> DateTimeField { get; set; }
  51.            
  52.         public List<string> RichTextField { get; set; }
  53.            
  54.         [JsonProperty(propertyName: "asssetfield")]
  55.         public List<AssetMetadata> AssetField { get; set; }
  56.            
  57.         [JsonProperty(propertyName: "numberfiel")]
  58.         public List<double?> NumberField { get; set; }
  59.  
  60.         public void Render()
  61.         {
  62.             Console.WriteLine(GetType());
  63.  
  64.             foreach (double? number in NumberField)
  65.             {
  66.                 Console.WriteLine("NumberField: " + number);
  67.             }
  68.                
  69.             foreach (string value in StringField)
  70.             {
  71.                 Console.WriteLine("StringField: " + value);
  72.             }
  73.  
  74.             foreach (DateTime dt in DateTimeField)
  75.             {
  76.                 Console.WriteLine("DateTimeField: " + dt);
  77.             }
  78.  
  79.             foreach (string richText in RichTextField)
  80.             {
  81.                 Console.WriteLine("RichTextField: " + richText);
  82.             }
  83.  
  84.             foreach (AssetMetadata asset in AssetField)
  85.             {
  86.                 Console.WriteLine("AssetField: " + asset.Url);
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement