Advertisement
lonksys

JsonFormater

Jan 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TobMia.DedicatedServerRCON
  8. {
  9.     public class JsonFormater
  10.     {
  11.         public string setOutput = null;
  12.         private string fields = null;
  13.  
  14.         private string Field;
  15.         private string Value;
  16.         private bool flat = false;
  17.  
  18.         public JsonFormater(string Field, string Value)
  19.         {
  20.             this.Field = Field;
  21.             this.Value = Value;
  22.  
  23.             this.fields = this.FieldFormat();
  24.         }
  25.  
  26.         public JsonFormater(ref List<JsonFormater> JsonFormaterList, bool flat = false)
  27.         {
  28.             this.flat = flat;
  29.             if (JsonFormaterList != null)
  30.             {
  31.                 string[] buildString = new string[JsonFormaterList.Count];
  32.                 int i = 0;
  33.                 if (JsonFormaterList.Count > 0)
  34.                 {
  35.                     foreach (var item in JsonFormaterList)
  36.                     {
  37.  
  38.                         buildString[i] = "" + item.JsonOutput().Replace("'", "\\'") + "";
  39.                         i++;
  40.                     }
  41.                     this.Value = string.Join(",", buildString);
  42.                 }
  43.                 this.fields = this.FieldFormat();
  44.             }
  45.             if (flat == false)
  46.             {
  47.                 this.fields = this.ArrayFormat();
  48.             }
  49.             else
  50.             {
  51.                 this.fields = this.FieldFormat();
  52.             }
  53.         }
  54.  
  55.         public JsonFormater(string Field, ref List<JsonFormater> JsonFormaterList, bool flat = false)
  56.         {
  57.             if (JsonFormaterList != null)
  58.             {
  59.                 this.Field = Field;
  60.                 this.flat = flat;
  61.                 string[] buildString = new string[JsonFormaterList.Count];
  62.                 int i = 0;
  63.                 if (JsonFormaterList.Count > 0)
  64.                 {
  65.                     foreach (var item in JsonFormaterList)
  66.                     {
  67.                         Char delimiter = ',';
  68.                         String[] substrings = item.JsonOutput().Split(delimiter);
  69.                         if (flat == false)
  70.                         {
  71.                             // HMM TODO?
  72.                             /*if (substrings.Length == 1 && JsonFormaterList.Count == 1)
  73.                             {
  74.                                 buildString[i] = "" + item.JsonOutput() + "";
  75.                             }
  76.                             else if (substrings.Length > 1 && JsonFormaterList.Count == 1)
  77.                             {
  78.                                 buildString[i] = "{" + item.JsonOutput() + "}";
  79.                             }
  80.                             else if (substrings.Length == 1)
  81.                             {
  82.                                 buildString[i] = "{" + item.JsonOutput() + "}";
  83.                             }
  84.                             else
  85.                             {
  86.                                
  87.                             }*/
  88.  
  89.                             buildString[i] = "{" + item.JsonOutput().Replace("'", "\\'") + "}";
  90.                         }
  91.                         else if (flat == true)
  92.                         {
  93.                             buildString[i] = "" + item.JsonOutput().Replace("'", "\\'") + "";
  94.                         }
  95.                         //System.Console.WriteLine("substrings.Length = " + substrings.Length + " | JsonFormaterList.Count = " + JsonFormaterList.Count + " | this.Field = " + this.Field + " | this.flat = " + this.flat + " | " + buildString[i]);
  96.                         i++;
  97.                     }
  98.                     if (flat == true)
  99.                     {
  100.                         this.Value = "{" + string.Join(",", buildString) + "}";
  101.                     }
  102.                     else
  103.                     {
  104.                         this.Value = string.Join(",", buildString);
  105.                     }
  106.                 }
  107.  
  108.             }
  109.  
  110.             if (flat == false)
  111.             {
  112.                 this.fields = this.ArrayFormat();
  113.             }
  114.             else
  115.             {
  116.                 this.fields = this.FieldFormat();
  117.             }
  118.         }
  119.  
  120.         public string JsonOutput()
  121.         {
  122.             return this.fields;
  123.         }
  124.  
  125.         private string SetOutput
  126.         {
  127.             get { return fields; }
  128.             set { fields = value; }
  129.         }
  130.  
  131.         private string FieldFormat()
  132.         {
  133.             //Console.WriteLine("FieldFormat() this.flat = " + this.flat.ToString() + " this.Field =  " + this.Field);
  134.             //this.Value =  this.Value.Replace("'", "\'");
  135.             if (this.Field == null)
  136.             {
  137.                 if (this.flat == false)
  138.                 {
  139.                     return "\"" + this.Value + "\"";
  140.                 }
  141.                 else
  142.                 {
  143.                     return "" + this.Value + "";
  144.                 }
  145.             }
  146.             else
  147.             {
  148.                 if (this.flat == false)
  149.                 {
  150.                     return "\"" + this.Field + "\": \"" + this.Value + "\"";
  151.                 }
  152.  
  153.                 else
  154.                 {
  155.  
  156.                     return "\"" + this.Field + "\": " + this.Value + "";
  157.                 }
  158.             }
  159.         }
  160.  
  161.         private string ArrayFormat()
  162.         {
  163.             return "\"" + this.Field + "\": [" + this.Value + "]";
  164.         }
  165.     }
  166.  
  167.     public class StringOutput
  168.     {
  169.         private string jString = null;
  170.         //public List<JsonFormater> List;
  171.  
  172.         public StringOutput()
  173.         {
  174.             this.jString = null;
  175.         }
  176.  
  177.         public StringOutput(ref List<JsonFormater> JsonFormaterList)
  178.         {
  179.             if (JsonFormaterList != null)
  180.             {
  181.                 string[] buildString = new string[JsonFormaterList.Count];
  182.                 int i = 0;
  183.                 if (JsonFormaterList.Count > 0)
  184.                 {
  185.                     foreach (var item in JsonFormaterList)
  186.                     {
  187.                         //Console.WriteLine(item.JsonOutput());
  188.                         buildString[i] = "" + item.JsonOutput();
  189.                         i++;
  190.                     }
  191.                     this.jString = string.Join(",", buildString);
  192.                 }
  193.             }
  194.         }
  195.  
  196.         public string jsonString()
  197.         {
  198.             return "{" + this.jString + "}";
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement