Пример #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
        static int Main(string[] args)
        {
            if (args.Length < 4)
            {
                Console.WriteLine("Usage: BSOA.FromJSchema <JSchemaPath> <OutputBsoaSchemaPath> <RootTypeName> <OutputNamespace>");
                return(-2);
            }

            try
            {
                string jschemaPath     = args[0];
                string outputPath      = args[1];
                string rootTypeName    = args[2];
                string outputNamespace = args[3];

                Console.WriteLine($"Converting jschema\r\n  '{jschemaPath}' to \r\n  '{outputPath}'...");
                JsonSchema schema = null;

                using (StreamReader sr = File.OpenText(jschemaPath))
                {
                    schema = SchemaReader.ReadSchema(sr, jschemaPath);
                }

                schema = JsonSchema.Collapse(schema);

                Database db = new Database($"{rootTypeName}Database", outputNamespace, rootTypeName);

                Table root = new Table(rootTypeName);
                db.Tables.Add(root);
                AddColumns(root, schema);

                foreach (KeyValuePair <string, JsonSchema> type in schema.Definitions)
                {
                    string tableName = type.Key.ToPascalCase();
                    if (TypeRenames.TryGetValue(tableName, out string renamed))
                    {
                        tableName = renamed;
                    }

                    Table table = new Table(tableName);
                    AddColumns(table, type.Value);
                    db.Tables.Add(table);
                }

                AsJson.Save(outputPath, db, verbose: true);
                Console.WriteLine("Done.");
                Console.WriteLine();

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: {ex}");
                return(-1);
            }
        }
Пример #4
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);
        }
Пример #5
0
        public void Save(string filePath)
        {
            string extension = Path.GetExtension(filePath).ToLowerInvariant();

            switch (extension)
            {
            case ".bsoa":
                WriteBsoa(filePath);
                break;

            default:
                AsJson.Save(filePath, this);
                break;
            }
        }
Пример #6
0
 public void Save(string filePath)
 {
     AsJson.Save(filePath, this);
 }