[1.0] JSON Deserializer Error

  1. #1

    [1.0] JSON Deserializer Error

    Hey,

    I start using Ext.Net from Scratch but using the same methdologies that I
    have used with 0.8.2.

    I'm getting actually a big problem with the deserialization process.

    I'm using this code to get the data from a datasource and save my entity
    wich is "Patient"

    protected void SaveClick(object sender, AjaxEventArgs e)
    
    
        {
    
    
            Notification.Config myConfig = new Notification.Config();
    
    
            myConfig.Icon = Icon.Information;
    
    
            myConfig.Height = 80;
    
    
            myConfig.Width = 230;
    
    
            myConfig.Title = "Notification";
    
    
    
    
    
            Patient Entity = 
    
    Newtonsoft.Json.JsonConvert.DeserializeObject<TList< Patient 
    
    >>(e.ExtraParams["jsonEntity"])[0];
    
    
    
    
    
            TransactionManager tm = 
    
    DataRepository.Provider.CreateTransaction();
    
    
    
    
    
            tm.BeginTransaction();
    
    
    
    
    
            try
    
    
            {
    
    
    
    
    
                if (CurrentId.Text != "0")
    
    
                {
    
    
                    Entity.Id = int.Parse(CurrentId.Text);
    
    
                    DataRepository.PatientProvider.Update(tm, Entity);
    
    
                }
    
    
                else
    
    
                    DataRepository.PatientProvider.Insert(tm, Entity);
    
    
                
    
    
                myConfig.Html = string.Format("Enregistrement : 
    
    {0} a été sauvegardé avec succès", Entity.Nom);
    
    
    
    
    
    
    
    
                CurrentId.Text = Entity.Id.ToString();
    
    
                tm.Commit();
    
    
                Ext.Notification.Show(myConfig);
    
    
    
    
    
    
    
    
            }
    
    
            catch (Exception ex)
    
    
            {
    
    
                if (tm != null &amp;&amp; tm.IsOpen)
    
    
                {
    
    
                    tm.Rollback();
    
    
                    Ext.MessageBox.Alert("Erreur", ex.Message).Show();
    
    
                }
    
    
            }
    this code was working well in 0.8.2. But in 1.0 i GET THIS ERROR.

    Erreur du serveur dans l'application '/Test.Website'.
        
    UNEXPECTED CHARACTER ENCOUNTERED WHILE PARSING 
    
    VALUE: <. LINE 1, POSITION 1. 
    
    Description : Une exception non gérée s'est produite au moment de l'exécution de la demande Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code.
    
    Détails de l'exception: Newtonsoft.Json.JsonReaderException:
    
    Unexpected character encountered while parsing value: <. Line 1, position 1.
    
    Erreur source: 
    
    Ligne 98 :         //Patient Entity = EntityHelper.DeserializeEntityXml<Patient>(myEntityValue);
    Ligne 99 : 
    Ligne 100 :        Patient Entity = JSON.Deserialize<Patient>(e.ExtraParams["jsonEntity"]);
    Ligne 101 :
    Ligne 102 :       
    
    Fichier source :  c:\Users\melek\Documents\Visual Studio 2008\WebSites\Test.Website\Forms\PatientForm.aspx.cs    
    
    Ligne :  100
    So, I tried to track the error and here you are what I found :

    1- In 0.8.2 the e.ExtraParams["jsonEntity"] return the following string :

    [{"Id":0,"NumeroDossier":88877,"Nom":"dcfdsdcdscs","Prenom":"","Age":null,"Tel":null,"Gestite":null,"Parite":null,"Ivg":false,"Ga":false,"Fc":false,"Ge":false,"SafeNameTermeThOriqueSemaine":null,"SafeNameTermeThOriqueJour":null,"TermeEchographiqueSemaine":null,"TermeEchographiqueJour":null,"TypeProtocol":null,"DateFirstPrise":null,"NbTotalCytotec":null,"DateExpulsion":null,"DureeHospitalisation":null,"DureeAdmissionPrise":null,"Vertiges":false,"Vomissement":false,"TroubleTransit":false,"DouleurAbdominoPelvienne":false,"Fievre":false,"AutresEffet":"","Retention":false,"CytotecRetention":false,"AspirationRetention":false,"Echec":false,"ChgmProtocoleEchec":false,"AspirationEchec":false,"Saignement":false,"Endometrie":false,"Perforation":false,"PerforationSecondaire":false,"Fr":false,"Synechie":false,"AutresComplication":"","Atb":false,"Tetracycline":false,"BLactamine":false,"Ocytocine":false,"Methergin":false,"Contraception":false,"CycleArtificiel":false}]
    2- In 1.0, the e.ExtraParams["jsonEntity"] return the following string :

    <Id>0</Id><NumeroDossier>0</NumeroDossier><Nom
    /><Age>0</Age><Tel>0</Tel><Gestite>0</Gestite><Parite>0</Parite><Ivg>false</Ivg><Ga>false</Ga><Fc>false</Fc><Ge>false</Ge><SafeNameTermeThOriqueSemaine>0</SafeNameTermeThOriqueSemaine><SafeNameTermeThOriqueJour>0</SafeNameTermeThOriqueJour><TermeEchographiqueSemaine>0</TermeEchographiqueSemaine><TermeEchographiqueJour>0</TermeEchographiqueJour><TypeProtocol>0</TypeProtocol><DateFirstPrise
    /><NbTotalCytotec>0</NbTotalCytotec><DateExpulsion 
    /><DureeHospitalisation>0</DureeHospitalisation><DureeAdmissionPrise>0</DureeAdmissionPrise><Vertiges>false</Vertiges><Vomissement>false</Vomissement><TroubleTransit>false</TroubleTransit><DouleurAbdominoPelvienne>false</DouleurAbdominoPelvienne><Fievre>false</Fievre><AutresEffet
    
    /><Retention>false</Retention><CytotecRetention>false</CytotecRetention><AspirationRetention>false</AspirationRetention><Echec>false</Echec><ChgmProtocoleEchec>false</ChgmProtocoleEchec><AspirationEchec>false</AspirationEchec><Saignement>false</Saignement><Endometrie>false</Endometrie><Perforation>false</Perforation><PerforationSecondaire>false</PerforationSecondaire><Fr>false</Fr><Synechie>false</Synechie><AutresComplication
    
    /><Atb>false</Atb><Tetracycline>false</Tetracycline><BLactamine>false</BLactamine><Ocytocine>false</Ocytocine><Methergin>false</Methergin><Contraception>false</Contraception><CycleArtificiel>false</CycleArtificiel>
    So, HOW can I solve he problem please !!!

    This is getting me crazy

    Thanks a lot for you help !!
  2. #2

    RE: [1.0] JSON Deserializer Error

    Hi,

    I think you have to encode your extra parameter, try to set Encode="true"
  3. #3

    RE: [1.0] JSON Deserializer Error

    Hey,

    Thanks a lot !! You solve my problem !!! Great Job !! :)))
  4. #4
    Hi, maybe someone can help us with this problem, we think almost the same:

    ASPX
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DirectUpdateTest.aspx.cs"
        Inherits="RCRM_Web.DirectUpdateTest" %>
    
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script type="text/javascript">
            var template = '<span style="color:{0};">{1}</span>';
            var change = function (value) {
                return String.format(template, (value > 0) ? "green" : "red", value);
            };
            var pctChange = function (value) {
                return String.format(template, (value > 0) ? "green" : "red", value + "%");
            };
    
            var startEditing = function (e) {
                if (e.getKey() === e.ENTER) {
                    var grid = GridPanel1,
                        record = grid.getSelectionModel().getSelected(),
                        index = grid.store.indexOf(record);
    
                    grid.startEditing(index, 1);
                }
            };
            var afterEdit = function (e) {
                CompanyX.AfterEdit(e.record.data.ID, e.field, e.originalValue, e.value, e.record.data);
            };
        </script>
    </head>
    <body>
        <ext:ResourceManager runat="server" RemoveViewState="true" IDMode="Explicit" />
        <h1>
            Editable GridPanel With Save To [DirectMethod]</h1>
        <ext:GridPanel ID="GridPanel1" runat="server" Title="Editable GridPanel" StripeRows="true"
            TrackMouseOver="true" Width="600" Height="350" AutoExpandColumn="Name">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:JsonReader IDProperty="ID">
                            <Fields>
                                <ext:RecordField Name="ID" Type="Int" />
                                <ext:RecordField Name="Name" />
                            </Fields>
                        </ext:JsonReader>
                    </Reader>
                </ext:Store>
            </Store>
            <Listeners>
                <KeyDown Fn="startEditing" />
                <AfterEdit Fn="afterEdit" />
            </Listeners>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Header="ID" DataIndex="ID" Width="35" />
                    <ext:Column ColumnID="Name" Header="Name" DataIndex="Name">
                        <Editor>
                            <ext:TextField runat="server" />
                        </Editor>
                    </ext:Column>
                </Columns>
            </ColumnModel>
            <SelectionModel>
                <ext:RowSelectionModel runat="server" SingleSelect="true" />
            </SelectionModel>
        </ext:GridPanel>
    </body>
    </html>
    ASPX.CS
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Ext.Net;
    
    namespace RCRM_Web
    {
        public partial class DirectUpdateTest : System.Web.UI.Page
        {
            public class Company
            {            
                public int ID { get; set; }
                public string Name { get; set; }
            }
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!X.IsAjaxRequest)
                {
                    this.BindData();
                }
            }
    
            private void BindData()
            {
                this.GridPanel1.Store.Primary.DataSource = this.GetData();
                this.GridPanel1.Store.Primary.DataBind();
            }
    
            private List<Company> GetData()
            {
                return new List<Company> 
            {
                new Company { ID = 1, Name = "3m Co"}
            };
            }
    
            [DirectMethod(Namespace = "CompanyX")]
            public void AfterEdit(int id, string field, string oldValue, string newValue, object customer) 
            {            
                string message = "<b>Property:</b> {0}<br /><b>Field:</b> {1}<br /><b>Old Value:</b> {2}<br /><b>New Value:</b> {3}";
                X.Msg.Notify("Edit Record #" + id.ToString(), string.Format(message, id, field, "", "")).Show();
                this.GridPanel1.Store.Primary.CommitChanges();
            }
        }
    }

    When we tried to change in the EDITOR the Method isnt executed,
    and Firebug shows us:

    POST OF AfterEdit:
    submitDirectEventConfig	{"config":{"__EVENTTARGET":"ctl01","__EVENTARGUMENT":"-|public|AfterEdit","extraParams":{"id":1,"field":"Name","oldValue":"3m Co","newValue":"3m Co TEST","customer":{"ID":1,"Name":"3m Co TEST"}}}}
    
    submitDirectEventConfig=%7B%22config%22%3A%7B%22__EVENTTARGET%22%3A%22ctl01%22%2C%22__EVENTARGUMENT%22%3A%22-%7Cpublic%7CAfterEdit%22%2C%22extraParams%22%3A%7B%22id%22%3A1%2C%22field%22%3A%22Name%22%2C%22oldValue%22%3A%223m%20Co%22%2C%22newValue%22%3A%223m%20Co%20TEST%22%2C%22customer%22%3A%7B%22ID%22%3A1%2C%22Name%22%3A%223m%20Co%20TEST%22%7D%7D%7D%7D
    ANSWER:
    {success:false,errorMessage:"Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Line 1, position 1.\r\n   at Newtonsoft.Json.JsonTextReader.ParseValue(Char currentChar)\r\n   at Newtonsoft.Json.JsonTextReader.Read()\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType)\r\n   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n   at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)\r\n   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n   at Ext.Net.JSON.Deserialize(String value, Type type, List`1 converters, IContractResolver resolver)\r\n   at Ext.Net.JSON.Deserialize(String value, Type type)\r\n   at Ext.Net.DirectMethod.Invoke(Object target, HttpContext context, ParameterCollection args)\r\n   at Ext.Net.DirectMethod.Invoke(Object target, ParameterCollection args)\r\n   at Ext.Net.ResourceManager.RaisePostBackEvent(String eventArgument)"}

    If we leave change the example and remove object customer and e.record.data to this:

    public void AfterEdit(int id, string field, string oldValue, string newValue) { }
    var afterEdit = function (e) {
                CompanyX.AfterEdit(e.record.data.ID, e.field, e.originalValue, e.value);
            };
    It is working. So there must be a problem with object deserialization. On examples.ext.net its working, but on our VS2010 with 1.0 Preview not. We have regional settings of germany and thought its a date thing, but as you can see above, we cut it down to strings.
    So is there now stll some encoding problem or does Ext.NET Preview generate wrong JSON Object for Codebehind?


    Thanks a lot.
    Last edited by geoffrey.mcgill; Feb 03, 2011 at 4:26 PM.
  5. #5
    We solved it ourself. It was a Encode problem, which is might be already fixed on the SVN, but not on the current released Ext.NET 1.0 RC.

    What we've done:

    CompanyX.AfterEdit(e.record.data.ID, e.field, e.originalValue, e.value, Ext.encode(e.record.data));

    The JSON Object from DataBinding comes so back to the DirectMethod correct escaped for deserialising.
  6. #6

    an extra feild

    hi there
    i have a problem , can you help me pls ?
    when i deserialize my jsonreader , it has an extra field , named "id" that has conflict with my fields
  7. #7
    Quote Originally Posted by Mina View Post
    hi there
    i have a problem , can you help me pls ?
    when i deserialize my jsonreader , it has an extra field , named "id" that has conflict with my fields
    Hi Mina,

    It would be best to start a new forum thread. If you can, please also include a code sample demonstrating as much of the problem as you can.
    Geoffrey McGill
    Founder

Similar Threads

  1. Replies: 1
    Last Post: Mar 26, 2012, 3:54 PM
  2. Error 404.15 on AjaxMethod.Request with JSon
    By Spootnick in forum 1.x Help
    Replies: 1
    Last Post: Jun 16, 2011, 4:31 PM
  3. [CLOSED] JSON error using HttpWriteProxy
    By rthiney in forum 1.x Legacy Premium Help
    Replies: 12
    Last Post: Feb 24, 2010, 8:27 PM
  4. JSON error using HttpWriteProxy
    By cramsundar in forum 1.x Help
    Replies: 0
    Last Post: Feb 17, 2010, 7:37 PM
  5. [CLOSED] error with json StoreDataHandler
    By idrissb in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Aug 25, 2009, 3:35 PM

Posting Permissions