Esempio n. 1
0
        public void SerializeType_NoJsonSerializerSettings_Serialized()
        {
            ButlerSerializerSettings serializerSettings = new ButlerSerializerSettings(Assembly.GetExecutingAssembly());

            serializerSettings.PreferredAttributeTypesOnConstructor = new[] { typeof(JsonConstructorAttribute) };

            string       serialized   = ButlerSerializer.SerializeType <ButlerTestClass0> (serializerSettings);
            const string expected     = ButlerTestClass0.ExpectedSerialized;
            string       errorMessage = $"Expected: {expected}; Actual: {serialized}";

            Assert.AreEqual(expected, serialized, errorMessage);
        }
Esempio n. 2
0
        public void SerializeType_NoPreferredCtorAttributes_Serialized()
        {
            ButlerSerializerSettings serializerSettings = new ButlerSerializerSettings(Assembly.GetExecutingAssembly());

            JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings();

            jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsonSerializerSettings.Formatting            = Formatting.None;
            serializerSettings.JsonSerializerSettings    = jsonSerializerSettings;

            string       serialized   = ButlerSerializer.SerializeType <ButlerTestClass0> (serializerSettings);
            const string expected     = ButlerTestClass0.ExpectedSerialized;
            string       errorMessage = $"Expected: {expected}; Actual: {serialized}";

            Assert.AreEqual(expected, serialized, errorMessage);
        }
Esempio n. 3
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="args">Event args.</param>
        private void Execute(object sender, EventArgs args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            ConfirmationWindow confirmationWindow = new ConfirmationWindow();
            DialogResult       buildConfirmation  = confirmationWindow.ShowDialogWithMessage("JsonButler needs to build solution to identify this type.\nProceed?");

            if (buildConfirmation != DialogResult.OK)
            {
                return;
            }

            _package.Dte?.Solution.SolutionBuild.Build(true);

            TextSelection textSelection = _package.Dte?.ActiveDocument.Selection as TextSelection;
            CodeElement   codeElement   = EditorUtilities.GetCodeElement(textSelection);
            AlertWindow   alertWindow;

            if (codeElement == null)
            {
                alertWindow = new AlertWindow();
                alertWindow.ShowDialogWithMessage("Invalid code element selected for serialization.");
                return;
            }

            ITypeResolutionService resolutionService = GetResolutionService(codeElement.ProjectItem.ContainingProject);
            Type type = resolutionService.GetType(codeElement.FullName);

            ButlerSerializerSettings serializerSettings = new ButlerSerializerSettings(type?.Assembly);

            JsonSerializerSettings     jsonSerializerSettings = new JsonSerializerSettings();
            SerializerContractResolver contractResolver       = new SerializerContractResolver();

            contractResolver.SerializationType           = _package.SerializationType;
            jsonSerializerSettings.ContractResolver      = contractResolver;
            jsonSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            jsonSerializerSettings.Formatting            = Formatting.Indented;

            serializerSettings.JsonSerializerSettings = jsonSerializerSettings;

            string serialized = ButlerSerializer.SerializeType(type, serializerSettings);

            Clipboard.SetText(serialized);

            _package.DoAlert("Serialized JSON contents copied to clipboard.");
        }