Exemplo n.º 1
0
        /// <summary>
        /// DeserializeFiles: DeserializeFiles json file to C# objects
        /// </summary>
        private void DeserializeFiles()
        {
            IInputDataReader jsonDataReader = new JsonDataReader();

            inputData     = jsonDataReader.DeserializeInputData(InputFile);
            changeSetData = jsonDataReader.DeserializeChangeSet(ChangeFile);
        }
Exemplo n.º 2
0
        public void Read_ShouldReturnSuccessfulResultWithExpectedData()
        {
            var path         = TestUtils.BuildPathFor(@"DataReadersTestCases\JsonDataReaderTestCase1.json");
            var expectedData = new[]
            {
                new Dictionary <string, double> {
                    { "weight", 150.05 }, { "speed", 220.15 }, { "tyre-consumption", 0.9845 }
                },
                new Dictionary <string, double> {
                    { "weight", 148.57 }, { "speed", 221.99 }, { "tyre-consumption", 0.9212 }
                },
                new Dictionary <string, double> {
                    { "weight", 146.98 }, { "speed", 225.05 }, { "tyre-consumption", 0.8601 }
                },
                new Dictionary <string, double> {
                    { "weight", 144.00 }, { "speed", 228.45 }, { "tyre-consumption", 0.7915 }
                },
                new Dictionary <string, double> {
                    { "weight", 142.00 }, { "speed", 229.98 }, { "tyre-consumption", 0.7000 }
                }
            };
            var reader = new JsonDataReader();

            var result = reader.Read(path);

            result.Data.ShouldBeEquivalentTo(expectedData);
        }
Exemplo n.º 3
0
        public void existing_selection_should_be_expanded()
        {
            var lastPath = new[] { "", "Claimant", "MailingAddress", "Country", "Code" };

            var nodes = JsonDataReader.BuildDataSourcePicker(SampleProjectFiles.BasicFile, SampleData.Standard, lastPath, null, 0, true);

            Assert.That(nodes, Is.Not.Null, "Failed to extract picker data");

            var n1 = nodes.N("");

            Assert.That(n1?.Expanded, Is.True, "Data root should have been expanded");
            var n2 = n1?.Nodes.N("Claimant");

            Assert.That(n2?.Expanded, Is.True, "Claimant node should have been expanded");
            var n3 = n2?.Nodes.N("MailingAddress");

            Assert.That(n3?.Expanded, Is.True, "MailingAddress node should have been expanded");
            var n4 = n3?.Nodes.N("Country");

            Assert.That(n4?.Expanded, Is.True, "Country node should have been expanded");
            var n5 = n4?.Nodes.N("Code");

            Assert.That(n5?.Expanded, Is.True, "Code node should have been expanded");
            Assert.That(n5?.Selected, Is.True, "Code node should have been selected");
        }
Exemplo n.º 4
0
        public IActionResult DataPicker([FromQuery] int docId, [FromQuery] int?pageIndex,
                                        [FromQuery] string?oldPath, [FromQuery] string target, [FromQuery] bool multiplesCanBePicked)
        {
            var sampleData = _fileDatabase.GetSampleData();
            var project    = _fileDatabase.GetDocumentById(docId);

            string[]? repeat = null;
            if (pageIndex is not null)
            {
                var page = project.Pages[pageIndex.Value];
                if (page.RepeatMode.Repeats)
                {
                    repeat = page.RepeatMode.DataPath;
                }
            }

            var prev = Array.Empty <string>();

            if (!string.IsNullOrWhiteSpace(oldPath))
            {
                prev = oldPath.Split('.');
            }

            var tree = JsonDataReader.BuildDataSourcePicker(project, sampleData, prev, repeat, pageIndex, multiplesCanBePicked);
            var list = JsonDataReader.FlattenTree(tree);

            var model = new DataSourceViewModel
            {
                Nodes = list, Target = target
            };

            return(PartialView("DataPathPicker", model) !);
        }
Exemplo n.º 5
0
        public MessageDTO Get(string messageId, string language = "es")
        {
            JsonDataReader jsonDataReader = new JsonDataReader();

            var messages = jsonDataReader.ReadDataFromJson <List <MessageDTO> >(MessagesJson, Encoding.UTF8);

            var result = messages.Where(x => x.Language.Equals(language) && x.MessageId.Equals(messageId)).ToList();

            return(result.FirstOrDefault());
        }
Exemplo n.º 6
0
        private void ReadInData()
        {
            if (FolderName == null)
            {
                return;
            }

            Reader          = new JsonDataReader(FolderName);
            ProductProvider = Reader.ReadInData();
        }
Exemplo n.º 7
0
        public void Read_ShouldReturnFailedResultWithExpectedMessageWhenValuesInWrongFormat()
        {
            var path   = TestUtils.BuildPathFor(@"DataReadersTestCases\JsonDataReaderTestCase2.json");
            var reader = new JsonDataReader();

            var result = reader.Read(path);

            result.Data.Should().BeEmpty();
            result.ErrorMessage.Should().ContainEquivalentOf("could not convert string to double");
        }
Exemplo n.º 8
0
        public void Read_ShouldReturnFailedResultWithExpectedMessageWhenFileNotFound()
        {
            var path = TestUtils.BuildPathFor(@"DataReadersTestCases\NotExistedFile.json");

            var reader = new JsonDataReader();

            var result = reader.Read(path);

            result.Data.Should().BeEmpty();
            result.ErrorMessage.Should().ContainEquivalentOf("Could not find file");
        }
Exemplo n.º 9
0
        private void RegisterBinders()
        {
            // TODO: this should be constructed via IoC container
            DataModelBinder binder = new DataModelBinder(new DataReaderProvider(new IDataReader[] {
                new JsonDataReader(JsonDataReader.CreateSettings(this.Context.IsDebuggingEnabled)),
                new XmlDataReader(XmlDataReader.CreateSettings(), new XmlSerializerNamespaces())
            }));

            binder.DefaultBinder = ModelBinders.Binders.DefaultBinder;

            // set as the new default
            ModelBinders.Binders.DefaultBinder = binder;
        }
Exemplo n.º 10
0
        public void JsonDataReader_Demandbase_IP_Localhost_Test()
        {
            var reader = new JsonDataReader()
            {
                Endpoint = "http://api.demandbase.com/api/v2/ip.json"
            };

            reader.Parameters.Add("key", DEMANDBASE_API_KEY);
            reader.Parameters.Add("query", "127.0.0.1");
            var stream = reader.GetDataStream();

            Assert.IsNull(stream);
        }
Exemplo n.º 11
0
        public void when_repeater_path_is_supplied_it_should_be_added_to_tree()
        {
            var lastPath   = new string[] {};
            var repeatPath = new[] { "", "Reclaims" };

            var nodes = JsonDataReader.BuildDataSourcePicker(SampleProjectFiles.BasicFile, SampleData.Standard, lastPath, repeatPath, 0, true);

            Assert.That(nodes, Is.Not.Null, "Failed to extract picker data");

            var pageRepeatNode = nodes.N("D");

            Assert.That(pageRepeatNode, Is.Not.Null, "No repeat node in place");
            Assert.That(pageRepeatNode?.Text, Is.EqualTo("Page Repeat Data"), "Bad node text");
        }
Exemplo n.º 12
0
        public virtual void Deserialize(string key)
        {
            var value = UserConnection.SessionData[key] as string;

            if (value == null)
            {
                return;
            }
            using (var stringReader = new StringReader(value)) {
                using (var reader = new JsonDataReader(stringReader)) {
                    reader.Read();
                    ReadMetaData(reader);
                }
            }
        }
Exemplo n.º 13
0
    public static T Json2Object(string json)
    {
        T obj = default(T);

        try
        {
            System.IO.StringReader stringReader = new System.IO.StringReader(json);
            JsonReaderSettings     settings     = new JsonReaderSettings();
            settings.AllowNullValueTypes = true;
            JsonDataReader jsonDataReader = new JsonDataReader(settings);
            obj = (T)jsonDataReader.Deserialize(stringReader, typeof(T));
        }
        catch { }

        return(obj);
    }
Exemplo n.º 14
0
        public void repeats_with_single_items_are_still_valid()
        {
            var lastPath   = new string[] {};
            var repeatPath = new[] { "#", "HugeGroup" };

            var nodes = JsonDataReader.BuildDataSourcePicker(SampleProjectFiles.BasicFile, SampleData.Standard, lastPath, repeatPath, 0, true);

            Assert.That(nodes, Is.Not.Null, "Failed to extract picker data");

            var node = nodes.N("D");

            Assert.That(node?.Text, Is.EqualTo("Page Repeat Data"), "Bad node text");
            Assert.That(node?.Nodes.Count, Is.EqualTo(6), "Wrong child-node count"); // all the reclaims should be on page 1

            var subNodeData = nodes.X("D").X("[0]").N("NumberOfShares");

            Assert.That(subNodeData, Is.Not.Null, "Lost path to repeater data");
        }
Exemplo n.º 15
0
        public override void Load()
        {
            // serialization
            this.Bind <IDataWriter>().To <JsonDataWriter>().InSingletonScope();
            this.Bind <JsonWriterSettings>().ToConstant(JsonDataWriter.CreateSettings(false));
            this.Bind <IDataWriter>().To <XmlDataWriter>().InSingletonScope();
            this.Bind <XmlWriterSettings>().ToConstant(XmlDataWriter.CreateSettings(Encoding.UTF8, false));
            this.Bind <XmlSerializerNamespaces>().ToConstant(new XmlSerializerNamespaces());
            this.Bind <IDataWriterProvider>().To <DataWriterProvider>().InSingletonScope();

            this.Bind <IDataReader>().To <JsonDataReader>().InSingletonScope();
            this.Bind <JsonReaderSettings>().ToConstant(JsonDataReader.CreateSettings(true));
            this.Bind <IDataReader>().To <XmlDataReader>().InSingletonScope();
            this.Bind <XmlReaderSettings>().ToConstant(XmlDataReader.CreateSettings());
            this.Bind <IDataReaderProvider>().To <DataReaderProvider>().InSingletonScope();

            // MVC and IoC types
            this.Bind <IActionInvoker>().To <NinjectActionInvoker>().InTransientScope();
        }
Exemplo n.º 16
0
        public static IEnumerable <ComponentSpecification> GetComponents()
        {
            if (_components == null)
            {
                var file = new JsonDataReader().ReadSingleSpecification <ComponentSpecificationFile>(
                    AppEnvironment.Current.AppData + "\\Rules\\Components.json");

                var localComponents = new List <ComponentSpecification>();
                foreach (var code in file.Weapons)
                {
                    localComponents.Add(new ComponentSpecification {
                        Code = code, ComponentType = KnownComponentTypes.Weapon
                    });
                }

                foreach (var code in file.Hulls)
                {
                    localComponents.Add(new ComponentSpecification {
                        Code = code, ComponentType = KnownComponentTypes.Hull
                    });
                }

                foreach (var code in file.Specials)
                {
                    localComponents.Add(new ComponentSpecification {
                        Code = code, ComponentType = KnownComponentTypes.Special
                    });
                }

                foreach (var code in file.Engines)
                {
                    localComponents.Add(new ComponentSpecification {
                        Code = code, ComponentType = KnownComponentTypes.Engines
                    });
                }


                _components = localComponents;
            }

            return(_components);
        }
Exemplo n.º 17
0
        public void JsonDataReader_Demandbase_IP_Test()
        {
            var reader = new JsonDataReader()
            {
                Endpoint = "http://api.demandbase.com/api/v2/ip.json"
            };

            reader.Parameters.Add("key", DEMANDBASE_API_KEY);
            reader.Parameters.Add("query", "24.22.14.68");
            var stream = reader.GetDataStream();

            Assert.IsNotNull(stream);
            var stream2 = new StreamReader(stream, Encoding.UTF8);
            var data    = stream2.ReadToEnd();

            Assert.IsNotNull(data);
            var obj = JsonConvert.DeserializeObject(data);

            Assert.IsNotNull(obj);
        }
Exemplo n.º 18
0
        public void array_data_should_be_selectable_when_multi_mode_is_on()
        {
            var lastPath = new string[] { };
            var p        = Strings.Separator;

            var nodes = JsonDataReader.BuildDataSourcePicker(SampleProjectFiles.BasicFile, SampleData.Standard, lastPath, null, 0, true);

            Assert.That(nodes, Is.Not.Null, "Failed to extract picker data");

            // Check pickable paths are present
            var claimantMailCode = nodes.X("").X("Claimant").X("MailingAddress").X("Country").N("Code");

            Assert.That(claimantMailCode?.CanBePicked, Is.True, "pickable data is not valid");
            Assert.That(claimantMailCode?.DataPath, Is.EqualTo($"{p}Claimant{p}MailingAddress{p}Country{p}Code"), "data path doesn't match node path");
            Assert.That(claimantMailCode?.Depth, Is.EqualTo(4), "incorrect depth in data path");

            // Check object paths are not pickable
            var claimantMailCountry = nodes.X("").X("Claimant").X("MailingAddress").N("Country");

            Assert.That(claimantMailCountry?.CanBePicked, Is.False, "non-pickable data is not valid");

            // With multiple-mode set to true, the reclaims should be pickable
            var reclaims = nodes.X("").N("Reclaims");

            Assert.That(reclaims?.CanBePicked, Is.True, "multiple data was not pickable, but should have been");


            // Should be able to flatten nodes
            var flattened = JsonDataReader.FlattenTree(nodes).ToList();

            // Nothing should be pre-selected as we gave a null path
            var selected = flattened.Where(n => n.Selected).ToList();

            Assert.That(selected, Is.Empty, "There were selected items with no selection path given");

            // Only level-1 nodes should be expanded
            var wrongExpansions = flattened.Where(n => n.Expanded != (n.Depth == 0)).ToList();

            Assert.That(wrongExpansions, Is.Empty, "Some expansions were wrong");
        }
Exemplo n.º 19
0
        private void LoadData()
        {
            m_DataDic.Clear();

            DirectoryInfo dInfo = new DirectoryInfo(m_DataPath);

            if (dInfo.Exists)
            {
                FileInfo[] fileInfos = dInfo.GetFiles("*.json");
                if (fileInfos != null && fileInfos.Length > 0)
                {
                    foreach (var fi in fileInfos)
                    {
                        StreamReader sr      = fi.OpenText();
                        string       content = sr.ReadToEnd();
                        sr.Close();

                        FlyerData flyerData = JsonDataReader.ReadObjectFromJson <FlyerData>(content);
                        m_DataDic.Add(fi.FullName, flyerData);
                    }
                }
            }
        }
Exemplo n.º 20
0
        public void JsonDataReader_Bizo_Classify_Test()
        {
            var reader = new JsonDataReader()
            {
                Endpoint = "http://api.bizographics.com/v4/classify.json"
            };

            reader.UseOauth    = true;
            reader.OAuthApiKey = BIZO_OAUTH_API_KEY;
            reader.OAuthSecret = BIZO_OAUTH_SECRET;
            reader.Parameters.Add("api_key", reader.OAuthApiKey);
            reader.Parameters.Add("secret", reader.OAuthSecret);
            reader.Parameters.Add("ip_address", "206.108.40.109");
            var stream = reader.GetDataStream();

            Assert.IsNotNull(stream);
            var stream2 = new StreamReader(stream, Encoding.UTF8);
            var data    = stream2.ReadToEnd();

            Assert.IsNotNull(data);
            var obj = JsonConvert.DeserializeObject(data);

            Assert.IsNotNull(obj);
        }
 public DbConnectionTests()
 {
     jsonDataReader      = new JsonDataReader();
     dbConnectionFactory = new DbConnectionFactory();
     dbConnectionBuilder = new DbConnectionBuilder();
 }
Exemplo n.º 22
0
        public static DamageAllocationTrack GetDamageAllocationTrack(int trackNumber)
        {
            if (_damageAllocationChart == null)
            {
                var file =
                    new JsonDataReader().ReadSingleSpecification <DamageAllocationChartFile>(
                        AppEnvironment.Current.AppData + "\\Rules\\DamageAllocationChart.json");

                _damageAllocationChart = new DamageAllocationChart();
                foreach (var kvp in file)
                {
                    var track = new DamageAllocationTrack();
                    _damageAllocationChart.Tracks.Add(kvp.Key, track);
                    foreach (var trackUnitCode in kvp.Value)
                    {
                        var code          = trackUnitCode;
                        var oncePerVolley = trackUnitCode.StartsWith("_");
                        if (oncePerVolley)
                        {
                            code = trackUnitCode.Substring(1);
                        }

                        var isAny = trackUnitCode.StartsWith("*");
                        if (isAny)
                        {
                            code = trackUnitCode.Substring(1);
                        }

                        var isEndsWith = trackUnitCode.StartsWith("^");
                        if (isEndsWith)
                        {
                            code = trackUnitCode.Substring(1);
                        }

                        if (isAny)
                        {
                            foreach (var cmp in _components)
                            {
                                if (cmp.ComponentType == code)
                                {
                                    var unit = new DamageAllocationTrackUnit
                                    {
                                        Id            = $"{kvp.Key}-{code}",
                                        Code          = code,
                                        OncePerVolley = oncePerVolley,
                                    };
                                    track.Units.Add(unit);
                                }
                            }

                            continue;
                        }

                        if (isEndsWith)
                        {
                            foreach (var cmp in _components)
                            {
                                if (cmp.Code.EndsWith(code))
                                {
                                    var unit = new DamageAllocationTrackUnit
                                    {
                                        Id            = $"{kvp.Key}-{code}",
                                        Code          = code,
                                        OncePerVolley = oncePerVolley,
                                    };
                                    track.Units.Add(unit);
                                }
                            }

                            continue;
                        }


                        var unit2 = new DamageAllocationTrackUnit
                        {
                            Id            = $"{kvp.Key}-{code}",
                            Code          = code,
                            OncePerVolley = oncePerVolley,
                        };
                        track.Units.Add(unit2);
                    }
                }
            }

            return(_damageAllocationChart.Tracks[trackNumber]);
        }
Exemplo n.º 23
0
        public ActionResult LoadData([Bind(Include = "LayerId,DataFile")] LoadDataViewModel model)
        {
            try
            {
                logger.InfoFormat("UserId={0}", User.Identity.GetUser().Id);

                if (ModelState.IsValid)
                {
                    using (var access = new DataAccess())
                    {
                        var layer = access.Layers.Get(l => l.Id == model.LayerId, includeProperties: "Map,Columns").SingleOrDefault();
                        if (layer == null)
                        {
                            logger.ErrorFormat("NOT_FOUND -- Layer with id={0} not found.", model.LayerId);

                            ModelState.AddModelError("", Error.NOT_FOUND);
                        }
                        else if (layer.Map.User.Id != User.Identity.GetUser().Id)
                        {
                            logger.ErrorFormat("FORBIDDEN -- User with id={0} cannot access layer with id={1}.",
                                               User.Identity.GetUser().Id, model.LayerId);

                            ModelState.AddModelError("", Error.FORBIDDEN);
                        }
                        else if (layer.HasData)
                        {
                            logger.ErrorFormat("UserId={0} -- Layer with id={1} already has data",
                                               User.Identity.GetUser().Id, model.LayerId);

                            ModelState.AddModelError("", string.Format("Layer with id={0} already has data", model.LayerId));
                        }
                        else
                        {
                            var extension = Path.GetExtension(model.DataFile.FileName);
                            if (extension == ".json")
                            {
                                IList <string> messages = new List <string>();
                                JsonDataReader reader   = new JsonDataReader();
                                if (!reader.LoadFile(model.DataFile.InputStream, layer, ref messages))
                                {
                                    foreach (var message in messages)
                                    {
                                        logger.ErrorFormat("USerId={0} -- Error message={1}", User.Identity.GetUser().Id, message);

                                        ModelState.AddModelError("", message);
                                    }
                                }
                                else
                                {
                                    layer.HasData          = true;
                                    layer.Center["lng"]    = 0;
                                    layer.Center["lat"]    = 0;
                                    layer.Center["radius"] = 0;

                                    access.Layers.Update(layer);
                                    access.Save();

                                    logger.InfoFormat("UserId={0} -- successful data load.", User.Identity.GetUser().Id);

                                    ModelState.AddModelError("", "Successful data load!");

                                    return(RedirectToAction("Filter", new { id = model.LayerId }));
                                }
                            }
                            else
                            {
                                logger.ErrorFormat("UserId={0} -- Extension {1} is not supported", User.Identity.GetUser().Id, extension);

                                ModelState.AddModelError("", string.Format("Extension '{0}' is not supported", extension));
                            }
                        }
                    }
                }
                else
                {
                    logger.ErrorFormat("UserId={0} -- Model state is invalid", User.Identity.GetUser().Id);
                }
            }
            catch (Exception ex)
            {
                logger.Fatal("", ex);
                ModelState.AddModelError("", Error.ERROR);
            }

            return(PartialView(model));
        }
Exemplo n.º 24
0
 public JsonDataHandler() : base()
 {
     m_DataWriter = new JsonDataWriter <T>();
     m_DataReader = new JsonDataReader <T>();
 }
Exemplo n.º 25
0
        /// <summary>
        /// Render a PDF into a writable stream
        /// </summary>
        /// <param name="fileSource">File provider, for loading backing PDFs, images, etc that may be referenced by the project file</param>
        /// <param name="data">The data to be rendered into the PDF</param>
        /// <param name="document">The document template project</param>
        /// <param name="target">Target stream to write a PDF into</param>
        /// <returns>Result information, with any error messages and timings</returns>
        public static RenderResultInfo ToStream(IFileSource fileSource, object data, TemplateProject document, Stream target)
        {
            var standardisedData = JsonDataReader.Standardise(data);

            return(new RenderProject(fileSource).ToStream(target, standardisedData, document));
        }
Exemplo n.º 26
0
 public ActionResult Annotation()
 {
     ViewBag.BasicData = BasicSale.GetBasicSales();
     ViewBag.FbData    = JsonDataReader.GetFromAssembly <FinanceData>("fb.json");
     return(View());
 }