Exemplo n.º 1
0
 private void should_have_valid_parameters(
     INode source, INode target, Options options)
 {
     source.ShouldBeSameAs(_source);
     target.ShouldBeSameAs(_target);
     options.ShouldBeSameAs(_options);
 }
Exemplo n.º 2
0
 public FileNode(Stream stream, Options options, Encoding encoding = null)
 {
     _options = options;
     _readStream = stream;
     _encoding = encoding ?? DefaultEncoding;
     _readRows = new Lazy<List<RowNode>>(ParseFile);
 }
Exemplo n.º 3
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>();
        }
Exemplo n.º 4
0
 public static ObjectNode CreateAccessibilityNode(Options options, Mode mode)
 {
     var value = new SimpleValue(new MemberAccessibilityEnumeration(), 
         typeof(MemberAccessibilityEnumeration).ToCachedType());
     return new ObjectNode(new Context(options ?? Options.Create(), 
         mode, "xml"), null, value, null, null);
 }
Exemplo n.º 5
0
        public void Setup()
        {
            _options = Options.Create();
            _source = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _target = new ValueNode(
                new Context(_options, Mode.Deserialize, "json"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _readers = new ReaderConventions(_options);

            _visitorIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _readerIncrementValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return s.Value == null ? 1 : (int)s.Value + 1;
            };

            _readerIncrementNullableValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(v, s, t, o);
                return s.Value == null ? 1 : (int?)s.Value + 1;
            };
        }
Exemplo n.º 6
0
 public static IValue CreateMemberValue(Mode mode, object value, Options options = null)
 {
     return ValueFactory.Create(mode,
         new SimpleValue(value, value.GetType().ToCachedType()),
         new CachedMember(value.GetType().GetProperty("Value")),
         options ?? Options.Create());
 }
Exemplo n.º 7
0
 private FileNode(NodeType nodeType, Type type, Encoding encoding, Options options)
 {
     _type = type.ToCachedType();
     if (nodeType != NodeType.Array || !_type.IsEnumerable)
         throw new BenderException("Only arrays can be serialized.");
     _encoding = encoding ?? DefaultEncoding;
     _options = options;
 }
Exemplo n.º 8
0
 private void should_have_valid_parameters(
     object value, INode source, INode target, Options options)
 {
     value.ShouldEqual(source.Value);
     source.ShouldBeSameAs(_source);
     target.ShouldBeSameAs(_target);
     options.ShouldBeSameAs(_options);
 }
Exemplo n.º 9
0
 public void Setup()
 {
     _options = Options.Create();
     _source = new Node { NodeType = NodeType.Value };
     _target = new Node { NodeType = NodeType.Value };
     _conventions = new VisitConventions<INode, INode>(_options,
         (e, s, t) => new VisitException("The visit failed yo!", e));
 }
Exemplo n.º 10
0
 public FileNode(NodeType nodeType, Type type, Options options)
 {
     _options = options;
     _type = type.ToCachedType();
     if (nodeType != NodeType.Array || !_type.IsEnumerable)
         throw new BenderException("Only arrays can be serialized.");
     _rows = new List<RowNode>();
 }
Exemplo n.º 11
0
 public static Options Create(Action<OptionsDsl> configure = null)
 {
     if (configure != null)
     {
         var options = new Options();
         configure(new OptionsDsl(options));
         return options;
     }
     return Empty;
 }
Exemplo n.º 12
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);
 }
Exemplo n.º 13
0
        public SerializationOptions(Options options)
        {
            XmlValueNodeType = XmlValueNodeType.Element;
            XmlNamespaces = new Dictionary<string, XNamespace>();
            SerializationType = SerializationType.SpecifiedType;

            Writers = new WriterConventions(options);

            Writers.AddValueWriter<Version>((v, s, t, o) => v.ToString());
            Writers.AddValueWriter<MailAddress>((v, s, t, o) => v.ToString());
            Writers.AddValueWriter<IPAddress>((v, s, t, o) => v.ToString());
            Writers.AddValueWriter<byte[]>((v, s, t, o) => Convert.ToBase64String(v));
        }
Exemplo n.º 14
0
 protected XmlNodeBase(XObject @object, ElementNode parent, Options options) : base(parent)
 {
     Options = options;
     if (@object is XAttribute)
     {
         XmlType = XmlObjectType.Attribute;
         Attribute = @object.As<XAttribute>();
     }
     else if (@object is XElement)
     {
         XmlType = XmlObjectType.Element;
         Element = @object.As<XElement>();
     }
     else throw new XObjectNotSupportedException(@object);
 }
Exemplo n.º 15
0
        public DeserializationOptions(Options options)
        {
            NameComparison = StringComparison.Ordinal;
            EnumValueComparison = StringComparison.OrdinalIgnoreCase;
            IgnoreXmlAttributes = false;

            FriendlyParseErrorMessages = new Dictionary<Type, string>
            {
                [typeof(Enum)] = "Option '{0}' is not valid.",
                [typeof(char)] = "Char '{0}' not valid, must be exactly one character.",
                [typeof(bool)] = "Boolean '{0}' not formatted correctly, must be 'true' or 'false'.",
                [typeof(sbyte)] = "Byte '{0}' not formatted correctly, must be an integer between -128 and 127.",
                [typeof(byte)] = "Unsigned byte '{0}' not formatted correctly, must be an integer between 0 and 255.",
                [typeof(short)] = "Integer '{0}' not formatted correctly, must be an integer between -32,768 and 32,767.",
                [typeof(ushort)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 65,535.",
                [typeof(int)] = "Integer '{0}' not formatted correctly, must be an integer between -2,147,483,648 and 2,147,483,647.",
                [typeof(uint)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 4,294,967,295.",
                [typeof(long)] = "Integer '{0}' not formatted correctly, must be an integer between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.",
                [typeof(ulong)] = "Unsigned integer '{0}' not formatted correctly, must be an integer between 0 and 18,446,744,073,709,551,615.",
                [typeof(float)] = "32 bit float '{0}' not formatted correctly, must be a single-precision 32 bit float between -3.402823e38 and 3.402823e38.",
                [typeof(double)] = "64 bit float '{0}' not formatted correctly, must be a double-precision 64-bit float between -1.79769313486232e308 and 1.79769313486232e308.",
                [typeof(decimal)] = "Decimal '{0}' not formatted correctly, must be a decimal number between -79,228,162,514,264,337,593,543,950,335 and 79,228,162,514,264,337,593,543,950,335.",
                [typeof(DateTime)] = "Date '{0}' not formatted correctly, must be formatted as m/d/yyy h:m:s AM.",
                [typeof(Guid)] = "UUID '{0}' not formatted correctly, should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).",
                [typeof(TimeSpan)] = "Timespan '{0}' not formatted correctly, must be formatted as 'd.h:m:s'.",
                [typeof(Uri)] = "Uri '{0}' not formatted correctly, must be formatted as 'scheme://host'.",

                [typeof(Version)] = "Version '{0}' not formatted correctly, must be formatted as '1.2.3.4'.",
                [typeof(MailAddress)] = "Email address '{0}' not formatted correctly, must be formatted as '*****@*****.**'.",
                [typeof(IPAddress)] = "IP address '{0}' not formatted correctly, must be formatted as '1.2.3.4'."
            };

            FriendlyParseErrorMessages[typeof(IntPtr)] = FriendlyParseErrorMessages[typeof(int)];
            FriendlyParseErrorMessages[typeof(UIntPtr)] = FriendlyParseErrorMessages[typeof(ushort)];

            Readers = new ReaderConventions(options);

            IgnoreUnmatchedElements = true;
            IgnoreUnmatchedMembers = true;

            Readers.AddValueReader((v, s, t, o) => Version.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new MailAddress(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => IPAddress.Parse(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => Convert.FromBase64String(v.ToString()));
            Readers.AddValueReader((v, s, t, o) => new SqlConnectionStringBuilder(v.ToString()));
        }
Exemplo n.º 16
0
        public void Setup()
        {
            _options = Options.Create();
            _source = new ValueNode(
                new Context(_options, Mode.Deserialize, "object"), null,
                new SimpleValue(typeof(int?).ToCachedType()), null, null);
            _target = new Node("yada") { NodeType = NodeType.Value, Format = "xml", Type = "element" };
            _writers = new WriterConventions(_options);

            _visitorIncrement = (s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _writerObjectIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = (int?) t.Value + 1 ?? 1;
            };

            _writerIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = (int?) t.Value + 1 ?? 1;
            };

            _writerNullableIncrement = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                t.Value = t.Value == null ? 1 : (int?)t.Value + 1;
            };

            _writerIncrementValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                return v + 1;
            };

            _writerIncrementNullableValue = (v, s, t, o) =>
            {
                should_have_valid_parameters(s, t, o);
                return v == null ? 1 : v + 1;
            };
        }
Exemplo n.º 17
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));
        }
Exemplo n.º 18
0
 public static IValue Create(Mode mode, IValue @object, CachedMember member, Options options)
 {
     return new MemberValue(@object, member, x => mode.IsSerialize() && UseActualType(x, options));
 }
Exemplo n.º 19
0
 public static IValue Create(object @object, CachedType type, bool @readonly, Options options)
 {
     return new SimpleValue(@object, UseActualType(type, options) ? @object.ToCachedType() : type, @readonly);
 }
Exemplo n.º 20
0
 private static TypeKind GetTypeKind(CachedType type, Options options)
 {
     return type.GetKind(
         options.TreatEnumerableImplsAsObjects,
         options.TreatDictionaryImplsAsObjects);
 }
Exemplo n.º 21
0
 // Deserializable nodes
 public static ObjectNodeBase CreateDeserializableRoot(CachedType type, string format, Options options)
 {
     return CreateDeserializable(null, ValueFactory.Create(type), null, new Context(options, Mode.Deserialize, format));
 }
Exemplo n.º 22
0
 public AttributeNode(XAttribute attribute, ElementNode parent, Options options)
     : base(attribute, parent, options)
 {
 }
Exemplo n.º 23
0
 public FileNode(NodeType nodeType, Type type, Options options,
     Stream stream, Encoding encoding)
     : this(nodeType, type, encoding, options)
 {
     _writeEncoder = new Encoder(stream, _encoding, options, NodeFormat, _type);
 }
Exemplo n.º 24
0
        // Inserting nodes

        public static ObjectNode CreateInsertionNode(Options options = null, IValue value = null, Mode mode = Mode.Deserialize)
        {
            return new ObjectNode(new Context(options ?? Options.Create(), mode, "xml"), null,
                value ?? new SimpleValue(typeof(MemberAccessModifierEnumeration).ToCachedType()), null, null);
        }
Exemplo n.º 25
0
 public static Context CreateContext(Mode mode, Options options = null)
 {
     return new Context(options ?? Options.Create(), mode, "xml");
 }
Exemplo n.º 26
0
 public static ObjectNode CreateAdditionNode(Options options = null, IValue value = null, Mode mode = Mode.Deserialize)
 {
     return new ObjectNode(new Context(options ?? Options.Create(), mode, "xml"), null,
         value ?? new SimpleValue(typeof(NodeAddition).ToCachedType()), null, null);
 }
Exemplo n.º 27
0
 public FileNode(NodeType nodeType, Type type, Options options)
     : this(nodeType, type, null, options)
 {
     _writeRows = new List<RowNode>();
 }
Exemplo n.º 28
0
 private static bool UseActualType(CachedType type, Options options)
 {
     return options.Serialization.SerializationType.IsActual() || 
         type == null || type.Type == typeof(object);
 }
Exemplo n.º 29
0
 public FileNode(byte[] bytes, Options options, Encoding encoding = null) : 
     this(new MemoryStream(bytes), options, encoding) { }
Exemplo n.º 30
0
 public FileNode(string data, Options options, Encoding encoding = null) : 
     this(new MemoryStream((encoding ?? Encoding.UTF8).GetBytes(data)), 
         options, encoding) { }