/// <summary> /// Returns a 'pretty printed' version of the specified JSON string, formatted for human /// consumption. /// </summary> /// <param name="json">A valid JSON string.</param> /// <returns>A 'pretty printed' version of the specified JSON string.</returns> public static string Format(string json) { if (json == null) { throw new ArgumentNullException("json should not be null."); } var context = new JsonFormatterStrategyContext(); var formatter = new JsonFormatterInternal(context); return formatter.Format(json); }
public JsonFormatterInternal(JsonFormatterStrategyContext context) { _context = context; _context.ClearStrategies(); _context.AddCharacterStrategy(new OpenBracketStrategy()); _context.AddCharacterStrategy(new CloseBracketStrategy()); _context.AddCharacterStrategy(new OpenSquareBracketStrategy()); _context.AddCharacterStrategy(new CloseSquareBracketStrategy()); _context.AddCharacterStrategy(new SingleQuoteStrategy()); _context.AddCharacterStrategy(new DoubleQuoteStrategy()); _context.AddCharacterStrategy(new CommaStrategy()); _context.AddCharacterStrategy(new ColonCharacterStrategy()); _context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\n')); _context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\r')); _context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\t')); _context.AddCharacterStrategy(new SkipWhileNotInStringStrategy(' ')); }
public JsonFormatterInternal(JsonFormatterStrategyContext context) { this.context = context; this.context.ClearStrategies(); this.context.AddCharacterStrategy(new OpenBracketStrategy()); this.context.AddCharacterStrategy(new CloseBracketStrategy()); this.context.AddCharacterStrategy(new OpenSquareBracketStrategy()); this.context.AddCharacterStrategy(new CloseSquareBracketStrategy()); this.context.AddCharacterStrategy(new SingleQuoteStrategy()); this.context.AddCharacterStrategy(new DoubleQuoteStrategy()); this.context.AddCharacterStrategy(new CommaStrategy()); this.context.AddCharacterStrategy(new ColonCharacterStrategy()); this.context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\n')); this.context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\r')); this.context.AddCharacterStrategy(new SkipWhileNotInStringStrategy('\t')); this.context.AddCharacterStrategy(new SkipWhileNotInStringStrategy(' ')); }