Advertisement
LaughingMan

Untitled

Dec 18th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using Sandbox.Extensions;
  8.  
  9. namespace Sandbox.Helpers
  10. {
  11.     public static class JsonHelper
  12.     {
  13.         public static Dictionary<string, Dictionary<string, object>> GlobalSchemas { get; set; }
  14.  
  15.         static JsonHelper()
  16.         {
  17.             GlobalSchemas = new Dictionary<string, Dictionary<string, object>>();
  18.         }
  19.  
  20.         public static object ProcessJsonSchema(string response)
  21.         {
  22.             if (response.IsNullOrWhiteSpace())
  23.             {
  24.                 return null;
  25.             }
  26.  
  27.             response = response.Replace("\r", "").Replace("\n", "").Trim();
  28.  
  29.             if (response.StartsWith("//"))
  30.             {
  31.                 response = response.Trim('/', '[');
  32.             }
  33.  
  34.             // Convert hex values (eg. "\x2F") to their string character equivalent (eg. "/")
  35.             for (int i = 0; i < 128; i++)
  36.             {
  37.                 string hexString = i.ToString("X").PadLeft(2, '0');
  38.                 string searchString = @"\x" + hexString;
  39.                 int charValue = Convert.ToInt32(hexString, 16);
  40.                 string character = Char.ConvertFromUtf32(charValue);
  41.  
  42.                 response = response.Replace(searchString, character == "\""
  43.                                                             ? "'"
  44.                                                             : character);
  45.             }
  46.  
  47.             JToken token = JToken.Parse(response);
  48.  
  49.             object schema = ProcessJToken(token);
  50.  
  51.             AddToGlobalSchema(schema, "StockData");
  52.             string output = OutputGlobalSchema();
  53.             Console.Clear();
  54.             Console.Write(output);
  55.             return schema;
  56.         }
  57.  
  58.         private static Dictionary<string, object> ProcessJObject(JObject jObject)
  59.         {
  60.             return ProcessJObject(jObject.ToString());
  61.         }
  62.  
  63.         private static Dictionary<string, object> ProcessJObject(string input)
  64.         {
  65.             //IDictionary<string, JToken> properties = JSchema.Parse(input).ExtensionData;
  66.             IDictionary<string, JToken> properties = JsonConvert.DeserializeObject<Dictionary<string, JToken>>(input);
  67.             Dictionary<string, object> fields = new Dictionary<string, object>();
  68.  
  69.             foreach (var property in properties)
  70.             {
  71.                 object schema = ProcessJToken(property.Value);
  72.                 fields.Add(property.Key, schema);
  73.             }
  74.  
  75.             return fields;
  76.         }
  77.  
  78.         private static object ProcessJToken(JToken token)
  79.         {
  80.             if (token == null)
  81.                 return null;
  82.  
  83.             switch (token.Type)
  84.             {
  85.                 case JTokenType.Object:
  86.                     JObject jObject = (JObject)token;
  87.                     Dictionary<string, object> objectResult = ProcessJObject(jObject);
  88.                     return objectResult;
  89.                 case JTokenType.Array:
  90.                     JArray array = (JArray)token;
  91.                     IEnumerable<object> arrayResult = array.Select(ProcessJToken);
  92.                     return arrayResult;
  93.                 case JTokenType.Property:
  94.                     JProperty property = (JProperty)token;
  95.                     object propertyResult = ProcessJToken(property.Value);
  96.                     return propertyResult;
  97.                 case JTokenType.Raw:
  98.                 case JTokenType.Uri:
  99.                 case JTokenType.Comment:
  100.                 case JTokenType.Constructor:
  101.                 case JTokenType.None:
  102.                 case JTokenType.Undefined:
  103.                     return JTokenType.String;
  104.                 case JTokenType.Null:
  105.                     return null;
  106.                 case JTokenType.Integer:
  107.                 case JTokenType.Float:
  108.                 case JTokenType.String:
  109.                 case JTokenType.Boolean:
  110.                 case JTokenType.Date:
  111.                 case JTokenType.Bytes:
  112.                 case JTokenType.Guid:
  113.                 case JTokenType.TimeSpan:
  114.                     return token.Type;
  115.                 default:
  116.                     throw new ArgumentOutOfRangeException();
  117.             }
  118.         }
  119.  
  120.         private static void AddToGlobalSchema(object schema, string name)
  121.         {
  122.             if (schema == null)
  123.                 return;
  124.  
  125.             name = name.IfNullOrWhiteSpace("JsonObject");
  126.  
  127.             if (!GlobalSchemas.ContainsKey(name))
  128.             {
  129.                 GlobalSchemas.Add(name, new Dictionary<string, object>());
  130.             }
  131.  
  132.             Dictionary<string, object> globalSchema = GlobalSchemas[name] ?? new Dictionary<string, object>();
  133.  
  134.             bool isArray = schema is IEnumerable<object>;
  135.             bool isModel = schema is Dictionary<string, object>;
  136.  
  137.             if (isArray)
  138.             {
  139.                 // Array
  140.                 IEnumerable<object> objects = schema as IEnumerable<object>;
  141.  
  142.                 foreach (var obj in objects)
  143.                 {
  144.                     AddToGlobalSchema(obj, name);
  145.                 }
  146.             }
  147.             else if (isModel)
  148.             {
  149.                 // Object
  150.                 Dictionary<string, object> fields = (Dictionary<string, object>)schema;
  151.  
  152.                 foreach (var field in fields)
  153.                 {
  154.                     if (field.Key.IsNullOrWhiteSpace() || field.Value == null)
  155.                         continue;
  156.  
  157.                     bool ismodel = field.Value is Dictionary<string, object>;
  158.                     bool isarray = field.Value is IEnumerable<object>;
  159.  
  160.                     string fieldName = field.Key + (isarray ? "Array" : "");
  161.                     bool isnew = !globalSchema.ContainsKey(fieldName);
  162.  
  163.                     if (isnew)
  164.                     {
  165.                         if (ismodel)
  166.                         {
  167.                             globalSchema.Add(fieldName, fieldName);
  168.                             AddToGlobalSchema(field.Value as Dictionary<string, object>, field.Key);
  169.                         }
  170.                         else if (isarray)
  171.                         {
  172.                             globalSchema.Add(fieldName, string.Format("Collection<{0}>", field.Key));
  173.  
  174.                             object[] objects = ((IEnumerable<object>)field.Value).ToArray();
  175.  
  176.                             if (!objects.Any())
  177.                                 continue;
  178.  
  179.                             foreach (object obj in objects)
  180.                             {
  181.                                 AddToGlobalSchema(obj, field.Key);
  182.                             }
  183.                         }
  184.                         else
  185.                         {
  186.                             globalSchema.Add(field.Key, field.Value);
  187.                         }
  188.                     }
  189.                     else
  190.                     {
  191.                         if (ismodel)
  192.                         {
  193.                             if (!globalSchema[fieldName].Equals(fieldName))
  194.                             {
  195.                                 globalSchema[fieldName] = fieldName;
  196.                             }
  197.                             AddToGlobalSchema(field.Value as Dictionary<string, object>, fieldName);
  198.                         }
  199.                         else if (isarray)
  200.                         {
  201.                             if (!globalSchema[fieldName].Equals(string.Format("Collection<{0}>", field.Key)))
  202.                             {
  203.                                 globalSchema[fieldName] = string.Format("Collection<{0}>", field.Key);
  204.                             }
  205.  
  206.                             object[] objects = ((IEnumerable<object>)field.Value).ToArray();
  207.  
  208.                             foreach (object obj in objects)
  209.                             {
  210.                                 AddToGlobalSchema(obj, field.Key);
  211.                             }
  212.                         }
  213.                         else
  214.                         {
  215.                             if (!globalSchema[fieldName].Equals(field.Value))
  216.                             {
  217.                                 globalSchema[fieldName] = field.Value;
  218.                             }
  219.                         }
  220.                     }
  221.                 }
  222.  
  223.                 GlobalSchemas[name] = globalSchema;
  224.             }
  225.             else
  226.             {
  227.                 // ?
  228.                 Debug.WriteLine(schema);
  229.             }
  230.  
  231.         }
  232.  
  233.         public static string OutputGlobalSchema()
  234.         {
  235.             if (GlobalSchemas == null || !GlobalSchemas.Any())
  236.                 return null;
  237.  
  238.             string output = "";
  239.             foreach (var schema in GlobalSchemas)
  240.             {
  241.                 output += string.Format("public class {0}\r\n{{\r\n", schema.Key);
  242.  
  243.                 foreach (var field in schema.Value)
  244.                 {
  245.                     output += string.Format("\tpublic {0} {1} {{ get; set; }}\r\n", field.Value, field.Key);
  246.                 }
  247.  
  248.                 output += "}";
  249.             }
  250.  
  251.             return output;
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement