Ejemplo n.º 1
0
        public void Null()
        {
            var buffer = JsonToFlexBufferConverter.Convert("null");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(true, flx.IsNull);
        }
Ejemplo n.º 2
0
        public void EmptyMap()
        {
            var buffer = JsonToFlexBufferConverter.Convert("{}");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(0, flx.AsMap.Length);
        }
Ejemplo n.º 3
0
        public void Int()
        {
            var buffer = JsonToFlexBufferConverter.Convert("3456");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(3456, flx.AsLong);
        }
Ejemplo n.º 4
0
        public void EmptyVector()
        {
            var buffer = JsonToFlexBufferConverter.Convert("[]");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(0, flx.AsVector.Length);
        }
Ejemplo n.º 5
0
        public void NegativeFloat()
        {
            var buffer = JsonToFlexBufferConverter.Convert("-34.56");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(-34.56, flx.AsDouble);
        }
Ejemplo n.º 6
0
        public void VectorOfMaps()
        {
            const string json  = @"
[
    {""name"": ""Max"", ""age"": 38},
    {""name"": ""Maxim"", ""age"": 35},
    {""age"": 18, ""name"": ""Alex""}
]
";
            var          bytes = JsonToFlexBufferConverter.Convert(json);
            var          flx   = FlxValue.FromBytes(bytes);

            Assert.AreEqual(3, flx.AsVector.Length);

            Assert.AreEqual(2, flx[0].AsMap.Length);
            Assert.AreEqual("Max", flx[0]["name"].AsString);
            Assert.AreEqual(38, flx[0]["age"].AsLong);

            Assert.AreEqual(2, flx[1].AsMap.Length);
            Assert.AreEqual("Maxim", flx[1]["name"].AsString);
            Assert.AreEqual(35, flx[1]["age"].AsLong);

            Assert.AreEqual(2, flx[2].AsMap.Length);
            Assert.AreEqual("Alex", flx[2]["name"].AsString);
            Assert.AreEqual(18, flx[2]["age"].AsLong);
        }
Ejemplo n.º 7
0
    static void ImportJson()
    {
        string jsonPath = EditorUtility.OpenFilePanel("Select JSON file", "", "json");

        if (jsonPath.Length == 0)
        {
            return;
        }
        var bytes = JsonToFlexBufferConverter.ConvertFile(jsonPath);

        if (bytes == null)
        {
            return;
        }

        var fileName = Path.GetFileNameWithoutExtension(jsonPath);

        var flxPath = EditorUtility.SaveFilePanel(
            "Save as FlexBuffer",
            "",
            fileName + ".bytes",
            "bytes");

        if (flxPath.Length != 0)
        {
            File.WriteAllBytes(flxPath, bytes);
        }
    }
Ejemplo n.º 8
0
        public void OneKeyMap()
        {
            var buffer = JsonToFlexBufferConverter.Convert("{\"\":1}");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(1, flx.AsMap.Length);
            Assert.AreEqual(1, flx[""].AsLong);
        }
        public void TestOffsetAndLengthAreOfTypeULong()
        {
            var json          = @"{""channels_in"":64,""dilation_height_factor"":1,""dilation_width_factor"":1,""fused_activation_function"":1,""pad_values"":1,""padding"":0,""stride_height"":1,""stride_width"":1}";
            var bytes         = JsonToFlexBufferConverter.Convert(json);
            var expectedBytes = new byte[] { 99, 104, 97, 110, 110, 101, 108, 115, 95, 105, 110, 0, 100, 105, 108, 97, 116, 105, 111, 110, 95, 104, 101, 105, 103, 104, 116, 95, 102, 97, 99, 116, 111, 114, 0, 100, 105, 108, 97, 116, 105, 111, 110, 95, 119, 105, 100, 116, 104, 95, 102, 97, 99, 116, 111, 114, 0, 102, 117, 115, 101, 100, 95, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 95, 102, 117, 110, 99, 116, 105, 111, 110, 0, 112, 97, 100, 95, 118, 97, 108, 117, 101, 115, 0, 112, 97, 100, 100, 105, 110, 103, 0, 115, 116, 114, 105, 100, 101, 95, 104, 101, 105, 103, 104, 116, 0, 115, 116, 114, 105, 100, 101, 95, 119, 105, 100, 116, 104, 0, 8, 130, 119, 97, 76, 51, 41, 34, 21, 8, 1, 8, 64, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 16, 36, 1 };

            Assert.AreEqual(expectedBytes, bytes);
        }
Ejemplo n.º 10
0
        public void TwoKeysMap()
        {
            var buffer = JsonToFlexBufferConverter.Convert("{\"a\":1, \"b\":2}");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(2, flx.AsMap.Length);
            Assert.AreEqual(1, flx["a"].AsLong);
            Assert.AreEqual(2, flx["b"].AsLong);
        }
Ejemplo n.º 11
0
        public void MixedVector()
        {
            var buffer = JsonToFlexBufferConverter.Convert("[null, true, false, \"hello 🙀\", -34, 6.1]");
            var flx    = FlxValue.FromBytes(buffer);

            Assert.AreEqual(6, flx.AsVector.Length);
            Assert.AreEqual(true, flx[0].IsNull);
            Assert.AreEqual(true, flx[1].AsBool);
            Assert.AreEqual(false, flx[2].AsBool);
            Assert.AreEqual("hello 🙀", flx[3].AsString);
            Assert.AreEqual(-34, flx[4].AsLong);
            Assert.AreEqual(6.1, flx[5].AsDouble);
        }
Ejemplo n.º 12
0
        public void ComplexMap()
        {
            const string json  = @"
{
    ""age"": 35,
    ""weight"": 72.5,
    ""name"": ""Maxim"",
    ""flags"": [true, false, true, true],
    ""something"": null,
    ""address"": {
        ""city"": ""Bla"",
        ""zip"": ""12345"",
        ""countryCode"": ""XX""
    }
}
";
            var          bytes = JsonToFlexBufferConverter.Convert(json);
            var          flx   = FlxValue.FromBytes(bytes);

            Assert.AreEqual(6, flx.AsMap.Length);

            Assert.AreEqual(35, flx["age"].AsLong);
            Assert.AreEqual(72.5, flx["weight"].AsDouble);
            Assert.AreEqual("Maxim", flx["name"].AsString);
            Assert.AreEqual(true, flx["something"].IsNull);

            Assert.AreEqual(4, flx["flags"].AsVector.Length);
            Assert.AreEqual(true, flx["flags"][0].AsBool);
            Assert.AreEqual(false, flx["flags"][1].AsBool);
            Assert.AreEqual(true, flx["flags"][2].AsBool);
            Assert.AreEqual(true, flx["flags"][3].AsBool);

            Assert.AreEqual(3, flx["address"].AsMap.Length);
            Assert.AreEqual("Bla", flx["address"]["city"].AsString);
            Assert.AreEqual("12345", flx["address"]["zip"].AsString);
            Assert.AreEqual("XX", flx["address"]["countryCode"].AsString);
        }
Ejemplo n.º 13
0
    void OnGUI()
    {
        if (GUILayout.Button("Open FlexBuffer file..."))
        {
            var jsonPath = EditorUtility.OpenFilePanel("Select FlexBuffer file", "", "bytes");
            if (jsonPath.Length == 0)
            {
                return;
            }

            _path = jsonPath;

            _query = "";

            var bytes = File.ReadAllBytes(_path);

            _fileSize = bytes.Length;

            var root = FlxValue.FromBytes(bytes);
            _treeView      = new FlexBufferTreeView(root, _treeViewState);
            _treeViewState = new TreeViewState();
        }

        if (_path.Length == 0)
        {
            return;
        }

        var jsonFileString = _jsonPath.Length > 0 ? $"| {_jsonPath} [{_jsonFileSize}]" : "";

        GUILayout.Label($"{_path} [{_fileSize}] {jsonFileString}");

        if (_jsonPath.Length > 0)
        {
            GUILayout.BeginHorizontal();
        }

        if (GUILayout.Button("Export as JSON..."))
        {
            var jsonPath = EditorUtility.SaveFilePanel(
                "Save as JSON",
                "",
                $"{Path.GetFileNameWithoutExtension(_path)}.json",
                "json");

            if (jsonPath.Length != 0)
            {
                var prettyJson = FlexBuffersPreferences.PrettyPrintedJson ? _treeView._rootValue.ToPrettyJson() : _treeView._rootValue.ToJson;
                _jsonFileSize = prettyJson.Length;
                File.WriteAllText(jsonPath, prettyJson);
            }

            _jsonPath = jsonPath;
        }

        if (_jsonPath.Length > 0)
        {
            if (GUILayout.Button("Open JSON"))
            {
                Application.OpenURL($"file://{_jsonPath}");
            }

            if (GUILayout.Button("Import from JSON"))
            {
                var bytes = JsonToFlexBufferConverter.ConvertFile(_jsonPath);

                if (bytes != null)
                {
                    File.WriteAllBytes(_path, bytes);
                }

                var root = FlxValue.FromBytes(bytes);
                _treeView      = new FlexBufferTreeView(root, _treeViewState);
                _treeViewState = new TreeViewState();
            }
            GUILayout.EndHorizontal();
        }

        var newQuery = GUILayout.TextField(_query);

        if (newQuery != _query)
        {
            var query = FlxQueryParser.Convert(newQuery);
            _query = newQuery;
            _treeView?.SetQuery(query);
        }

        _treeView?.OnGUI(new Rect(0, 80, position.width, position.height - 80));


        if (Event.current.type == EventType.KeyUp && (Event.current.modifiers == EventModifiers.Control ||
                                                      Event.current.modifiers == EventModifiers.Command))
        {
            if (Event.current.keyCode == KeyCode.C)
            {
                Event.current.Use();
                var selection = _treeView?.GetSelection();
                if (selection != null && selection.Count > 0)
                {
                    if (_treeView.GetRows().First(item => item.id == selection[0]) is FlxValueTreeViewItem row)
                    {
                        GUIUtility.systemCopyBuffer = row.FlxValue.ToJson;
                    }
                }
            }
        }
    }