示例#1
0
        public void Database_NewtonsoftCompatibility()
        {
            // Test that basic Database, Table, and Row types can successfully roundtrip via Newtonsoft.Json serialization by default.
            // These use generated JsonConverter classes to serialize using safe constructors and good default behaviors.

            V1.Community v1 = new V1.Community();
            v1.People = new List <V1.Person>();

            v1.People.Add(new V1.Person()
            {
                Age = 39, Name = "Scott"
            });
            v1.People.Add(new V1.Person()
            {
                Age = 36, Name = "Adam"
            });

            string serializeToPath = Path.GetFullPath("Community.V1.json");

            AsJson.Save(serializeToPath, v1, verbose: true);

            V1.Community roundTrip = AsJson.Load <V1.Community>(serializeToPath);
            CollectionReadVerifier.VerifySame(v1.People, roundTrip.People);

            AsJson.Save(serializeToPath, v1, verbose: true);
            roundTrip = AsJson.Load <V1.Community>(serializeToPath);
            CollectionReadVerifier.VerifySame(v1.People, roundTrip.People);

            // Verify V2 object model will load community (post-replacements make Person parsing non-strict)
            V2.Community v2 = AsJson.Load <V2.Community>(serializeToPath);
            Assert.Equal(2, v2.People.Count);
            Assert.Equal(v1.People[0].Name, v2.People[0].Name);
        }
示例#2
0
        public void Database_NewtonsoftCompatibility()
        {
            // Test that basic Database, Table, and Row types can successfully roundtrip via Newtonsoft.Json serialization by default.
            // These use generated JsonConverter classes to serialize using safe constructors and good default behaviors.

            V1.Community v1 = new V1.Community();
            v1.People.Add(new V1.Person()
            {
                Age = 39, Name = "Scott"
            });
            v1.People.Add(new V1.Person()
            {
                Age = 36, Name = "Adam"
            });

            string serializeToPath = Path.GetFullPath("Community.V1.json");

            AsJson.Save(serializeToPath, v1, verbose: true);

            V1.Community roundTrip = AsJson.Load <V1.Community>(serializeToPath);
            CollectionReadVerifier.VerifySame(v1.People, roundTrip.People);

            AsJson.Save(serializeToPath, v1, verbose: true);
            roundTrip = AsJson.Load <V1.Community>(serializeToPath);
            CollectionReadVerifier.VerifySame(v1.People, roundTrip.People);
        }
示例#3
0
        public void Generate()
        {
            Console.WriteLine($"Generating BSOA object model from schema\r\n  '{SchemaPath}' at \r\n  '{OutputFolder}'...");

            Database db = AsJson.Load <Database>(SchemaPath);

            if (Directory.Exists(OutputFolder))
            {
                Directory.Delete(OutputFolder, true);
            }
            Directory.CreateDirectory(OutputFolder);

            Dictionary <string, string> postReplacements = new Dictionary <string, string>();

            if (PostReplacementsPath != null)
            {
                postReplacements = AsJson.Load <Dictionary <string, string> >(PostReplacementsPath);
            }

            // List and Dictionary read and write methods need a writeValue delegate passed
            postReplacements["me.([^ ]+) = JsonToIList<([^>]+)>.Read\\(reader, root\\)"] = "JsonToIList<$2>.Read(reader, root, me.$1, JsonTo$2.Read)";
            postReplacements["JsonToIList<([^>]+)>.Write\\(writer, ([^,]+), item.([^,]+), default\\);"] = "JsonToIList<$1>.Write(writer, $2, item.$3, JsonTo$1.Write);";

            postReplacements["me.([^ ]+) = JsonToIDictionary<String, ([^>]+)>.Read\\(reader, root\\)"] = @"me.$1 = JsonToIDictionary<String, $2>.Read(reader, root, null, JsonTo$2.Read)";
            postReplacements["JsonToIDictionary<String, ([^>]+)>.Write\\(writer, ([^,]+), item.([^,]+), default\\);"] = "JsonToIDictionary<String, $1>.Write(writer, $2, item.$3, JsonTo$1.Write);";

            // Generate Database class
            new ClassGenerator(TemplateType.Database, TemplatePath(@"Internal\CompanyDatabase.cs"), @"Internal\{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Tables
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Internal\TeamTable.cs"), @"Internal\{0}Table.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Entities
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Team.cs"), "{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Root Entity (overwrite normal entity form)
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Company.cs"), @"{0}.cs", postReplacements)
            .Generate(OutputFolder, db.Tables.Where((table) => table.Name.Equals(db.RootTableName)).First(), db);

            // Generate Entity Json Converter
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Json\JsonToTeam.cs"), @"Json\JsonTo{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Root Entity Json Converter (overwrite normal entity form)
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Json\JsonToCompany.cs"), @"Json\JsonTo{0}.cs", postReplacements)
            .Generate(OutputFolder, db.Tables.Where((table) => table.Name.Equals(db.RootTableName)).First(), db);

            Console.WriteLine("Done.");
            Console.WriteLine();
        }
示例#4
0
        public static FileSystem Load(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLowerInvariant();

            switch (extension)
            {
            case ".bsoa":
                return(ReadBsoa(filePath));

            default:
                return(AsJson.Load <FileSystem>(filePath));
            }
        }
示例#5
0
        public void Generate()
        {
            Console.WriteLine($"Generating BSOA object model from schema\r\n  '{SchemaPath}' at \r\n  '{OutputFolder}'...");

            Database db = AsJson.Load <Database>(SchemaPath);

            if (Directory.Exists(OutputFolder))
            {
                Directory.Delete(OutputFolder, true);
            }
            Directory.CreateDirectory(OutputFolder);

            PostReplacements postReplacements = new PostReplacements();

            if (PostReplacementsPath != null)
            {
                postReplacements = AsJson.Load <PostReplacements>(PostReplacementsPath);
            }

            // List and Dictionary read and write methods need a writeValue delegate passed
            AddDefaultPostReplacements(postReplacements);

            // Generate Database class
            new ClassGenerator(TemplateType.Database, TemplatePath(@"Internal\CompanyDatabase.cs"), @"Internal\{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Tables
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Internal\TeamTable.cs"), @"Internal\{0}Table.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Entities
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Team.cs"), "{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Root Entity (overwrite normal entity form)
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Company.cs"), @"{0}.cs", postReplacements)
            .Generate(OutputFolder, db.Tables.Where((table) => table.Name.Equals(db.RootTableName)).First(), db);

            // Generate Entity Json Converter
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Json\JsonToTeam.cs"), @"Json\JsonTo{0}.cs", postReplacements)
            .Generate(OutputFolder, db);

            // Generate Root Entity Json Converter (overwrite normal entity form)
            new ClassGenerator(TemplateType.Table, TemplatePath(@"Json\JsonToCompany.cs"), @"Json\JsonTo{0}.cs", postReplacements)
            .Generate(OutputFolder, db.Tables.Where((table) => table.Name.Equals(db.RootTableName)).First(), db);

            Console.WriteLine("Done.");
            Console.WriteLine();
        }
示例#6
0
        public void JsonToPerson_Basics()
        {
            Community readRoot = new Community();
            Person    p        = new Person()
            {
                Name = "Scott", Birthdate = new DateTime(1981, 01, 01, 00, 00, 00, DateTimeKind.Utc)
            };

            // Serialization via typed methods
            JsonRoundTrip.ValueOnly(p, JsonToPerson.Write, (r, db) => JsonToPerson.Read(r, readRoot));
            JsonRoundTrip.ValueOnly(null, JsonToPerson.Write, (r, db) => JsonToPerson.Read(r, readRoot));
            JsonRoundTrip.NameAndValue(p, null, (w, pn, v, dv, req) => JsonToPerson.Write(w, pn, v, req), (r, db) => JsonToPerson.Read(r, readRoot));

            // JsonConverter.CanConvert (not called by default serialization)
            JsonToPerson converter = new JsonToPerson();

            Assert.True(converter.CanConvert(typeof(Person)));
            Assert.False(converter.CanConvert(typeof(Community)));

            // Serialization via Newtonsoft default
            string personPath = "Person.NewtonsoftDefault.json";

            AsJson.Save(personPath, p, true);
            Person roundTrip = AsJson.Load <Person>(personPath);

            Assert.Equal(p, roundTrip);

            // Serialize null via Newtonsoft
            AsJson.Save <Person>(personPath, null);
            roundTrip = AsJson.Load <Person>(personPath);
            Assert.Null(roundTrip);

            // Serialize empty root
            string communityPath = "Community.NewtonsoftDefault.json";

            AsJson.Save(communityPath, readRoot);
            Community roundTripCommunity = AsJson.Load <Community>(communityPath);

            Assert.Null(roundTripCommunity.People);

            // Serialize root with Person
            readRoot.People = new List <Person>();
            readRoot.People.Add(p);
            AsJson.Save(communityPath, readRoot);
            roundTripCommunity = AsJson.Load <Community>(communityPath);
            Assert.Single(roundTripCommunity.People);
            Assert.Equal(p, roundTripCommunity.People[0]);
            Assert.Equal("Scott", roundTripCommunity.People[0].Name);
        }
示例#7
0
 public static FileSystem Load(string filePath)
 {
     return(AsJson.Load <FileSystem>(filePath));
 }