public static string ToText <TRec>(TRec record, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string jsonPath = null) { if (record is DataTable) { StringBuilder xml = new StringBuilder(); using (var w = new ChoJSONWriter(xml, configuration)) w.Write(record as DataTable); return(xml.ToString()); } else if (record is IDataReader) { StringBuilder xml = new StringBuilder(); using (var w = new ChoJSONWriter(xml, configuration)) w.Write(record as IDataReader); return(xml.ToString()); } if (configuration == null) { configuration = new ChoJSONRecordConfiguration(); } configuration.IgnoreRootName = true; configuration.RootName = null; if (configuration.SingleElement == null) { configuration.SingleElement = true; } configuration.SupportMultipleContent = true; return(ToTextAll(ChoEnumerable.AsEnumerable <TRec>(record), configuration, traceSwitch, jsonPath)); }
public ChoJSONReader(Stream inStream, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(inStream, "Stream"); Configuration = configuration; Init(); if (inStream is MemoryStream) { _textReader = new Lazy <TextReader>(() => new StreamReader(inStream)); } else { _textReader = new Lazy <TextReader>(() => { if (Configuration.DetectEncodingFromByteOrderMarks == null) { return(new StreamReader(inStream, Configuration.GetEncoding(inStream), false, Configuration.BufferSize)); } else { return(new StreamReader(inStream, Encoding.Default, Configuration.DetectEncodingFromByteOrderMarks.Value, Configuration.BufferSize)); } }); } //_closeStreamOnDispose = true; }
public ChoJSONReader(IEnumerable <JToken> jObjects, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(jObjects, "JObjects"); Configuration = configuration; Init(); _jObjects = jObjects; }
public ChoJSONReader(JToken jObject, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(jObject, "jObject"); Configuration = configuration; Init(); _jObjects = new JToken[] { jObject }; }
public static string ToText <TRec>(TRec record, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string jsonPath = null) where TRec : class { configuration.IgnoreRootName = true; configuration.RootName = null; configuration.SingleElement = true; return(ToTextAll(ChoEnumerable.AsEnumerable <TRec>(record), configuration, traceSwitch, jsonPath)); }
public ChoJSONReader(JsonTextReader JSONReader, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(JSONReader, "JSONReader"); Configuration = configuration; Init(); _JSONReader = JSONReader; }
public string Serialize(T record, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) { if (configuration == null) { configuration = Configuration; } return(ToText <T>(record, configuration, traceSwitch)); }
public string SerializeAll(IEnumerable <T> records, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) { if (configuration == null) { configuration = Configuration; } return(ToTextAll <T>(records, configuration, traceSwitch)); }
public static T LoadJToken(JToken jObject, ChoJSONRecordConfiguration configuration = null) { if (jObject == null) { return(default(T)); } return(LoadJTokens(new JToken[] { jObject }, configuration).FirstOrDefault()); }
public ChoJSONReader(TextReader textReader, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(textReader, "TextReader"); Configuration = configuration; Init(); _textReader = textReader; }
public ChoJSONWriter(TextWriter textWriter, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(textWriter, "TextWriter"); Configuration = configuration; Init(); _textWriter = new Lazy <TextWriter>(() => textWriter); }
public ChoJSONRecordReader(Type recordType, ChoJSONRecordConfiguration configuration) : base(recordType) { ChoGuard.ArgumentNotNull(configuration, "Configuration"); Configuration = configuration; _callbackRecord = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordRead>(recordType); //Configuration.Validate(); }
public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string jsonPath = null) { if (records == null) { return(null); } if (typeof(DataTable).IsAssignableFrom(typeof(TRec))) { StringBuilder json = new StringBuilder(); foreach (var dt in records.Take(1)) { using (var w = new ChoJSONWriter(json, configuration)) { w.Write(dt); } } return(json.ToString()); } else if (typeof(IDataReader).IsAssignableFrom(typeof(TRec))) { StringBuilder json = new StringBuilder(); foreach (var dt in records.Take(1)) { using (var w = new ChoJSONWriter(json, configuration)) { w.Write(dt); } } return(json.ToString()); } using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoJSONWriter <TRec>(writer, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }) { parser.Configuration.JSONPath = jsonPath; parser.Write(records); parser.Close(); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
private void Init() { if (Configuration == null) { Configuration = new ChoJSONRecordConfiguration(typeof(T)); } _writer = new ChoJSONRecordWriter(typeof(T), Configuration); _writer.RowsWritten += NotifyRowsWritten; }
public ChoJSONReader(Stream inStream, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(inStream, "Stream"); Configuration = configuration; Init(); _textReader = new StreamReader(inStream, Configuration.GetEncoding(inStream), false, Configuration.BufferSize); _closeStreamOnDispose = true; }
public ChoJSONWriter(string filePath, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath"); Configuration = configuration; Init(); _textWriter = new Lazy <TextWriter>(() => new StreamWriter(filePath, false, Configuration.Encoding, Configuration.BufferSize)); _closeStreamOnDispose = true; }
public ChoJSONReader(string filePath, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNullOrEmpty(filePath, "FilePath"); Configuration = configuration; Init(); _JSONReader = new JsonTextReader(new StreamReader(ChoPath.GetFullPath(filePath), Configuration.GetEncoding(filePath), false, Configuration.BufferSize)); _closeStreamOnDispose = true; }
public T Deserialize(JToken jObject, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) { if (configuration == null) { configuration = Configuration; } return(new ChoJSONReader <T>(jObject, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }.FirstOrDefault()); }
public IEnumerable <T> Deserialize(IEnumerable <JToken> jObjects, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) { if (configuration == null) { configuration = Configuration; } return(new ChoJSONReader <T>(jObjects, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }); }
public IEnumerable <T> Deserialize(Stream inStream, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null) { if (configuration == null) { configuration = Configuration; } return(new ChoJSONReader <T>(inStream, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }); }
private void Init() { var recordType = typeof(T).GetUnderlyingType(); if (Configuration == null) { Configuration = new ChoJSONRecordConfiguration(recordType); } _writer = new ChoJSONRecordWriter(recordType, Configuration); _writer.Writer = this; _writer.RowsWritten += NotifyRowsWritten; }
private void Init() { _enumerator = new Lazy <IEnumerator <T> >(() => GetEnumerator()); if (Configuration == null) { Configuration = new ChoJSONRecordConfiguration(typeof(T)); } else { Configuration.RecordType = typeof(T); } _prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; }
public ChoJSONWriter(Stream inStream, ChoJSONRecordConfiguration configuration = null) { ChoGuard.ArgumentNotNull(inStream, "Stream"); Configuration = configuration; Init(); if (inStream is MemoryStream) { _textWriter = new Lazy <TextWriter>(() => new StreamWriter(inStream)); } else { _textWriter = new Lazy <TextWriter>(() => new StreamWriter(inStream, Configuration.Encoding, Configuration.BufferSize)); } //_closeStreamOnDispose = true; }
internal static string ToText(object rec, ChoJSONRecordConfiguration configuration, Encoding encoding, int bufferSize, TraceSwitch traceSwitch = null) { ChoJSONRecordWriter writer = new ChoJSONRecordWriter(rec.GetType(), configuration); writer.TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitchOff : traceSwitch; using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var sw = new StreamWriter(stream, configuration.Encoding, configuration.BufferSize)) { writer.WriteTo(sw, new object[] { rec }).Loop(); sw.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
private void Init() { _enumerator = new Lazy <IEnumerator <T> >(() => GetEnumerator()); var recordType = typeof(T).GetUnderlyingType(); if (Configuration == null) { Configuration = new ChoJSONRecordConfiguration(recordType); } else { Configuration.RecordType = recordType; } Configuration.RecordType = Configuration.RecordType.GetUnderlyingType(); Configuration.IsDynamicObject = Configuration.RecordType.IsDynamicType(); _prevCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = Configuration.Culture; }
internal void Validate(ChoJSONRecordConfiguration config) { try { if (FieldName.IsNullOrWhiteSpace()) { FieldName = Name; } //if (JSONPath.IsNullOrWhiteSpace()) // throw new ChoRecordConfigurationException("Missing XPath."); if (FillChar != null) { if (FillChar.Value == ChoCharEx.NUL) { throw new ChoRecordConfigurationException("Invalid '{0}' FillChar specified.".FormatString(FillChar)); } } if (Size != null && Size.Value <= 0) { throw new ChoRecordConfigurationException("Size must be > 0."); } if (ErrorMode == null) { ErrorMode = config.ErrorMode; // config.ErrorMode; } if (IgnoreFieldValueMode == null) { IgnoreFieldValueMode = config.IgnoreFieldValueMode; } if (QuoteField == null) { QuoteField = config.QuoteAllFields; } } catch (Exception ex) { throw new ChoRecordConfigurationException("Invalid configuration found at '{0}' field.".FormatString(Name), ex); } }
public static string ToTextAll <TRec>(IEnumerable <TRec> records, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null, string jsonPath = null) where TRec : class { using (var stream = new MemoryStream()) using (var reader = new StreamReader(stream)) using (var writer = new StreamWriter(stream)) using (var parser = new ChoJSONWriter <TRec>(writer, configuration) { TraceSwitch = traceSwitch == null ? ChoETLFramework.TraceSwitch : traceSwitch }) { parser.Configuration.JSONPath = jsonPath; parser.Write(records); writer.Flush(); stream.Position = 0; return(reader.ReadToEnd()); } }
public ChoJSONReader(ChoJSONRecordConfiguration configuration = null) { Configuration = configuration; Init(); }
public ChoJSONReader(StringBuilder sb, ChoJSONRecordConfiguration configuration = null) : this(new StringReader(sb.ToString()), configuration) { }
public static ChoJSONReader <T> LoadJTokens(IEnumerable <JToken> jObjects, ChoJSONRecordConfiguration configuration = null) { var r = new ChoJSONReader <T>(jObjects, configuration); return(r); }