Пример #1
0
        // Serializable nodes

        public static ObjectNodeBase CreateSerializableRoot(object @object, CachedType type, Options options, string format)
        {
            var context = new Context(options, Mode.Serialize, format);
            return CreateSerializable(
                context.Options.TypeNameConventions.GetName(type, context, true), 
                ValueFactory.Create(@object, type, true, context.Options), null, context).As<ObjectNodeBase>();
        }
Пример #2
0
        public static ObjectNodeBase CreateDeserializable(string name, IValue @object, INode parent,
            Context context, CachedMember member = null)
        {
            var type = @object.SpecifiedType;
            var kind = GetTypeKind(type, context.Options);

            if (kind == TypeKind.Simple)
            {
                if (parent == null) throw new TypeNotSupportedException("simple type",
                    @object.SpecifiedType, Mode.Deserialize, "complex types");
                return new ValueNode(context, name, @object, member, parent);
            }

            Func<object> factory = () => ObjectFactory.CreateInstance(type,
                context.Options.Deserialization.ObjectFactory, parent.MapOrDefault(x => x.Value));

            if (member == null || parent == null) @object.Instance = factory();
            else @object = ValueFactory.Create(@object, factory);

            switch (kind)
            {
                case TypeKind.Dictionary: return new DictionaryNode(context, name, @object, member, parent);
                case TypeKind.Enumerable: return new EnumerableNode(context, name, @object, member, parent);
                default: return new ObjectNode(context, name, @object, member, parent);
            }
        }
Пример #3
0
 public MemberContext(CachedMember member, Context context)
 {
     Member = member;
     Format = context.Format;
     Mode = context.Mode;
     Type = member.Type.Type;
 }
Пример #4
0
        public override void Encode(Stream stream, Encoding encoding = null)
        {
            var context = new Context(_options, Mode.Deserialize, NodeFormat);
            var header = new SimpleValue(_type.GenericEnumerableType);
            var columns = new ObjectNode(context, null, header, null, null)
                .Select(x => x.Name).ToArray();

            if (!columns.Any()) return;

            var writer = new StreamWriter(stream, encoding ?? UTF8Encoding.NoBOM);
            var qualifier = _options.CsvQualifier;

            Action<string[]> writeLine = x => writer
                .Write(x.Select(y =>
                    $"{qualifier}{y?.Replace(qualifier, qualifier + qualifier)}{qualifier}")
                .Aggregate((a, i) => $"{a}{_options.CsvDelimiter}{i}") + _options.CsvNewLine);

            writeLine(columns);

            foreach (var row in _rows)
            {
                var fields = new string[columns.Length];
                row.Where(x => columns.Contains(x.Name))
                    .ForEach(x => fields[columns.IndexOf(x.Name)] = x.Value?.ToString());
                writeLine(fields);
            }
                
            writer.Flush();
        }
Пример #5
0
 public TypeContext(CachedType type, Context context, bool isRoot = false)
 {
     Type = type;
     IsRoot = isRoot;
     Format = context.Format;
     Options = context.Options;
     Mode = context.Mode;
 }
Пример #6
0
 public ArrayItemContext(CachedType type, CachedMember member, Context context)
 {
     Type = type;
     Member = member;
     Format = context.Format;
     Options = context.Options;
     Mode = context.Mode;
 }
Пример #7
0
 public EnumContext(object value, Type type, CachedMember member, Context context)
 {
     Value = value;
     Type = type;
     Member = member;
     Format = context.Format;
     Options = context.Options;
     Mode = context.Mode;
 }
Пример #8
0
 public ValueNode(
     Context context,
     string name,
     IValue source,
     CachedMember member,
     INode parent)
     : base(name, source, member, parent, context)
 {
     _friendlyParseMessages = Context.Options.Deserialization.FriendlyParseErrorMessages;
 }
Пример #9
0
 public static ObjectNodeBase CreateDeserializableRoot(string name, CachedType type, string format, Options options)
 {
     var context = new Context(options, Mode.Deserialize, format);
     var @object = ValueFactory.Create(type);
     if (!context.Options.Deserialization.IgnoreRootName)
     {
         var expectedName = context.Options.TypeNameConventions.GetName(@object.SpecifiedType, context, true);
         if (!name.Equals(expectedName, options.Deserialization.NameComparison))
             throw new InvalidRootNameDeserializationException(type, format, name, expectedName);
     }
     return CreateDeserializable(name, @object, null, context);
 }
Пример #10
0
 public ObjectNode(
     Context context,
     string name,
     IValue @object,
     CachedMember member,
     INode parent) :
         base(name, @object, member, parent, context)
 {
     _members = new Lazy<IEnumerable<MemberDefinition>>(EnumerateMembers);
     _nodes = new Lazy<IEnumerable<INode>>(EnumerateNodes);
     _addedNodes = new Lazy<IList<INode>>(() => new List<INode>());
 }
Пример #11
0
 public DictionaryNode(
     Context context, 
     string name, 
     IValue dictionary, 
     CachedMember member,
     INode parent)
     : base(name, dictionary, member, parent, context)
 {
     _dictionary = new Lazy<IDictionary>(() =>
         dictionary.Instance.MapOrDefault(GenericDictionaryAdapter.Create));
     if (SpecifiedType.IsGenericDictionary) _itemType = SpecifiedType.GenericDictionaryTypes.Value;
     _nodes = new Lazy<IEnumerable<INode>>(EnumerateNodes);
 }
Пример #12
0
 public static ObjectNodeBase CreateSerializable(string name, IValue @object, INode parent,
     Context context, CachedMember member = null)
 {
     switch (GetTypeKind(@object.SpecifiedType, context.Options))
     {
         case TypeKind.Simple:
             if (parent == null) throw new TypeNotSupportedException("simple type", @object.SpecifiedType, Mode.Serialize, "complex types");
             return new ValueNode(context, name, @object, member, parent);
         case TypeKind.Dictionary: return new DictionaryNode(context, name, @object, member, parent);
         case TypeKind.Enumerable: return new EnumerableNode(context, name, @object, member, parent);
         default: return new ObjectNode(context, name, @object, member, parent);
     }
 }
Пример #13
0
 public EnumerableNode(
     Context context, 
     string name, 
     IValue enumerable, 
     CachedMember member,
     INode parent)
     : base(name, enumerable, member, parent, context)
 {
     _list = new Lazy<IList>(() => 
         enumerable.Instance.MapOrDefault(x => enumerable.ActualType.IsArray ? 
             ArrayAdapter.Create(enumerable) : GenericListAdapter.Create(x)));
     if (SpecifiedType.IsGenericEnumerable) _itemType = enumerable.SpecifiedType.GenericEnumerableType;
     _nodes = new Lazy<IEnumerable<INode>>(EnumerateNodes);
 }
Пример #14
0
 protected ObjectNodeBase(
     string name, 
     IValue source,
     CachedMember member,
     INode parent,  
     Context context) : base(parent)
 {
     _name = name;
     Source = source;
     Context = context;
     Member = member;
     HasMember = member != null;
     if (HasMember) Metadata.Add(member.Attributes);
     else if (Source?.ActualType != null) Metadata.Add(ActualType.Attributes);
 }
Пример #15
0
        public Encoder(Stream output, Encoding encoding, 
            Options options, string format, CachedType type)
        {
            if (type.IsGenericEnumerable && options.Serialization
                .SerializationType == SerializationType.SpecifiedType)
            {
                var header = new SimpleValue(type.GenericEnumerableType);
                var context = new Context(options, Mode.Deserialize, format);
                _columns = new ObjectNode(context, null, header, null, null)
                    .Select(x => x.Name).ToArray();
            }

            _header = options.CsvHeader;
            _qualifier = options.CsvQualifier;
            _delimiter = options.CsvDelimiter;
            _newLine = options.CsvNewLine;
            _writer = new Lazy<StreamWriter>(() => new StreamWriter(output, encoding));
        }
Пример #16
0
 public static string GetName(this NamingConventions<MemberContext> conventions,
     CachedMember member, Context context)
 {
     return conventions.GetName(new MemberContext(member, context));
 }
Пример #17
0
 public static string GetName(this NamingConventions<EnumContext> conventions,
     object value, Type type, CachedMember member, Context context)
 {
     return conventions.GetName(new EnumContext(value, type, member, context));
 }
Пример #18
0
 public static string GetName(this NamingConventions<ArrayItemContext> conventions,
     CachedType type, CachedMember member, Context context)
 {
     return conventions.GetName(new ArrayItemContext(type, member, context));
 }
Пример #19
0
 public static string GetName(this NamingConventions<TypeContext> conventions,
     CachedType type, Context context, bool isRoot = false)
 {
     return conventions.GetName(new TypeContext(type, context, isRoot));
 }