コード例 #1
0
ファイル: Program.cs プロジェクト: davidjcaton/JsonFormatter
        private static void Main(string[] args)
        {
            var test1 = JObject.FromObject(new
            {
                IntProperty = 12,
                StringProperty = "Hello World",
                DateProperty = DateTime.Now,
                Bool = true,
                NullValue = (object)null,
                ArrayOfInts = new int[] {1, 2, 3, 4, 5},
                ChildObject = new
                {
                    ChildInt = 50,
                    AnotherNull = (object)null,
                    GrandChildObject = new
                    {
                        ILoveNulls = (object)null
                    }
                },
                AnotherInt = 42,
                AnotherString = "Foo bar!"
            });

            var writer = new JsonFormatter();
            writer.PropertyFormattingRulesCollection.AddPatternRule(
                "ArrayOfInts",
                new FormattingRules
                {
                    NewLineBeforeArrayItem = false,
                    NewLineBeforeArrayStart = false,
                    NewLineBeforeArrayEnd = false
                });

            writer.PropertyFormattingRulesCollection.AddPatternRule(
                "ChildObject",
                new FormattingRules
                {
                    NewLineBeforeArrayItem = false,
                    NewLineBeforeArrayStart = false,
                    NewLineBeforeArrayEnd = false,
                    IncludePropertiesWithNullValues = false,
                    NewLineBeforeProperty = false,
                    NewLineBeforeObjectEnd = false
                });

            writer.PropertyFormattingRulesCollection.AddPatternRule(
                "ChildObject.GrandChildObject",
                new FormattingRules
                {
                    IncludePropertiesWithNullValues = true
                });

            File.WriteAllText("test1_pretty.json", writer.Stringify(test1));
            var isGood = DataValidator.IsEquivalentToKnownGoodSerializer(test1, writer.Stringify(test1));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: davidjcaton/JsonFormatter
        private static void ShowExampleRulesFile()
        {
            var rules = new PropertyRulesCollectionDto
            {
                new PropertyRulesDto
                {
                    PropertyPattern = "ArrayOfInts",
                    Rules = new FormattingRules
                    {
                        NewLineBeforeArrayItem = false,
                        NewLineBeforeArrayStart = false,
                        NewLineBeforeArrayEnd = false
                    }
                },
                new PropertyRulesDto
                {
                    PropertyPattern = "ChildObject",
                    Rules = new FormattingRules
                    {
                        NewLineBeforeArrayItem = false,
                        NewLineBeforeArrayStart = false,
                        NewLineBeforeArrayEnd = false,
                        IncludePropertiesWithNullValues = false,
                        NewLineBeforeProperty = false,
                        NewLineBeforeObjectEnd = false
                    }
                },
                new PropertyRulesDto
                {
                    PropertyPattern = "ChildObject.GrandChildObject",
                    Rules = new FormattingRules
                    {
                        IncludePropertiesWithNullValues = true
                    }
                }
            };

            var formatter = new JsonFormatter();
            formatter.PropertyFormattingRulesCollection.AddPatternRule(".*", new FormattingRules { IncludePropertiesWithNullValues = false });
            var jObject = JsonConvert.DeserializeObject<JToken>(JsonConvert.SerializeObject(rules));
            Console.Out.WriteLine(formatter.Stringify(jObject));
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: davidjcaton/JsonFormatter
        private static int Main(string[] args)
        {
            var settings = new Settings();
            settings.Parse(args);

            if (settings.ShowHelp)
            {
                settings.WriteOptionDescriptions();
                return 0;
            }
            if (settings.ShowExampleRulesFile)
            {
                ShowExampleRulesFile();
                return 0;
            }

            TextReader reader = null;

            try
            {
                // read from file or console if file not specified
                reader = settings.InputFile == null ? Console.In : File.OpenText(settings.InputFile);
                var jsonIn = reader.ReadToEnd();

                // get input object
                var deserializedObject = JsonConvert.DeserializeObject<JToken>(jsonIn);

                // build the formatter
                PropertyFormattingRulesCollection rules = null;
                if (settings.RulesFile != null)
                {
                    var rulesDto = JsonConvert.DeserializeObject<PropertyRulesCollectionDto>(File.ReadAllText(settings.RulesFile));
                    rules = rulesDto.ToModel();
                }
                var formatter = new JsonFormatter(rules);

                // serialize the formatted json
                var jsonOut = formatter.Stringify(deserializedObject);

                // write to file or console if no file specified
                if (settings.OutputFile == null)
                {
                    Console.Out.WriteLine(jsonOut);
                }
                else
                {
                    File.WriteAllText(settings.OutputFile, jsonOut);
                }
            }
            catch (Exception ex)
            {
                // something nasty happened
                Console.Error.WriteLine("Abnormal program termination: exception encountered");
                Console.Error.WriteLine(ex);
                return 1;
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            // all good
            return 0;
        }