[CLOSED] JsonSerializerSettings breaks TriggerField

  1. #1

    [CLOSED] JsonSerializerSettings breaks TriggerField

    After more than 5 hours i was able to find what was breaking TriggerField :(

    When JSON.GlobalSettings is set to new Newtonsoft.Json.JsonSerializerSettings() (Global.asax line 5), the FieldTrigger does not work as expected.

    Error


    It was supposed to be:


    Global.asax
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            JSON.GlobalSettings = new Newtonsoft.Json.JsonSerializerSettings();
    
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
    
        }
    
        protected void Application_AuthenticateRequest(object sender, System.EventArgs e)
        {
            string url = HttpContext.Current.Request.FilePath;
    
            if (url.EndsWith("ext.axd"))
            {
                HttpContext.Current.SkipAuthorization = true;
            }
        }
    
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{extnet-root}/{extnet-file}/ext.axd");
    
            routes.MapRoute("Default", "{controller}/{action}");
        }
    }
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
    </head>
    <body>
        <ext:ResourceManager runat="server" SeparateUIStyles="false" Theme="Gray" />
     
        <ext:DropDownField Margin="10"
            ID="_ddd"
            Editable="false"
            Width="800"
            runat="server">
            <Triggers>
                <ext:FieldTrigger Icon="Clear" Weight="-1" />
            </Triggers>
            <Component>
                <ext:Panel Title="Inner Panel" runat="server" Height="600">
                    <Loader Url="http://www.ext.net" Mode="Frame" runat="server" />
                </ext:Panel>
            </Component>
        </ext:DropDownField>
    </body>
    </html>



    Unfortunately in my scenario was pretty more difficult to find what was going on because i use ComponentLoader to render the page contents, so, i had to "deconstruct" my application to a smaller (smallest) scenario to be able to identify the issue

    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <script type="text/javascript">
            var DocumentReady = function () {
                ProcessContent(App._hdnContent.value, BindContent);
            }
            var ProcessContent = function (content) {
                content = Ext.decode(content, true);
                var auxContent = content;
                if (auxContent && auxContent['x.res']) {
    
                    if (auxContent.config) {
                        content = Ext.decode(auxContent.config, true);
                    }
    
                    if (auxContent['x.res'].ns) {
                        Ext.ns.apply(Ext, auxContent['x.res'].ns);
                    }
    
                    if (auxContent['x.res'].res) {
                        Ext.net.ResourceMgr.load(auxContent['x.res'].res, function () {
                            BindContent(content);
                        });
                    }
                    else {
                        BindContent(content);
                    }
                }
                else {
                    BindContent(content);
                }
            }
            var BindContent = function (content) {
                if (content != null) {
                    App._vwp.add(content);
                }
                App._hdnContent.destroy();
            }
        </script>
    </head>
    <body>
        <ext:ResourceManager runat="server" Theme="Gray">
            <Listeners>
                <DocumentReady Handler="DocumentReady();" Delay="1000" />
            </Listeners>
        </ext:ResourceManager>
        <ext:Hidden ID="_hdnContent" Text="<%# ViewBag.ApplicationContent %>"
            AutoDataBind="true" runat="server" />
        <ext:Panel  ID="_vwp" Padding="10" Width="300" Layout="FitLayout" Border="false" runat="server">
        </ext:Panel>
    </body>
    </html>

    public class ExampleController : System.Web.Mvc.Controller
    {
        public ActionResult Index()
        {
            SelectBox sb = new SelectBox
            {
                FieldLabel = "Ext.Net",
            };
            sb.Items.Add(new ListItem { Text = "E", Value = "1" });
            sb.Items.Add(new ListItem { Text = "X", Value = "2" });
            sb.Items.Add(new ListItem { Text = "T", Value = "3" });
    
            sb.Triggers.Add(new FieldTrigger { Icon = TriggerIcon.Clear, Weight = -1 });
    
            ViewBag.ApplicationContent = ComponentLoader.ToConfig(sb);
    
            return View();
        }
    }
    Attached Thumbnails Click image for larger version. 

Name:	CL001.png 
Views:	46 
Size:	22.2 KB 
ID:	16831   Click image for larger version. 

Name:	CL002.png 
Views:	48 
Size:	22.2 KB 
ID:	16841  
    Last edited by Daniil; Dec 08, 2014 at 2:00 PM. Reason: [CLOSED]
  2. #2
    The following image shows the difference when TriggerField is serialized using JSON.GlobalSettings is set to new Newtonsoft.Json.JsonSerializerSettings

    Click image for larger version. 

Name:	CL003.png 
Views:	15 
Size:	89.3 KB 
ID:	16851

    Thanks in advance
  3. #3
    That's why i use Newtonsoft.Json.JsonSerializerSettings: http://forums.ext.net/showthread.php...m-as-its-value
  4. #4
    Hi Raphael,

    If a developer changes the JSON.GlobalSettings or RequestSettings, he is responsible for how the things are serialized.

    It is how the GlobalSettings property looks by default.

    private static bool readFromConfig = false;
    
    private static JsonSerializerSettings globalSettings = new JsonSerializerSettings {
        Converters = JSON.Converters,
        DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    };
    
    public static JsonSerializerSettings GlobalSettings
    {
        get
        {
            if (!JSON.readFromConfig)
            {
                JSON.globalSettings.DateTimeZoneHandling = GlobalConfig.Settings.DateTimeZoneHandling;
                JSON.readFromConfig = true;
            }
    
            return JSON.globalSettings;
        }
        set
        {
            JSON.globalSettings = value;
        }
    }
    All the settings are quite essential for proper Ext.NET serialization.

    When you use this
    JSON.GlobalSettings = new Newtonsoft.Json.JsonSerializerSettings();
    all the settings are being reset.

    Well, it is the reason why a TriggerField is not serialized correctly.

    I am not sure what to suggest, because I don't know why you reset the GlobalSettings. Please elaborate on that.
  5. #5
    That's why i used Newtonsoft.Json.JsonSerializerSettings: http://forums.ext.net/showthread.php...m-as-its-value > http://forums.ext.net/showthread.php...l=1#post115834

    All the settings are quite essential for proper Ext.NET serialization.
    I completely agree with you.

    At this time i prefer to stop overriding JSON.GlobalSettings since i can break the application if i do so.

    The purpose of this thread was to inform to others who may also overriden as i did.

    Thank you Daniil, once again.

    Please mark this thread as closed.
    Last edited by RCN; Dec 08, 2014 at 1:53 PM.
  6. #6
    On my scenario, i was able to stop overriding JSON.GlobalSettings, but as stated on http://forums.ext.net/showthread.php...m-as-its-value i need to serialize enum as its value, so i needed to remove StringEnumConverter from JSON.GlobalSettings.Converters, as shown below:

    protected void Application_Start()
    {
        JsonConverter stringEnumConverter = JSON.GlobalSettings.Converters.FirstOrDefault(converter => converter is StringEnumConverter);
    
        if (stringEnumConverter != null)
        {
            JSON.GlobalSettings.Converters.Remove(stringEnumConverter);
        }
    }
  7. #7
    Just one more information:

    FieldTrigger requires Ext.Net.JRawValueJsonConverter, which is not defined by default in JsonSerializerSettings;

    It's possible to overcome the issue by adding JRawValueJsonConverter to JsonSerializerSettings' converters
    protected void Application_Start()
    {
        JsonSerializerSettings settings = new JsonSerializerSettings();
    
        settings.Converters.Add(new Ext.Net.JRawValueJsonConverter());
    
        JSON.GlobalSettings = settings;
    }
    But as stated by Daniil (http://forums.ext.net/showthread.php...1#post115834): Be careful to override the GlobalSettings. It affects all serialization in Ext.NET and it can break something

Similar Threads

  1. [CLOSED] GridPanel RowSelection breaks GridDragDrop
    By stratadev in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: Nov 29, 2012, 6:29 PM
  2. [CLOSED] Ext.NET 2.0 breaks ASP.NET RegisterHiddenField method
    By jchau in forum 2.x Legacy Premium Help
    Replies: 7
    Last Post: Nov 22, 2012, 12:14 PM
  3. [CLOSED] When I try to set an item in the comboBox it breaks.
    By Fahd in forum 2.x Legacy Premium Help
    Replies: 4
    Last Post: Jun 19, 2012, 9:15 PM
  4. [FIXED] 0.5.3 Breaks Fieldsets
    By jlertle in forum Bugs
    Replies: 4
    Last Post: Jul 02, 2008, 11:22 PM
  5. Replies: 3
    Last Post: Jun 24, 2008, 12:32 AM

Posting Permissions