/// <summary> /// The constructor /// </summary> /// <param name="columnSeparator">String that is used to separate columns in the text</param> /// <param name="autoSizeColumns">Defines if the rows of a column shall all be of the same width</param> public DocumentFormatDefinitionBuilder(string columnSeparator, bool autoSizeColumns) { _columnSeparator = columnSeparator; _autosizeColumns = autoSizeColumns; ServiceStackTextHelpers.EmptyCtorDelegate defaultInitializer = ServiceStackTextHelpers.GetConstructorMethodToCache(typeof(T)); _instantiator = () => (T)defaultInitializer(); }
public void Parse(T item, string value) { // Remove unneeded quotes if (value.Length >= 2 && value[0] == '"' && value[value.Length - 1] == '"') { value = value.Substring(1, value.Length - 2).Replace("\"\"", "\""); } if (_isDummyColumn) { return; } if (_importFunc == null) { _importFunc = s => ServiceStackTextHelpers.GetParseFn(_propertyInfo.PropertyType, _stringFormat, _booleanTrue, _provider)(s); } try { object v; if (String.IsNullOrEmpty(value)) { if (_getDefaultValueFunc == null) { return; } v = _getDefaultValueFunc(); } else { v = _importFunc(value); } if (_setValueFunc == null) { _setValueFunc = _propertyInfo.GetValueSetter <T>(); } _setValueFunc(item, v); } catch (Exception ex) { throw new ImportException(Header, value, ex); } }
public string Serialize(T item) { if (item == null) { throw new ArgumentNullException("item"); } object value = _getValueFunc.Invoke(item); if (value == null && _getDefaultValueFunc != null) { value = _getDefaultValueFunc(); } if (value == null) { return(""); } if (_exportFunc == null) { _exportFunc = (x, ret) => ServiceStackTextHelpers.GetSerializeFunc(_propertyInfo.PropertyType, _stringFormat, _booleanTrue, _booleanFalse, _provider, _exportQuotedString)(ret); } try { string exportedValue = _exportFunc(item, value); if (_throwOnColumnOverflow && exportedValue.Length > ColumnWidth) { throw new OverflowException(string.Format("Column data width ({0}) exceeded maximum column width ({1})", exportedValue.Length, ColumnWidth)); } return(exportedValue); } catch (Exception ex) { throw new ExportException(Header, item, ex); } }