コード例 #1
0
        private static void SerializeDeserialize(TextWriter writer, string unitTestsFolder, string unitTestFile, object obj, JsonReaderSettings readerSettings, JsonWriterSettings writerSettings)
        {
            writer.WriteLine(JsonText.Seperator);

            string source = String.Empty;

            try
            {
                using (JsonWriter jsonWriter = new JsonWriter(unitTestsFolder + unitTestFile, writerSettings))
                {
                    jsonWriter.Write(obj);
                }

                source = File.ReadAllText(unitTestsFolder + unitTestFile);

                obj = new JsonReader(source, readerSettings).Deserialize((obj == null) ? null : obj.GetType());
                writer.WriteLine("READ: " + unitTestFile);
                writer.WriteLine("Result: {0}", (obj == null) ? "null" : obj.GetType().FullName);
            }
            catch (JsonDeserializationException ex)
            {
                int col, line;
                writer.WriteLine("ERROR: " + unitTestFile);
                ex.GetLineAndColumn(source, out line, out col);
                writer.WriteLine("-- \"{0}\" ({1}, {2})", ex.Message, line, col);
            }
            catch (Exception ex)
            {
                writer.WriteLine("ERROR: " + unitTestFile);
                writer.WriteLine("-- \"{0}\"", ex.Message);
            }
        }
コード例 #2
0
        public static void RunTest(TextWriter writer, string unitTestsFolder, string outputFolder)
        {
            string[] unitTests = Directory.GetFiles(unitTestsFolder, UnitTestsFiles, SearchOption.AllDirectories);
            if (unitTests.Length > 0)
            {
                JsonReaderSettings readerSettings = new JsonReaderSettings();
                readerSettings.TypeHintName            = StronglyTyped.MyTypeHintName;
                readerSettings.AllowNullValueTypes     = true;
                readerSettings.AllowUnquotedObjectKeys = true;

                JsonWriterSettings writerSettings = new JsonWriterSettings();
                writerSettings.TypeHintName = StronglyTyped.MyTypeHintName;
                writerSettings.PrettyPrint  = false;
                writerSettings.MaxDepth     = 100;

                writer.WriteLine(JsonText.Seperator);
                writer.WriteLine("JsonReaderSettings:");
                new JsonWriter(writer).Write(readerSettings);

                writer.WriteLine(JsonText.Seperator);
                writer.WriteLine("JsonWriterSettings:");
                new JsonWriter(writer).Write(writerSettings);

                foreach (string unitTest in unitTests)
                {
                    string source = String.Empty;

                    try
                    {
                        writer.WriteLine(JsonText.Seperator);

                        source = File.ReadAllText(unitTest);
                        JsonReader jsonReader = new JsonReader(source, readerSettings);

                        object obj, obj2;
                        obj2 = obj = jsonReader.Deserialize();

                        do
                        {
                            writer.WriteLine("READ: {0}", unitTest.Replace(unitTestsFolder, ""));
                            writer.WriteLine("Result: {0}", (obj == null) ? "null" : obj.GetType().FullName);

                            obj = jsonReader.Deserialize();
                        } while (obj != null);

                        string outputFile = unitTest.Replace(unitTestsFolder, outputFolder);
                        string outputDir  = Path.GetDirectoryName(outputFile);
                        if (!Directory.Exists(outputDir))
                        {
                            Directory.CreateDirectory(outputDir);
                        }
                        using (JsonWriter jsonWriter = new JsonWriter(outputFile, writerSettings))
                        {
                            jsonWriter.Write(obj2);
                        }
                    }
                    catch (JsonDeserializationException ex)
                    {
                        int col, line;
                        ex.GetLineAndColumn(source, out line, out col);

                        writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, ""));
                        writer.WriteLine("-- \"{0}\" ({1}, {2})", ex.Message, line, col);
                        continue;
                    }
                    catch (Exception ex)
                    {
                        writer.WriteLine("ERROR: {0}", unitTest.Replace(unitTestsFolder, ""));
                        writer.WriteLine("-- \"{0}\"", ex.Message);
                        continue;
                    }
                }
            }
            else
            {
                writer.WriteLine(ErrorMessage);
            }
        }