| | |
| | | public class JsonWriter |
| | | { |
| | | #region Fields |
| | | private static NumberFormatInfo number_format; |
| | | private static readonly NumberFormatInfo number_format; |
| | | |
| | | private WriterContext context; |
| | | private Stack<WriterContext> ctx_stack; |
| | |
| | | private StringBuilder inst_string_builder; |
| | | private bool pretty_print; |
| | | private bool validate; |
| | | private bool lower_case_properties; |
| | | private TextWriter writer; |
| | | #endregion |
| | | |
| | |
| | | public bool Validate { |
| | | get { return validate; } |
| | | set { validate = value; } |
| | | } |
| | | |
| | | public bool LowerCaseProperties { |
| | | get { return lower_case_properties; } |
| | | set { lower_case_properties = value; } |
| | | } |
| | | #endregion |
| | | |
| | |
| | | indent_value = 4; |
| | | pretty_print = false; |
| | | validate = true; |
| | | lower_case_properties = false; |
| | | |
| | | ctx_stack = new Stack<WriterContext> (); |
| | | context = new WriterContext (); |
| | |
| | | writer.Write (','); |
| | | |
| | | if (pretty_print && ! context.ExpectingValue) |
| | | writer.Write ('\n'); |
| | | writer.Write (Environment.NewLine); |
| | | } |
| | | |
| | | private void PutString (string str) |
| | |
| | | context.ExpectingValue = false; |
| | | } |
| | | |
| | | [CLSCompliant(false)] |
| | | public void Write (ulong number) |
| | | { |
| | | DoValidation (Condition.Value); |
| | |
| | | { |
| | | DoValidation (Condition.Property); |
| | | PutNewline (); |
| | | string propertyName = (property_name == null || !lower_case_properties) |
| | | ? property_name |
| | | : property_name.ToLowerInvariant(); |
| | | |
| | | PutString (property_name); |
| | | PutString (propertyName); |
| | | |
| | | if (pretty_print) { |
| | | if (property_name.Length > context.Padding) |
| | | context.Padding = property_name.Length; |
| | | if (propertyName.Length > context.Padding) |
| | | context.Padding = propertyName.Length; |
| | | |
| | | for (int i = context.Padding - property_name.Length; |
| | | for (int i = context.Padding - propertyName.Length; |
| | | i >= 0; i--) |
| | | writer.Write (' '); |
| | | |