internal static object ParseObject(object input, ValueNodeSchema schema, IContext context) { object result; string str; IEnumerable<string> strs; Stream stream; if ((str = input as string) != null) { result = ParseString(str, schema, context); } else if ((strs = input as IEnumerable<string>) != null && schema.Type == NodeType.List) { result = ParseList(strs, (schema as ListSchema).GetItemSchema(), context); } else if ((stream = input as Stream) != null && schema.Type == NodeType.File) { result = stream; } else { Trace.WriteLine("Unrecognized input type, it shall not be parsed: " + input.GetType()); result = input; } return result; }
/// <summary> /// </summary> public Field(ValueNodeSchema schema, string value = null) : base(schema.Key) { if (schema == null) { throw new ArgumentNullException("schema"); } NodeSchema = schema; Context = new DefaultContext(); _rawVal = value; }
internal static string Format(dynamic input, ValueNodeSchema schema, IContext context) { string result; if (schema == null || context == null) { result = Convert.ToString(input); } else { var config = context.Config; var type = schema.DataType; switch (type) { case DataType.String: result = FormatString(input); break; case DataType.Boolean: result = FormatBoolean(input, schema.GetApplicableFormat(config), config); break; case DataType.Integer: case DataType.Decimal: case DataType.BigInteger: case DataType.BigDecimal: result = FormatNumber(input, schema.GetApplicableFormat(config), config); break; case DataType.DateTime: result = FormatDateTime(input, schema.GetApplicableFormat(config), config); break; case DataType.Guid: result = FormatGuid(input, schema.GetApplicableFormat(config)); break; case DataType.Link: result = FormatUri(input); break; case DataType.Binary: result = FormatBinary(input); break; case DataType.Object: result = FormatCustomType(input, schema as FieldSchema, context); break; case DataType.List: var listFmt = schema.GetApplicableFormat(config); result = FormatList(input, listFmt, schema as ListSchema, context); break; case DataType.Table: throw new NotSupportedException("Formatting of a whole table is not supported"); default: throw new ArgumentOutOfRangeException(); } } return result; }
internal static object ParseString(string input, ValueNodeSchema schema, IContext context) { object result; var config = context.Config; switch (schema.DataType) { case DataType.String: result = input ?? String.Empty; break; case DataType.Boolean: result = ParseBoolean(input, schema.GetApplicableFormat(config)); break; case DataType.Integer: result = ParseInt(input, config.Culture); break; case DataType.BigInteger: result = ParseBigInt(input, config.Culture); break; case DataType.Decimal: result = ParseDecimal(input, config.Culture); break; case DataType.BigDecimal: result = ParseBigDecimal(input, config.Culture); break; case DataType.DateTime: result = ParseDateTime(input, schema.DataFormat ?? config.DateTimeFormat, config.Culture); break; case DataType.Guid: result = ParseGuid(input, schema.GetApplicableFormat(config)); break; case DataType.Link: result = ParseUri(input); break; case DataType.Binary: result = ParseBytes(input); break; case DataType.List: var listSchema = schema as ListSchema; result = ParseList(input, listSchema.GetApplicableFormat(config), listSchema.GetItemSchema(), context); break; case DataType.Object: result = ParseCustomType(input, schema as FieldSchema, context); break; default: throw new NotSupportedException("ParseString does not support this DataType: " + schema.DataType); } return result; }
/// <summary> /// </summary> public Field(ValueNodeSchema schema, object value) : base(schema.Key, value) { NodeSchema = schema; Context = new DefaultContext(); }
private static IEnumerable<object> ParseList(IEnumerable<string> items, ValueNodeSchema itemSchema, IContext context) { return itemSchema.DataType == DataType.String ? items : items.Select(itemData => !String.IsNullOrEmpty(itemData) ? ParseString(itemData, itemSchema, context) : itemSchema.DataType.GetDefaultValue()) .ToArray() .AsEnumerable(); }
private static IEnumerable<object> ParseList(string input, string format, ValueNodeSchema itemSchema, IContext context) { //TODO:consider escaping of the seperator (however, this method should be called for Default values only, so there's no need to worry) var itemVals = !String.IsNullOrEmpty(input) ? input.Split(new[]{format}, StringSplitOptions.None) : new string[0]; return ParseList(itemVals, itemSchema, context); }