示例#1
0
        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));
        }
示例#2
0
        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;
        }
示例#3
0
        public ChoJSONReader(IEnumerable <JToken> jObjects, ChoJSONRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(jObjects, "JObjects");

            Configuration = configuration;
            Init();
            _jObjects = jObjects;
        }
示例#4
0
        public ChoJSONReader(JToken jObject, ChoJSONRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(jObject, "jObject");

            Configuration = configuration;
            Init();
            _jObjects = new JToken[] { jObject };
        }
示例#5
0
 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));
 }
示例#6
0
        public ChoJSONReader(JsonTextReader JSONReader, ChoJSONRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(JSONReader, "JSONReader");

            Configuration = configuration;
            Init();

            _JSONReader = JSONReader;
        }
示例#7
0
        public string Serialize(T record, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null)
        {
            if (configuration == null)
            {
                configuration = Configuration;
            }

            return(ToText <T>(record, configuration, traceSwitch));
        }
示例#8
0
        public string SerializeAll(IEnumerable <T> records, ChoJSONRecordConfiguration configuration = null, TraceSwitch traceSwitch = null)
        {
            if (configuration == null)
            {
                configuration = Configuration;
            }

            return(ToTextAll <T>(records, configuration, traceSwitch));
        }
示例#9
0
        public static T LoadJToken(JToken jObject, ChoJSONRecordConfiguration configuration = null)
        {
            if (jObject == null)
            {
                return(default(T));
            }

            return(LoadJTokens(new JToken[] { jObject }, configuration).FirstOrDefault());
        }
示例#10
0
        public ChoJSONReader(TextReader textReader, ChoJSONRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(textReader, "TextReader");

            Configuration = configuration;
            Init();

            _textReader = textReader;
        }
示例#11
0
        public ChoJSONWriter(TextWriter textWriter, ChoJSONRecordConfiguration configuration = null)
        {
            ChoGuard.ArgumentNotNull(textWriter, "TextWriter");

            Configuration = configuration;
            Init();

            _textWriter = new Lazy <TextWriter>(() => textWriter);
        }
示例#12
0
        public ChoJSONRecordReader(Type recordType, ChoJSONRecordConfiguration configuration) : base(recordType)
        {
            ChoGuard.ArgumentNotNull(configuration, "Configuration");
            Configuration = configuration;

            _callbackRecord = ChoMetadataObjectCache.CreateMetadataObject <IChoNotifyRecordRead>(recordType);

            //Configuration.Validate();
        }
示例#13
0
        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());
                        }
        }
示例#14
0
        private void Init()
        {
            if (Configuration == null)
            {
                Configuration = new ChoJSONRecordConfiguration(typeof(T));
            }

            _writer              = new ChoJSONRecordWriter(typeof(T), Configuration);
            _writer.RowsWritten += NotifyRowsWritten;
        }
示例#15
0
        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;
        }
示例#16
0
        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;
        }
示例#17
0
        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;
        }
示例#18
0
        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());
        }
示例#19
0
        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
            });
        }
示例#20
0
        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
            });
        }
示例#21
0
        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;
        }
示例#22
0
        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;
        }
示例#23
0
        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;
        }
示例#24
0
        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());
                    }
        }
示例#25
0
        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);
            }
        }
示例#27
0
        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());
                        }
        }
示例#28
0
 public ChoJSONReader(ChoJSONRecordConfiguration configuration = null)
 {
     Configuration = configuration;
     Init();
 }
示例#29
0
 public ChoJSONReader(StringBuilder sb, ChoJSONRecordConfiguration configuration = null) : this(new StringReader(sb.ToString()), configuration)
 {
 }
示例#30
0
        public static ChoJSONReader <T> LoadJTokens(IEnumerable <JToken> jObjects, ChoJSONRecordConfiguration configuration = null)
        {
            var r = new ChoJSONReader <T>(jObjects, configuration);

            return(r);
        }