Exemplo n.º 1
0
        public static string CreateText(object value, SerializationSettings settings)
        {
            if (value == null)
            {
                return("null");
            }

            if (value is bool)
            {
                return(((bool)value) ? "1" : "0");
            }

            if (value is Enum)
            {
                return(((int)value).ToString());
            }

            if (value is DateTime)
            {
                return(((DateTime)value).ToString(settings.DateTimeFormat));
            }

            var formatter = value.GetType().GetMethod("ToString", new[] { typeof(IFormatProvider) });

            if (formatter != null)
            {
                return((string)formatter.Invoke(value, new[] { settings.CultureInfo }));
            }

            return(value.ToString());
        }
Exemplo n.º 2
0
 public TransformWriter(Writer writer, ITransform transform, SerializationSettings settings)
     : base(settings)
 {
     this.writer             = writer;
     this.transform          = transform;
     this.transform.Settings = this.Settings;
 }
Exemplo n.º 3
0
 public TransformWriter(Writer writer, ITransform transform, SerializationSettings settings)
     : base(settings, TextCase.KeepOriginal)
 {
     this.writer             = writer;
     this.transform          = transform;
     this.transform.Settings = this.Settings;
 }
Exemplo n.º 4
0
        public SerializationSettings(SerializationSettings settings)
        {
            this.instances = new Dictionary <Type, object>();
            this.TextCase  = TextCase.Default;

            this.PropertyResolver = settings?.PropertyResolver;
        }
Exemplo n.º 5
0
 public TransformReader(Reader reader, ITransform transform, SerializationSettings settings)
     : base(settings ?? new SerializationSettings())
 {
     this.reader             = reader;
     this.nodes              = EnumerateNodes().GetEnumerator();
     this.transform          = transform;
     this.transform.Settings = this.Settings;
 }
Exemplo n.º 6
0
 public Writer(SerializationSettings settings, TextCase defaultTextCase)
 {
     this.forward  = DoWrite;
     this.Settings = settings ?? new SerializationSettings();
     if (this.Settings.TextCase == TextCase.Default)
     {
         this.Settings.TextCase = defaultTextCase;
     }
 }
Exemplo n.º 7
0
        public SerializationSettings CreateScope()
        {
            var scopedPropertyResolver = new ScopedPropertyResolver(PropertyResolver);
            var scope = new SerializationSettings {
                PropertyResolver = scopedPropertyResolver
            };

            return(scope);
        }
Exemplo n.º 8
0
        public static string CreateName(object text, SerializationSettings settings, TextCase defaultCase)
        {
            if (text == null)
            {
                return(null);
            }

            return(CreateName(text.ToString(), settings, defaultCase));
        }
Exemplo n.º 9
0
        public SerializationSettings Clone()
        {
            var clonedProperties = new Dictionary <string, object>();

            foreach (var property in PropertyResolver.GetProperties())
            {
                clonedProperties[property] = PropertyResolver.Get(property);
            }
            var clonedPropertyResolver = new DictionaryPropertyResolver(clonedProperties);
            var clone = new SerializationSettings {
                PropertyResolver = clonedPropertyResolver
            };

            return(clone);
        }
Exemplo n.º 10
0
        public static string CreateName(string text, SerializationSettings settings, TextCase defaultCase)
        {
            if (text == null)
            {
                return(null);
            }

            if (text.StartsWith("\"") || text.StartsWith("'"))
            {
                text = text.Substring(1, text.Length - 2);
            }

            var textCase = (settings.TextCase == TextCase.Default) ? defaultCase : settings.TextCase;

            return(text.ChangeCase(textCase));
        }
Exemplo n.º 11
0
        public static Reader CreateReader(TextReader reader, SerializationSettings settings)
        {
            var documentReader = SupportedDocumentTextReader.Create(reader);

            if (documentReader.DocumentFormat == SupportedDocumentTextReader.XmlFormat)
            {
                return(new XmlDocumentReader(documentReader, settings));
            }

            if (documentReader.DocumentFormat == SupportedDocumentTextReader.JsonFormat)
            {
                return(new JsonReader(documentReader, settings));
            }

            return(new CsvReader(documentReader, settings));
        }
Exemplo n.º 12
0
        public static Reader CreateReader(TextReader reader, SerializationSettings settings)
        {
            var documentReader = FormattedTextReader.CreateReader(reader);

            switch (documentReader.DocumentFormat)
            {
            case FormattedTextReader.XmlFormat:
                return(new XmlDocumentReader(documentReader, settings));

            case FormattedTextReader.JsonFormat:
                return(new JsonReader(documentReader, settings));

            case FormattedTextReader.CsvFormat:
                return(new CsvReader(documentReader, settings));

            default:
                throw new HttpException(HttpStatusCode.NotAcceptable);
            }
        }
Exemplo n.º 13
0
        public static string CreateText(object value, SerializationSettings settings)
        {
            if (value == null)
            {
                return("null");
            }

            if (value is bool)
            {
                return(((bool)value) ? "1" : "0");
            }

            if (value is Enum)
            {
                return(((int)value).ToString());
            }

            return(Change.To <string>(value));
        }
Exemplo n.º 14
0
        public static string CreateQuotedText(object value, SerializationSettings settings, Func <string, string> escapeMethod = null)
        {
            if (value == null)
            {
                return("null");
            }

            if (escapeMethod == null)
            {
                escapeMethod = EscapeLiterals;
            }

            var text = CreateText(value, settings);

            bool isNumeric = false;

            // Quando um número extrapola o limite da capacidade deve
            // então ser tratado como texto.
            // Mas como determinar se o número extrapola o limite?
            // De qualquer forma qualquer manutenção futura deve
            // considerar mais de 15 caracteres como texto, para evitar
            // problemas de compatibilidade com o uso atual da API.
            //
            // Mar/2017
            // Guga Coder
            if (text.Length <= 15)
            {
                isNumeric = Regex.IsMatch(text, @"^\d+([.]\d+)?$", RegexOptions.CultureInvariant);
            }
            if (!isNumeric)
            {
                text = '"' + escapeMethod.Invoke(text) + '"';
            }

            return(text);
        }
Exemplo n.º 15
0
 public DocumentReader(DocumentModel document, SerializationSettings settings)
     : this((NodeModel)document, settings)
 {
     // nada a fazer aqui. use o outro construtor.
 }
Exemplo n.º 16
0
 public DocumentWriter(SerializationSettings settings)
     : base(settings ?? new SerializationSettings())
 {
     this.stack = new Stack <NodeModel>();
     this.cache = new Stack <Stack <NodeModel> >();
 }
Exemplo n.º 17
0
 public DocumentReader(NodeModel document, SerializationSettings settings)
     : base(settings)
 {
     this.enumerator = EnumerateNodes(document).GetEnumerator();
 }
Exemplo n.º 18
0
        public static Reader CreateReader(string filename, SerializationSettings settings)
        {
            var reader = new StreamReader(filename);

            return(CreateReader(reader, settings));
        }
Exemplo n.º 19
0
        public static object CreateValue(string text, SerializationSettings settings, Func <string, string> unescapeMethod = null)
        {
            if ((text == null) || (text == "null"))
            {
                return(null);
            }

            if (unescapeMethod == null)
            {
                unescapeMethod = UnescapeLiterals;
            }

            try
            {
                text = unescapeMethod.Invoke(text);

                var isNumeric =
                    !text.StartsWith("\"") &&
                    Regex.IsMatch(text, @"^\d+([.]\d+)?$", RegexOptions.CultureInvariant);

                if (isNumeric)
                {
                    if (text.Contains('.'))
                    {
                        return(double.Parse(text, settings.CultureInfo));
                    }
                    else
                    {
                        var value = long.Parse(text);
                        if (value >= int.MinValue && value <= int.MaxValue)
                        {
                            return((int)value);
                        }
                        return(value);
                    }
                }
                else
                {
                    if (text.StartsWith("\""))
                    {
                        text = text.Substring(1, text.Length - 2);
                    }

                    var format    = settings.DateTimeFormat;
                    var culture   = settings.CultureInfo;
                    var dateStyle = DateTimeStyles.None;
                    var dateTime  = DateTime.MinValue;
                    var ok        = DateTime.TryParseExact(text, format, culture, dateStyle, out dateTime);
                    if (ok)
                    {
                        return(dateTime);
                    }

                    return(text);
                }
            }
            catch
            {
                return(text);
            }
        }
Exemplo n.º 20
0
 public Reader(SerializationSettings settings)
 {
     this.Settings   = settings ?? new SerializationSettings();
     this.readMethod = DoRead;
 }
Exemplo n.º 21
0
 public Writer(SerializationSettings settings)
 {
     this.Settings = settings ?? new SerializationSettings();
     this.forward  = DoWrite;
 }