Exemplo n.º 1
0
        public static byte[] SingleValue(double x, double y, double z)
        {
            var buffer = new FlexBuffer(28);
            var start  = buffer.StartVector();

            buffer.Add(x);
            buffer.Add(y);
            buffer.Add(z);
            buffer.EndVector(start, true, true);
            return(buffer.Finish());
        }
Exemplo n.º 2
0
        public static byte[] SingleValue(ulong x, ulong y, ulong z, ulong w)
        {
            var buffer = new FlexBuffer(36);
            var start  = buffer.StartVector();

            buffer.Add(x);
            buffer.Add(y);
            buffer.Add(z);
            buffer.Add(w);
            buffer.EndVector(start, true, true);
            return(buffer.Finish());
        }
        public static byte[] Convert(string xmlData)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(new StringReader(xmlData));

            var flx = new FlexBuffer();

            Process(flx, doc.DocumentElement);

            return(flx.Finish());
        }
Exemplo n.º 4
0
        public static byte[] From(IEnumerable value, Options options = Options.ShareKeys | Options.ShareStrings | Options.ShareKeyVectors)
        {
            var buffer = new FlexBuffer(options: options);

            if (value is IDictionary dictionary)
            {
                buffer.AddDynamicMap(dictionary);
            }
            else
            {
                buffer.AddDynamicVector(value);
            }

            return(buffer.Finish());
        }
        private void ReadObject(FlexBuffer flx)
        {
            _scanner.Assert('{');

            _scanner.SkipWhitespace();

            var start = flx.StartVector();

            if (_scanner.Peek() == '}')
            {
                _scanner.Read();
            }
            else
            {
                while (true)
                {
                    _scanner.SkipWhitespace();

                    ReadString(flx, true);

                    _scanner.SkipWhitespace();

                    _scanner.Assert(':');

                    _scanner.SkipWhitespace();

                    ReadJsonValue(flx);

                    _scanner.SkipWhitespace();

                    var next = _scanner.Read();

                    if (next == '}')
                    {
                        break;
                    }

                    if (next == ',')
                    {
                        continue;
                    }
                    throw new Exception($"Unexpected character {next} at position {_scanner.Position}");
                }
            }

            flx.SortAndEndMap(start);
        }
        private void ReadBoolean(FlexBuffer flx)
        {
            switch (_scanner.Peek())
            {
            case 't':
                _scanner.Assert("true");
                flx.Add(true);
                return;

            case 'f':
                _scanner.Assert("false");
                flx.Add(false);
                return;

            default:
                throw new Exception($"Unexpected character {_scanner.Position}");
            }
        }
        private static void Process(FlexBuffer flx, XmlNode element)
        {
            var node = flx.StartVector();

            flx.AddKey("tagName");
            flx.Add(element.Name);
            var attributes = element.Attributes;

            if (attributes != null)
            {
                for (var i = 0; i < attributes.Count; i++)
                {
                    var att = attributes.Item(i);
                    flx.AddKey(att.Name);
                    flx.Add(att.Value);
                }
            }

            var children = element.ChildNodes;

            if (children.Count > 0)
            {
                flx.AddKey("children");
                var childVector = flx.StartVector();
                for (var i = 0; i < children.Count; i++)
                {
                    var child = children[i];
                    if (child.NodeType == XmlNodeType.Text || child.NodeType == XmlNodeType.CDATA)
                    {
                        flx.Add(child.Value);
                    }
                    else if (child.NodeType == XmlNodeType.Comment)
                    {
                    }
                    else
                    {
                        Process(flx, child);
                    }
                }

                flx.EndVector(childVector, false, false);
            }
            flx.SortAndEndMap(node);
        }
        private void ReadJsonValue(FlexBuffer flx)
        {
            _scanner.SkipWhitespace();

            var next = _scanner.Peek();

            if (char.IsNumber(next))
            {
                ReadNumber(flx);
                return;
            }

            switch (next)
            {
            case '{':
                ReadObject(flx);
                return;

            case '[':
                ReadArray(flx);
                return;

            case '"':
                ReadString(flx);
                return;

            case '-':
                ReadNumber(flx);
                return;

            case 't':
            case 'f':
                ReadBoolean(flx);
                return;

            case 'n':
                ReadNull(flx);
                return;

            default:
                throw new Exception($"Unexpected character {_scanner.Position}");
            }
        }
        private void ReadArray(FlexBuffer flx)
        {
            _scanner.Assert('[');

            var start = flx.StartVector();

            _scanner.SkipWhitespace();

            if (_scanner.Peek() == ']')
            {
                _scanner.Read();
            }
            else
            {
                while (true)
                {
                    ReadJsonValue(flx);

                    _scanner.SkipWhitespace();

                    var next = _scanner.Read();

                    if (next == ']')
                    {
                        break;
                    }

                    if (next == ',')
                    {
                        continue;
                    }

                    throw new Exception($"Unexpected character {next} at position {_scanner.Position}");
                }
            }

            flx.EndVector(start, false, false);
        }
        private void ReadString(FlexBuffer flx, bool asKey = false)
        {
            var builder = new StringBuilder();

            _scanner.Assert('"');

            while (true)
            {
                var c = _scanner.Read();

                if (c == '\\')
                {
                    c = _scanner.Read();

                    switch (char.ToLower(c))
                    {
                    case '"':                              // "
                    case '\\':                             // \
                    case '/':                              // /
                        builder.Append(c);
                        break;

                    case 'b':
                        builder.Append('\b');
                        break;

                    case 'f':
                        builder.Append('\f');
                        break;

                    case 'n':
                        builder.Append('\n');
                        break;

                    case 'r':
                        builder.Append('\r');
                        break;

                    case 't':
                        builder.Append('\t');
                        break;

                    case 'u':
                        builder.Append(ReadUnicodeLiteral());
                        break;

                    default:
                        throw new Exception($"Unexpected character {_scanner.Position}");
                    }
                }
                else if (c == '"')
                {
                    break;
                }
                else
                {
                    if (char.IsControl(c))
                    {
                        throw new Exception($"Unexpected character {_scanner.Position}");
                    }
                    else
                    {
                        builder.Append(c);
                    }
                }
            }

            if (asKey)
            {
                flx.AddKey(builder.ToString());
            }
            else
            {
                flx.Add(builder.ToString());
            }
        }
 private void ReadNull(FlexBuffer flx)
 {
     _scanner.Assert("null");
     flx.AddNull();
 }
        public static byte[] Convert(string csv, char separator = ',')
        {
            var flx              = new FlexBuffer();
            var sb               = new StringBuilder();
            var outerVec         = flx.StartVector();
            var innerVec         = -1;
            var isInDoubleQuotes = false;

            for (var offset = 0; offset < csv.Length;)
            {
                if (innerVec == -1)
                {
                    innerVec = flx.StartVector();
                }

                if (csv[offset] == '"')
                {
                    if (isInDoubleQuotes == false)
                    {
                        isInDoubleQuotes = true;
                        offset++;
                    }
                    else
                    {
                        if (csv.Length > offset + 1 && csv[offset + 1] == '"')
                        {
                            sb.Append('"');
                            offset += 2;
                        }
                        else
                        {
                            isInDoubleQuotes = false;
                            offset++;
                        }
                    }
                }
                else if (csv[offset] == separator && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    sb.Clear();
                    offset++;
                }
                else if (csv[offset] == '\n' && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    flx.EndVector(innerVec, false, false);
                    innerVec = -1;
                    sb.Clear();
                    offset++;
                }
                else if (csv[offset] == '\r' && csv.Length > offset + 1 && csv[offset + 1] == '\n' && isInDoubleQuotes == false)
                {
                    flx.Add(sb.ToString());
                    flx.EndVector(innerVec, false, false);
                    innerVec = -1;
                    sb.Clear();
                    offset += 2;
                }
                else
                {
                    sb.Append(csv[offset]);
                    offset++;
                }
            }

            if (innerVec != -1)
            {
                flx.Add(sb.ToString());
                flx.EndVector(innerVec, false, false);
            }
            flx.EndVector(outerVec, false, false);
            return(flx.Finish());
        }
Exemplo n.º 13
0
 internal FlexBufferMapBuilder(FlexBuffer buffer)
 {
     _buffer = buffer;
 }
Exemplo n.º 14
0
 internal FlexBufferVectorBuilder(FlexBuffer buffer)
 {
     _buffer = buffer;
 }