Last active
January 17, 2017 21:25
-
-
Save JustinMorgan/fbf851c7068970a689036e646206e212 to your computer and use it in GitHub Desktop.
Dump complex object to JSON (quick & dirty approximation of JavaScript's console.log)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using Newtonsoft.Json; | |
using System.Text; | |
public static class JsonDumperSimple | |
{ | |
public static string Stringify(object obj) | |
{ | |
return JsonConvert.SerializeObject(obj, Formatting.Indented); | |
} | |
} | |
public static class JsonDumper | |
{ | |
[Flags] | |
public enum FormatOptions | |
{ | |
BreakOnNesting = 0, | |
BreakOnBranchNode = 1, | |
BreakOnLeafNode = 2 | |
} | |
public const FormatOptions Flattened = FormatOptions.BreakOnNesting | FormatOptions.BreakOnBranchNode; | |
public const FormatOptions KBR = FormatOptions.BreakOnLeafNode; | |
public const FormatOptions Expanded = FormatOptions.BreakOnNesting | FormatOptions.BreakOnBranchNode | FormatOptions.BreakOnLeafNode; | |
public static void Print(object obj, FormatOptions format = Expanded) | |
{ | |
Console.WriteLine(Stringify(obj, format)); | |
} | |
public static string Stringify(object obj, FormatOptions format = Expanded) | |
{ | |
var str = JsonConvert.SerializeObject(obj); | |
return Format(str, format); | |
} | |
public static string Format(string str, FormatOptions format) | |
{ | |
var indent = 0; | |
var quoted = false; | |
var sb = new StringBuilder(); | |
for (var i = 0; i < str.Length; i++) | |
{ | |
var ch = str[i]; | |
switch (ch) | |
{ | |
case '{': | |
case '[': | |
sb.Append(ch); | |
if (!quoted && i + 1 < str.Length) | |
{ | |
++indent; | |
switch (str[i + 1]) | |
{ | |
case '{': | |
case '[': | |
if ((format & FormatOptions.BreakOnNesting) == FormatOptions.BreakOnNesting) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
break; | |
default: | |
var _ch = ch == '{' ? '}' : ']'; | |
if ((format & FormatOptions.BreakOnLeafNode) == FormatOptions.BreakOnLeafNode || !Regex.IsMatch(str.Substring(i + 1), @"^[^[{:]*\" + _ch)) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
break; | |
} | |
} | |
break; | |
case '}': | |
case ']': | |
if (!quoted) | |
{ | |
--indent; | |
if (i > 0) | |
{ | |
switch (str[i - 1]) | |
{ | |
case ']': | |
case '}': | |
if ((format & FormatOptions.BreakOnNesting) == FormatOptions.BreakOnNesting) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
break; | |
default: | |
if ((format & FormatOptions.BreakOnLeafNode) == FormatOptions.BreakOnLeafNode) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
break; | |
} | |
} | |
} | |
sb.Append(ch); | |
break; | |
case '"': | |
sb.Append(ch); | |
bool escaped = false; | |
var index = i; | |
while (index > 0 && str[--index] == '\\') | |
escaped = !escaped; | |
if (!escaped) | |
quoted = !quoted; | |
break; | |
case ',': | |
sb.Append(ch); | |
if (!quoted && i + 1 < str.Length) | |
{ | |
switch (str[i + 1]) | |
{ | |
case '[': | |
case '{': | |
if ((format & FormatOptions.BreakOnBranchNode) == FormatOptions.BreakOnBranchNode) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
else | |
{ | |
sb.Append(" "); | |
} | |
break; | |
default: | |
if ((format & FormatOptions.BreakOnLeafNode) == FormatOptions.BreakOnLeafNode || !Regex.IsMatch(str.Substring(i), @"^[^[{:]*[}\]]")) | |
{ | |
sb.AppendLine(); | |
sb.Append(GetIndent(indent)); | |
} | |
break; | |
} | |
} | |
break; | |
case ':': | |
sb.Append(ch); | |
if (!quoted) | |
sb.Append(" "); | |
break; | |
default: | |
sb.Append(ch); | |
break; | |
} | |
} | |
return sb.ToString(); | |
} | |
private static string GetIndent(int indent, int increment = 2) | |
{ | |
var totalIndent = indent * increment; | |
return totalIndent > 0 | |
? string.Join("", Enumerable.Range(0, totalIndent).Select(i => " ")) | |
: ""; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text.RegularExpressions; | |
using Newtonsoft.Json; | |
using System.Text; | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var z = new List<List<string>>() | |
{ | |
new List<string> {"a","b","c"}, | |
new List<string> {"d","e"}, | |
new List<string> {"g","h","i"}, | |
}; | |
var t = Test.Create("Property 1", z.GetAllPhrases()); | |
JsonDumper.Print(t, JsonDumper.Flattened); | |
} | |
} | |
public static class Test | |
{ | |
public class _Test<T> | |
{ | |
public string Prop1 {get;set;} | |
public T Prop2 {get;set;} | |
} | |
public static _Test<T> Create<T>(string prop1, T prop2) | |
{ | |
return new _Test<T>() | |
{ | |
Prop1 = prop1, | |
Prop2 = prop2 | |
}; | |
} | |
} | |
public static class EnumerableExtensions | |
{ | |
public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) | |
{ | |
IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; | |
//For each sequence, add each item in that sequence to the product we have so far. | |
return sequences.Aggregate(emptyProduct, | |
(accumulator, sequence) => | |
from seq in accumulator | |
from item in sequence | |
select seq.Concat(new[] { item })); | |
} | |
public static IEnumerable<List<string>> GetAllPhrases(this IEnumerable<List<string>> sequences, int minLength = 2, int maxLength = 5) | |
{ | |
var sequenceList = sequences.ToList(); | |
for (var i = minLength; i <= sequenceList.Count && i <= maxLength; i++) | |
{ | |
var cart = sequenceList.Take(i).CartesianProduct().ToList(); | |
foreach (var sequence in cart) | |
{ | |
yield return sequence.ToList(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment