Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TobMia.DedicatedServerRCON
- {
- public class JsonFormater
- {
- public string setOutput = null;
- private string fields = null;
- private string Field;
- private string Value;
- private bool flat = false;
- public JsonFormater(string Field, string Value)
- {
- this.Field = Field;
- this.Value = Value;
- this.fields = this.FieldFormat();
- }
- public JsonFormater(ref List<JsonFormater> JsonFormaterList, bool flat = false)
- {
- this.flat = flat;
- if (JsonFormaterList != null)
- {
- string[] buildString = new string[JsonFormaterList.Count];
- int i = 0;
- if (JsonFormaterList.Count > 0)
- {
- foreach (var item in JsonFormaterList)
- {
- buildString[i] = "" + item.JsonOutput().Replace("'", "\\'") + "";
- i++;
- }
- this.Value = string.Join(",", buildString);
- }
- this.fields = this.FieldFormat();
- }
- if (flat == false)
- {
- this.fields = this.ArrayFormat();
- }
- else
- {
- this.fields = this.FieldFormat();
- }
- }
- public JsonFormater(string Field, ref List<JsonFormater> JsonFormaterList, bool flat = false)
- {
- if (JsonFormaterList != null)
- {
- this.Field = Field;
- this.flat = flat;
- string[] buildString = new string[JsonFormaterList.Count];
- int i = 0;
- if (JsonFormaterList.Count > 0)
- {
- foreach (var item in JsonFormaterList)
- {
- Char delimiter = ',';
- String[] substrings = item.JsonOutput().Split(delimiter);
- if (flat == false)
- {
- // HMM TODO?
- /*if (substrings.Length == 1 && JsonFormaterList.Count == 1)
- {
- buildString[i] = "" + item.JsonOutput() + "";
- }
- else if (substrings.Length > 1 && JsonFormaterList.Count == 1)
- {
- buildString[i] = "{" + item.JsonOutput() + "}";
- }
- else if (substrings.Length == 1)
- {
- buildString[i] = "{" + item.JsonOutput() + "}";
- }
- else
- {
- }*/
- buildString[i] = "{" + item.JsonOutput().Replace("'", "\\'") + "}";
- }
- else if (flat == true)
- {
- buildString[i] = "" + item.JsonOutput().Replace("'", "\\'") + "";
- }
- //System.Console.WriteLine("substrings.Length = " + substrings.Length + " | JsonFormaterList.Count = " + JsonFormaterList.Count + " | this.Field = " + this.Field + " | this.flat = " + this.flat + " | " + buildString[i]);
- i++;
- }
- if (flat == true)
- {
- this.Value = "{" + string.Join(",", buildString) + "}";
- }
- else
- {
- this.Value = string.Join(",", buildString);
- }
- }
- }
- if (flat == false)
- {
- this.fields = this.ArrayFormat();
- }
- else
- {
- this.fields = this.FieldFormat();
- }
- }
- public string JsonOutput()
- {
- return this.fields;
- }
- private string SetOutput
- {
- get { return fields; }
- set { fields = value; }
- }
- private string FieldFormat()
- {
- //Console.WriteLine("FieldFormat() this.flat = " + this.flat.ToString() + " this.Field = " + this.Field);
- //this.Value = this.Value.Replace("'", "\'");
- if (this.Field == null)
- {
- if (this.flat == false)
- {
- return "\"" + this.Value + "\"";
- }
- else
- {
- return "" + this.Value + "";
- }
- }
- else
- {
- if (this.flat == false)
- {
- return "\"" + this.Field + "\": \"" + this.Value + "\"";
- }
- else
- {
- return "\"" + this.Field + "\": " + this.Value + "";
- }
- }
- }
- private string ArrayFormat()
- {
- return "\"" + this.Field + "\": [" + this.Value + "]";
- }
- }
- public class StringOutput
- {
- private string jString = null;
- //public List<JsonFormater> List;
- public StringOutput()
- {
- this.jString = null;
- }
- public StringOutput(ref List<JsonFormater> JsonFormaterList)
- {
- if (JsonFormaterList != null)
- {
- string[] buildString = new string[JsonFormaterList.Count];
- int i = 0;
- if (JsonFormaterList.Count > 0)
- {
- foreach (var item in JsonFormaterList)
- {
- //Console.WriteLine(item.JsonOutput());
- buildString[i] = "" + item.JsonOutput();
- i++;
- }
- this.jString = string.Join(",", buildString);
- }
- }
- }
- public string jsonString()
- {
- return "{" + this.jString + "}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement