Created
September 24, 2013 18:21
-
-
Save danesparza/6689047 to your computer and use it in GitHub Desktop.
Pretty printing JSON data to a file
This file contains hidden or 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.IO; | |
| using System.Linq; | |
| using System.Runtime.Serialization; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using ServiceStack.Text; | |
| namespace PrettyJsonTest | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Load up some test data: | |
| TestData data1 = new TestData() | |
| { | |
| Name = "Test", | |
| Description = "This is a nice long description", | |
| Score = 42 | |
| }; | |
| TestData data2 = new TestData() | |
| { | |
| Name = "Test", | |
| Description = "This is a nice long description", | |
| Score = 42 | |
| }; | |
| TestData data3 = new TestData() | |
| { | |
| Name = "Test", | |
| Description = "This is a nice long description", | |
| Score = 42 | |
| }; | |
| List<TestData> listOfStuff = new List<TestData>(); | |
| listOfStuff.Add(data1); | |
| listOfStuff.Add(data2); | |
| listOfStuff.Add(data3); | |
| // Write it out to a file: | |
| string jsonString = listOfStuff.ToJson(); | |
| string formattedJSON = PrettyJson.FormatJson(jsonString); | |
| File.WriteAllText(@"c:\temp\test.json", formattedJSON); | |
| } | |
| } | |
| [DataContract] | |
| class TestData | |
| { | |
| [DataMember] | |
| public string Name { get; set; } | |
| [DataMember] | |
| public string Description { get; set; } | |
| [DataMember] | |
| public int Score { get; set; } | |
| } | |
| public class PrettyJson | |
| { | |
| private const string INDENT_STRING = " "; | |
| public static string FormatJson(string str) | |
| { | |
| 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) | |
| { | |
| sb.AppendLine(); | |
| Enumerable.Range(0, ++indent).ForEach(item => sb.Append(INDENT_STRING)); | |
| } | |
| break; | |
| case '}': | |
| case ']': | |
| if(!quoted) | |
| { | |
| sb.AppendLine(); | |
| Enumerable.Range(0, --indent).ForEach(item => sb.Append(INDENT_STRING)); | |
| } | |
| 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) | |
| { | |
| sb.AppendLine(); | |
| Enumerable.Range(0, indent).ForEach(item => sb.Append(INDENT_STRING)); | |
| } | |
| break; | |
| case ':': | |
| sb.Append(ch); | |
| if(!quoted) | |
| sb.Append(" "); | |
| break; | |
| default: | |
| sb.Append(ch); | |
| break; | |
| } | |
| } | |
| return sb.ToString(); | |
| } | |
| } | |
| static class Extensions | |
| { | |
| public static void ForEach<T>(this IEnumerable<T> ie, Action<T> action) | |
| { | |
| foreach(var i in ie) | |
| { | |
| action(i); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment