Beispiel #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>();
        }
Beispiel #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);
            }
        }
Beispiel #3
0
 public MemberContext(CachedMember member, Context context)
 {
     Member = member;
     Format = context.Format;
     Mode = context.Mode;
     Type = member.Type.Type;
 }
Beispiel #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();
        }
Beispiel #5
0
 public TypeContext(CachedType type, Context context, bool isRoot = false)
 {
     Type = type;
     IsRoot = isRoot;
     Format = context.Format;
     Options = context.Options;
     Mode = context.Mode;
 }
Beispiel #6
0
 public ArrayItemContext(CachedType type, CachedMember member, Context context)
 {
     Type = type;
     Member = member;
     Format = context.Format;
     Options = context.Options;
     Mode = context.Mode;
 }
Beispiel #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;
 }
Beispiel #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;
 }
Beispiel #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);
 }
Beispiel #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>());
 }
Beispiel #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);
 }
Beispiel #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);
     }
 }
Beispiel #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);
 }
Beispiel #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);
 }
Beispiel #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));
        }
Beispiel #16
0
 public static string GetName(this NamingConventions<MemberContext> conventions,
     CachedMember member, Context context)
 {
     return conventions.GetName(new MemberContext(member, context));
 }
Beispiel #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));
 }
Beispiel #18
0
 public static string GetName(this NamingConventions<ArrayItemContext> conventions,
     CachedType type, CachedMember member, Context context)
 {
     return conventions.GetName(new ArrayItemContext(type, member, context));
 }
Beispiel #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));
 }