Ejemplo n.º 1
0
        public static dynamic DynDeserialize(this string objectString, SerializerMode mode = SerializerMode.Default)
        {
            if (mode == SerializerMode.Default)
            {
                mode = DefaultMode;
            }
            //todo onError?
            switch (mode)
            {
            case SerializerMode.Default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);

            case SerializerMode.Xml:

                var     doc      = XDocument.Parse(objectString); //or XDocument.Load(path)
                var     jsonText = JsonConvert.SerializeXNode(doc);
                dynamic dyn      = JObject.Parse(jsonText);       //or JsonConvert.DeserializeObject<ExpandoObject>(jsonText);

                return(dyn);

            case SerializerMode.Json:
                return(JObject.Parse(objectString));

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
        }
 public SerializationContext(ContextMode contextMode, [NotNull] IStreamSource streamSource, SerializerMode serializerMode)
 {
     ContextMode = contextMode;
     StreamSource = streamSource;
     SerializerMode = serializerMode;
     _backgroundStream = new BackgroundStream();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Supports serializing a dynamic object
        /// </summary>
        /// <param name="o"></param>
        /// <param name="mode"></param>
        /// <param name="xmlroot">If an object has more than 1 top-level field this must be specified</param>
        /// <returns></returns>
        public static string DynSerialize(object o, SerializerMode mode, string xmlroot = null)
        {
            if (mode == SerializerMode.Default)
            {
                mode = DefaultMode;
            }


            //todo а как удалить из динамика проперть?

            //for json
            var json = JsonConvert.SerializeObject(o);

            if (mode == SerializerMode.Json)
            {
                return(json);
            }


            var xmldoc = JsonConvert.DeserializeXmlNode(json, xmlroot);

            //beautifying (could simply do return xmldoc.OuterXml;)
            var sb = new StringBuilder();
            var xmlWriterSettings = new XmlWriterSettings {
                Indent = true, OmitXmlDeclaration = true
            };

            using (var writer = XmlWriter.Create(sb, xmlWriterSettings))
            {
                xmldoc.Save(writer);
            }

            return(sb.ToString());
        }
Ejemplo n.º 4
0
 public ShaderGraphSerializer(Stream stream, SerializerMode mode) : base(stream, mode)
 {
     parsedVariables = new Dictionary <string, Variable>();
     parsedNodes     = new Dictionary <string, NodeBase>();
     parsedMethods   = new Dictionary <string, MethodBase>();
     AllowIdentity   = true;
 }
Ejemplo n.º 5
0
        private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
        {
            var serializer = new BinarySerializer(stream, mode, Text.Encoding.ASCII);

            serializer.ArrayLengthType = ArrayLengthType.Int;
            serializer.RegisterDynamicList <MaterialTexture>("MATL");
            return(serializer);
        }
 private SerializationContext(ContextMode contextMode, [NotNull] IStreamSource streamSource,
                              SerializerMode serializerMode, [NotNull] BackgroundStream parentStream)
 {
     ContextMode = contextMode;
     StreamSource = streamSource;
     SerializerMode = serializerMode;
     _backgroundStream = parentStream;
 }
Ejemplo n.º 7
0
 public override void Init(SerializerMode mode)
 {
     _mode = mode;
     if (_cache != null)
     {
         _cache = new SerializerCache <DateTime>(mode);
     }
 }
Ejemplo n.º 8
0
 public override void Init(SerializerMode mode)
 {
     _mode = mode;
     if (_refCache != null)
     {
         _refCache = new SerializerCache <byte[]>(mode);
     }
 }
Ejemplo n.º 9
0
 public override void Init(SerializerMode mode)
 {
     _mode = mode;
     Cache?.Clear(mode);
     Cache16?.Clear(mode);
     Cache32?.Clear(mode);
     _useCache = (mode != SerializerMode.NoCached);
 }
Ejemplo n.º 10
0
        private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
        {
            var serializer = new BinarySerializer(stream, mode, Encoding.BigEndianUnicode)
            {
                ArrayLengthType = ArrayLengthType.Int
            };

            return(serializer);
        }
        public void Progress(object graph, TContext context, SerializerMode mode)
        {
            foreach (var mappingEntry in _mapper.Entries)
            {
                mappingEntry.Progress(graph, context, mode);
            }

            CleanUp(context);
        }
            private Stream OpenStream(object target, SerializerMode mode)
            {
                if (_current != null)
                {
                    throw new InvalidOperationException();
                }

                _current = _open(target, mode);
                return(_current);
            }
Ejemplo n.º 13
0
 private DeserializersTable(SerializerMode mode)
 {
     Mode = mode;
     DateTimeOffsetSerializer.Init(mode);
     DateTimeSerializer.Init(mode);
     GuidSerializer.Init(mode);
     NumberSerializer.Init(mode);
     TimeSpanSerializer.Init(mode);
     ByteArraySerializer.Init(mode);
     StringSerializer.Init(mode);
 }
        /// <summary>
        /// Default  constructor
        /// </summary>
        /// <param name="rawData">serialized packet</param>
        /// <param name="offset">rawData offset</param>
        /// <param name="count">rawData byte size</param>
        /// <param name="serializerMode">serializer mode</param>
        public PacketSerializer(byte[] rawData, int offset, int count, SerializerMode serializerMode = SerializerMode.SILVERLIGHT_SERIALIZER)
        {
            m_formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            Mode = serializerMode;
            switch (Mode)
            {
            case SerializerMode.ALLOW_ALL_ASSEMBLY_VERSION_DESERIALIZATION:
                m_formatter.Binder = new AllowAllAssemblyVersionDeserializationBinder();
                break;

            case SerializerMode.DEFAULT:
            case SerializerMode.SILVERLIGHT_SERIALIZER:
                break;
            }
            m_stream = new MemoryStream(rawData, offset, count);
        }
        /// <summary>
        /// Default copy constructor
        /// </summary>
        /// <param name="orig">the object to copy from</param>
        public PacketSerializer(PacketSerializer <PacketStruct> orig)
        {
            m_formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
            Mode = orig.Mode;
            switch (Mode)
            {
            case SerializerMode.ALLOW_ALL_ASSEMBLY_VERSION_DESERIALIZATION:
                m_formatter.Binder = new AllowAllAssemblyVersionDeserializationBinder();
                break;

            case SerializerMode.DEFAULT:
            case SerializerMode.SILVERLIGHT_SERIALIZER:
                break;
            }
            m_stream = orig.m_stream;
        }
        private void ProgressString(SerializerMode mode, ref string str, ref object?obj)
        {
            switch (mode)
            {
            case SerializerMode.Deserialize:
                obj = _converter?.ConvertBack(str);
                break;

            case SerializerMode.Serialize:
                str = _converter?.Convert(obj) ?? string.Empty;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode));
            }
        }
Ejemplo n.º 17
0
        public override void Progress(object target, TContext context, SerializerMode mode)
        {
            switch (mode)
            {
            case SerializerMode.Deserialize:
                Deserialize(target, context);
                break;

            case SerializerMode.Serialize:
                Serialize(target, context);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode));
            }
        }
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="packet">packet class object</param>
        /// <param name="serializerMode">serializer mode</param>
        public PacketSerializer(PacketStruct packet = null, SerializerMode serializerMode = SerializerMode.SILVERLIGHT_SERIALIZER)
        {
            m_formatter.AssemblyFormat = FormatterAssemblyStyle.Simple;

            m_stream = new MemoryStream();
            Mode     = serializerMode;
            switch (Mode)
            {
            case SerializerMode.DEFAULT:
            case SerializerMode.ALLOW_ALL_ASSEMBLY_VERSION_DESERIALIZATION:
                m_formatter.Serialize(m_stream, packet);
                break;

            case SerializerMode.SILVERLIGHT_SERIALIZER:
                SilverlightSerializer.Serialize(packet, m_stream);
                break;
            }
        }
Ejemplo n.º 19
0
        public static DeserializersTable GetTable(SerializerMode mode)
        {
            switch (mode)
            {
            case SerializerMode.CachedUShort:
                return(CachedUShortTablePool.New());

            case SerializerMode.Cached2048:
                return(Cached2048TablePool.New());

            case SerializerMode.Cached1024:
                return(Cached1024TablePool.New());

            case SerializerMode.Cached512:
                return(Cached512TablePool.New());

            default:
                return(NoCachedTablePool.New());
            }
        }
Ejemplo n.º 20
0
        public static string Serialize(this object @this, SerializerMode mode = SerializerMode.Default)
        {
            if (mode == SerializerMode.Default)
            {
                mode = DefaultMode;
            }
            switch (mode)
            {
            case SerializerMode.Default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);

            case SerializerMode.Xml:
                return(XmlSerialize(@this));

            case SerializerMode.Json:
                return(JsonSerialize(@this));

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
        }
Ejemplo n.º 21
0
        public static T Deserialize <T>(this string objectString, SerializerMode mode = SerializerMode.Default, Action <string> onError = null)
        {
            if (mode == SerializerMode.Default)
            {
                mode = DefaultMode;
            }
            switch (mode)
            {
            case SerializerMode.Default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);

            case SerializerMode.Xml:
                return(XmlDeserialize <T>(objectString, onError));

            case SerializerMode.Json:
                return(JsonDeserialize <T>(objectString, onError));

            default:
                throw new ArgumentOutOfRangeException(nameof(mode), mode, null);
            }
        }
Ejemplo n.º 22
0
 public override void Init(SerializerMode mode)
 {
     _mode = mode;
     if (_decimalCache != null)
     {
         _decimalCache = new SerializerCache <decimal>(mode);
     }
     if (_doubleCache != null)
     {
         _doubleCache = new SerializerCache <double>(mode);
     }
     if (_floatCache != null)
     {
         _floatCache = new SerializerCache <float>(mode);
     }
     if (_longCache != null)
     {
         _longCache = new SerializerCache <long>(mode);
     }
     if (_uLongCache != null)
     {
         _uLongCache = new SerializerCache <ulong>(mode);
     }
     if (_intCache != null)
     {
         _intCache = new SerializerCache <int>(mode);
     }
     if (_uIntCache != null)
     {
         _uIntCache = new SerializerCache <uint>(mode);
     }
     if (_shortCache != null)
     {
         _shortCache = new SerializerCache <short>(mode);
     }
     if (_uShortCache != null)
     {
         _uShortCache = new SerializerCache <ushort>(mode);
     }
 }
        private void ProgressElement(SerializerMode mode, XObject?xobj, ref object?obj)
        {
            if (xobj == null)
            {
                return;
            }

            var attr = xobj as XAttribute;

            var str = attr != null ? attr.Value : ((XElement)xobj).Value;

            switch (mode)
            {
            case SerializerMode.Deserialize:
                ProgressString(mode, ref str, ref obj);
                break;

            case SerializerMode.Serialize:
                ProgressString(mode, ref str, ref obj);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(mode));
            }

            if (mode != SerializerMode.Serialize)
            {
                return;
            }

            if (attr != null)
            {
                attr.Value = str;
            }
            else
            {
                ((XElement)xobj).Value = str;
            }
        }
Ejemplo n.º 24
0
        static int WithSerializer(SerializerMode mode, MemoryStream stream, Action <ISerializer> func, ISerializer parentSerializer, ref int fakeOffset)
        {
            switch (mode)
            {
            case SerializerMode.Writing:
            {
                using var bw = new BinaryWriter(stream, Encoding.GetEncoding(850), true);
                var s = new AlbionWriter(bw);
                func(s);
                return((int)s.Offset);
            }

            case SerializerMode.WritingAnnotated:
            {
                using var tw = new StreamWriter(stream, Encoding.GetEncoding(850), 1024, true);
                using var s  = new AnnotatedFormatWriter(tw, (AnnotatedFormatWriter)parentSerializer);
                s.Seek(fakeOffset);
                func(s);
                int length = (int)s.Offset - fakeOffset;
                fakeOffset = (int)s.Offset;
                return(length);
            }

            case SerializerMode.WritingJson:
            {
                using var tw = new StreamWriter(stream, Encoding.GetEncoding(850), 1024, true);
                using var s  = new JsonWriter(tw, (JsonWriter)parentSerializer);
                s.Seek(fakeOffset);
                func(s);
                int length = (int)s.Offset - fakeOffset;
                fakeOffset = (int)s.Offset;
                return(length);
            }

            default: throw new InvalidOperationException();
            }
        }
Ejemplo n.º 25
0
        public void Clear(SerializerMode mode)
        {
            switch (mode)
            {
            case SerializerMode.Cached512:
                _maxIndex = 511;
                break;

            case SerializerMode.Cached1024:
                _maxIndex = 1023;
                break;

            case SerializerMode.Cached2048:
                _maxIndex = 2047;
                break;

            case SerializerMode.CachedUShort:
                _maxIndex = 65534;
                break;
            }
            _currentIndex = 0;
            _serializationCache.Clear();
            _deserializationCache.Clear();
        }
Ejemplo n.º 26
0
        public SerializerCache(SerializerMode mode, IEqualityComparer <T> sercomparer = null, IEqualityComparer <int> descomparer = null)
        {
            _serializationCache   = new Dictionary <T, int>(sercomparer);
            _deserializationCache = new Dictionary <int, T>(descomparer);
            switch (mode)
            {
            case SerializerMode.Cached512:
                _maxIndex = 511;
                break;

            case SerializerMode.Cached1024:
                _maxIndex = 1023;
                break;

            case SerializerMode.Cached2048:
                _maxIndex = 2047;
                break;

            case SerializerMode.CachedUShort:
                _maxIndex = 65534;
                break;
            }
            _currentIndex = 0;
        }
Ejemplo n.º 27
0
        protected override SerializationContext GetRealContext(XmlElementContext origial, SerializerMode mode)
        {
            if (!(XmlElementSerializer.GetElement(origial.XElement, mode == SerializerMode.Serialize, _target) is XElement ele))
            {
                _useSnapShot = false;
                return(origial.Original);
            }

            _useSnapShot = true;
            return(origial.Original.CreateSnapshot(ele.Value));
        }
 public void Initialize(object target, SerializerMode mode)
 {
     _target = Argument.NotNull(target, nameof(target));
     _mode   = mode;
 }
        private void ProgressElement(SerializerMode mode, [CanBeNull] XObject xobj, [NotNull] ref object obj)
        {
            if (xobj == null) return;

            var attr = xobj as XAttribute;

            string str = attr != null ? attr.Value : ((XElement) xobj).Value;

            switch (mode)
            {
                case SerializerMode.Deserialize:
                    ProgressString(mode, ref str, ref obj);
                    break;
                case SerializerMode.Serialize:
                    ProgressString(mode, ref str, ref obj);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("mode");
            }

            if (mode != SerializerMode.Serialize) return;

            if (attr != null) attr.Value = str;
            else ((XElement) xobj).Value = str;
        }
Ejemplo n.º 30
0
        private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
        {
            var serializer = new BinarySerializer(stream, mode, Text.Encoding.ASCII);

            return(serializer);
        }
 private void ProgressString(SerializerMode mode, [NotNull] ref string str, [CanBeNull] ref object obj)
 {
     switch (mode)
     {
         case SerializerMode.Deserialize:
             obj = _converter.ConvertBack(str);
             break;
         case SerializerMode.Serialize:
             str = _converter.Convert(obj);
             break;
         default:
             throw new ArgumentOutOfRangeException("mode");
     }
 }
Ejemplo n.º 32
0
 private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
 {
     var serializer = new BinarySerializer(stream, mode, Text.Encoding.ASCII);
     return serializer;
 }
Ejemplo n.º 33
0
 public abstract void Init(SerializerMode mode);
Ejemplo n.º 34
0
 public override void Init(SerializerMode mode)
 {
 }
        private void Progress(object graph, IStreamSource target, SerializerMode mode)
        {
            var context = BuildContext(new SerializationContext(_contextMode, target, mode));

            Progress(graph, context, mode);
        }
Ejemplo n.º 36
0
 private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
 {
     var serializer = new BinarySerializer(stream, mode, Text.Encoding.ASCII);
     serializer.RegisterDynamic<Vector4>();
     serializer.RegisterDynamic<Vector3>();
     serializer.RegisterDynamic<Vector2>();
     return serializer;
 }
Ejemplo n.º 37
0
 private static BinarySerializer GetSerializer(Stream stream, SerializerMode mode)
 {
     var serializer = new BinarySerializer(stream, mode, Text.Encoding.ASCII);
     serializer.ArrayLengthType = ArrayLengthType.Int;
     serializer.RegisterDynamicList<MaterialTexture>("MATL");
     return serializer;
 }